关于spring-cloud-alibaba:Spring-Cloud-Alibaba整合Nacos

40次阅读

共计 5299 个字符,预计需要花费 14 分钟才能阅读完成。

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/nacos/releases

启动 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 注解,开启 @LoadBalanced 与 Ribbon 的集成

@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/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md
[2] Nacos Spring Cloud: https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
[3] Nacos: https://nacos.io/zh-cn/docs/quick-start.html

正文完
 0