Nacos是阿里巴巴开发的用于发现、配置和管理微服务。与Spring Cloud Alibaba完美结合。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。Nacos是国产的,所以中文文档也很丰富。

启动配置管理

引入依赖

<dependencies>
        <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>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
    </dependencies>
    <!-- SpringCloud依赖,起到管理版本的作用 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${com-alibaba-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置

在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 元数据

spring.application.name=spring-cloud-alibaba-nacos-config-example
spring.cloud.nacos.config.server-addr=127.0.0.1:8848

需要先启动Nacos,否则启动后会有些报错

完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能

@RefreshScope
 class SampleController {

     @Value("${user.name}")
     String userName;

     @Value("${user.age}")
     int age;
 }

启动 Nacos Server 并添加配置

下载Nacos

GitHub地址:https://github.com/alibaba/na...

启动 Server

进去其相对文件夹 nacos/bin,并对照操作系统实际情况执行如下命令。详情参考官方文档

  • Linux/Unix/Mac 操作系统,执行命令 sh startup.sh -m standalone
  • Windows 操作系统,执行命令 cmd startup.cmd

在命令行执行如下命令,向 Nacos Server 中添加一条配置。

POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17"

启动服务发现

引入依赖

父级

<properties>
    <java.version>1.8</java.version>
    <com-alibaba-cloud.version>2.2.5.RELEASE</com-alibaba-cloud.version>
</properties>

<modules>
    <module>nacos-discovery-consumer</module>
    <module>nacos-discovery-provider</module>
</modules>

<dependencies>
    <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>
</dependencies>
<!-- SpringCloud依赖,起到管理版本的作用 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${com-alibaba-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

consumer

<properties>
    <spring-cloud-netflix.version>2.2.5.RELEASE</spring-cloud-netflix.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        <version>${spring-cloud-netflix.version}</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
    </dependency>
</dependencies>

provider

<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
    </dependency>
</dependencies>

添加注解

通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能

// 开启服务注册发现功能
@SpringBootApplication
@EnableDiscoveryClient
public class NacosDiscoveryProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosDiscoveryProviderApplication.class, args);
    }
}

配置

application.properties 中配置 Nacos server 的地址

consumer

server.port=18083
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# nacos认证信息
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos

provier

server.port=18082
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# nacos认证信息
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos

consumer

  1. config

通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能。给 RestTemplate 实例添加 @LoadBalanced 注解,开启 @LoadBalancedRibbon 的集成

@Configuration
public class NacosConfig {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
  1. controller
@RestController
public class TestController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) {
        return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
    }
}

provier

  1. controller
@RestController
public class TestController {
    @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
    public String echo(@PathVariable String string) {
        return "Hello Nacos Discovery " + string;
    }
}

最后启动 ProviderApplicationConsumerApplication ,调用 http://localhost:8080/echo/2021,返回内容为 Hello Nacos Discovery 2021`。

References

[1] Nacos Config Example: https://github.com/alibaba/sp...
[2] Nacos Spring Cloud: https://nacos.io/zh-cn/docs/q...
[3] Nacos: https://nacos.io/zh-cn/docs/q...

03-05 16:15