Spring Cloud Alibaba整合Nacos

Nacos是阿里巴巴開發的用於發現、配置和管理微服務。與Spring Cloud Alibaba完美結合。Nacos 提供了一組簡單易用的特性集,幫助您快速實現動態服務發現、服務配置、服務元數據及流量管理。Nacos是國產的,因此中文文檔也很豐富。html

啓動配置管理

引入依賴

<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 元數據java

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

須要先啓動Nacos,不然啓動後會有些報錯git

完成上述兩步後,應用會從 Nacos Config 中獲取相應的配置,並添加在 Spring Environment 的 PropertySources 中。這裏咱們使用 @Value 註解來將對應的配置注入到 SampleController 的 userName 和 age 字段,並添加 @RefreshScope 打開動態刷新功能github

@RefreshScope
 class SampleController {

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

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

啓動 Nacos Server 並添加配置

下載Nacos

GitHub地址:https://github.com/alibaba/nacos/releasesweb

啓動 Server

進去其相對文件夾 nacos/bin,並對照操做系統實際狀況執行以下命令。詳情參考官方文檔spring

  • Linux/Unix/Mac 操做系統,執行命令 sh startup.sh -m standalone
  • Windows 操做系統,執行命令 cmd startup.cmd

在命令行執行以下命令,向 Nacos Server 中添加一條配置。shell

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 開啓服務註冊發現功能bootstrap

// 開啓服務註冊發現功能
@SpringBootApplication
@EnableDiscoveryClient
public class NacosDiscoveryProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosDiscoveryProviderApplication.class, args);
    }
}

配置

application.properties 中配置 Nacos server 的地址app

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 的集成ide

@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

相關文章
相關標籤/搜索