Alibaba Nacos 學習(二):Spring Cloud Nacos Config

Alibaba Nacos 學習(一):Nacos介紹與安裝

Alibaba Nacos 學習(二):Spring Cloud Nacos Config

Alibaba Nacos 學習(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服務註冊與發現

Alibaba Nacos 學習(四):Nacos Docker

Alibaba Nacos 學習(五):K8S Nacos搭建,使用nfs

Nacos 分佈式配置中心

在分佈式系統中,因爲服務數量巨多,爲了方便服務配置文件統一管理,實時更新,因此須要分佈式配置中心組件html

 

接入配置中心

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>nacos.server</groupId>
    <artifactId>nacos.server</artifactId>
    <name>nacos.server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.0.3.RELEASE</spring-boot.version>
        <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>0.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!-- swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
    </dependencies>
</project>

新增控制器java

應用會從 Nacos Config 中獲取相應的配置,並添加在 Spring Environment 的 PropertySources 中。這裏咱們使用 @Value 註解來將對應的配置注入到 ConfigController 中的字段,並添加 @RefreshScope 打開動態刷新功能
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;
    @Value("${user.name}")
    private String userName;
    @Value("${user.age}")
    private Integer userAge;

    /**
     * http://localhost:8080/config/get
     */
    @RequestMapping("/get")
    @ResponseBody
    public String get() {
        return "useLocalCache:" + useLocalCache + " userName:" + userName + " userAge:" + userAge;
    }
}

新增註冊配置web

bootstrap.properties增長如下配置spring

spring.cloud.nacos.config.server-addr=192.168.50.31:8848 --配置地址
spring.application.name=service-provider  --dataid
spring.profiles.active=test   --運行環境
server.port=8080
 

登陸Nacos控制檯

設置相關配置信息docker

 

 

 

 

 

 

啓動項目

2019-11-25 15:05:14.025  INFO 62860 --- [           main] o.s.c.a.n.c.NacosPropertySourceBuilder   : Loading nacos data, dataId: 'service-provider-test.properties', group: 'DEFAULT_GROUP'
2019-11-25 15:05:14.025  INFO 62860 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='service-provider-test.properties'}, NacosPropertySource {name='service-provider.properties'}]}
2019-11-25 15:05:14.029  INFO 62860 --- [           main] e.d.example.dockertest.Application       : The following profiles are active: test
2019-11-25 15:05:14.043  INFO 62860 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@22c86919: startup date [Mon Nov 25 15:05:14 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6aa61224
2019-11-25 15:05:14.699  INFO 62860 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=0fd1ef74-7ad3-37d4-b602-1ed486ff9aa7
Loading nacos data, dataId: 'service-provider-test.properties'
在Nacos-Server中新建配置,其中Data ID它的定義規則是: ${prefix}-${spring.profile.active}.${file-extension}
  • prefix 默認爲 spring.application.name 的值,也能夠經過配置項 spring.cloud.nacos.config.prefix 來配置。
  • spring.profile.active 即爲當前環境對應的 profile,能夠經過配置項 spring.profile.active 來配置。
  • file-exetension 爲配置內容的數據格式,能夠經過配置項 spring.cloud.nacos.config.file-extension 來配置。

測試內容:

 

修改內容

 

2019-11-25 15:19:24.525  INFO 62860 --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4275635b: startup date [Mon Nov 25 15:19:24 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77
2019-11-25 15:19:24.556  INFO 62860 --- [.168.50.31_8848] o.s.boot.SpringApplication               : Started application in 2.611 seconds (JVM running for 855.251)
2019-11-25 15:19:24.556  INFO 62860 --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4275635b: startup date [Mon Nov 25 15:19:24 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77
2019-11-25 15:19:24.556  INFO 62860 --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77: startup date [Mon Nov 25 15:19:23 CST 2019]; root of context hierarchy
2019-11-25 15:19:24.562  INFO 62860 --- [.168.50.31_8848] o.s.c.e.event.RefreshEventListener       : Refresh keys changed: [user.age, user.name]
Refresh keys changed: [user.age, user.name],沒有修改useLocalCache字段,因此不會有刷新紀錄。
相關文章
相關標籤/搜索