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 分布式配置中心

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件

 

接入配置中心

<?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>

新增控制器

应用会从 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;
    }
}

新增注册配置

bootstrap.properties增加以下配置

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控制台

设置相关配置信息

 

 

 

 

 

 

启动项目

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字段,所以不会有刷新纪录。

 

posted @ 2019-11-26 20:17  释迦苦僧  阅读(1293)  评论(0编辑  收藏  举报