Nacos 单机版安装与使用

795 阅读2分钟

Nacos

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

安装环境

🐖 JAVA和Maven之前装的就不做安装步骤描述

  1. MAC
  2. JDK1.8+ (java version "1.8.0_191")
  3. Maven 3.6.3

安装Nacos

Nacos版本:2.0.0-bugfix (Mar 30th, 2021)

此时最新版,如果需要其他版本自行下载。百度云盘中也是这个版本。

下载地址

官方地址:github.com/alibaba/nac…

链接: pan.baidu.com/s/1ppOfKViL…
密码: 2dth

下载源码或者安装包

🐖 我是通过这种方式,先下载安装包的。git clone特别慢我就直接下载了

git clone https://github.com/alibaba/nacos.git

cd nacos/

mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U

ls -al distribution/target/

// change the $version to your actual path
# 启动脚本目录
cd distribution/target/nacos-server-$version/nacos/bin

下载编译后压缩包方式

unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz
 
cd nacos/bin

启动服务

Linux/Unix/Mac

启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

ubuntu

如果您使用的是ubuntu系统,或者运行脚本报错提示[符号找不到],可尝试如下运行:

bash startup.sh -m standalone

Windows

启动命令(standalone代表着单机模式运行,非集群模式):

cmd startup.cmd -m standalone

关闭服务

Linux/Unix/Mac

sh shutdown.sh

Windows

cmd shutdown.cmd

Spring Cloud + Naocs

pom

版本依赖很重要,我用的基本上都是最新版
版本说明 Wiki

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
            <groupId>com.xxx.www</groupId>
            <artifactId>xxx</artifactId>
            <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Consumer Project for Spring Boot</description>

        <dependencies>

            <!-- 引入 common_util 依赖 -->
            <dependency>
                <groupId>com.havexin.www</groupId>
                <artifactId>common-module</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
            </dependency>

            <!-- mybatis plus 部分 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>

            <!-- 引入mybatis plus依赖 -->
            <!-- Mybatis的增强工具包,可简化开发 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.2</version>
            </dependency>

            <!-- 引入 alibaba druid 数据库链接依赖 -->
            <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid-spring-boot-starter</artifactId>
                    <version>1.2.4</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/log4j/log4j -->
            <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>1.2.17</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.76</version>
            </dependency>

            <!-- Nacos 与 Spring Cloud 服务注册与发现 整合依赖 -->
            <!-- nacos服务 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>2.2.5.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
                <version>1.3.3</version>
            </dependency>

        </dependencies>

    <dependencyManagement>
        <dependencies>

            <!-- Spring Cloud 依赖 -->
            <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <!-- Provide the latest stable Spring Cloud release train version (e.g. 2020.0.0) -->
                    <version>2020.0.2</version>
                    <type>pom</type>
                    <scope>import</scope>
            </dependency>

            <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.2.5.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
            </dependency>

         </dependencies>

    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

不加下面这个报错,之前用的低版本没事

nacos-error.png

<dependencyManagement>
    <dependencies>
        <!-- Spring Cloud 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <!-- Provide the latest stable Spring Cloud release train version (e.g. 2020.0.0) -->
            <version>2020.0.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
     </dependencies>
</dependencyManagement>

yml

server:
  port: 9090
  servlet:
    context-path: /consumer
  compression:
    enabled: true
    min-response-size: 2048
  tomcat:
    uri-encoding: UTF-8

spring:
  # 项目名称
  application:
    name: consumer
  # nacos 配置
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

启动类

@SpringBootApplication
// 通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能
@EnableDiscoveryClient
public class ConsumerApplication {
    public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
    }
}

访问Nacos Server

开启服务

image.png

关闭服务

image.png


反应还是很迅速的,哈哈😀

简单demo跑通了,后续更新其他参数和集群版本

做个笔记记录下来,以后方便自己用.有点粗糙,不断补充