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.</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..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..RELEASE</spring-boot.version>
<nacos-config-spring-boot.version>0.2.</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..RELEASE</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.</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=
 

登录Nacos控制台

设置相关配置信息

Alibaba Nacos 学习(二):Spring Cloud Nacos Config-LMLPHP

Alibaba Nacos 学习(二):Spring Cloud Nacos Config-LMLPHP

启动项目

-- ::14.025  INFO  --- [           main] o.s.c.a.n.c.NacosPropertySourceBuilder   : Loading nacos data, dataId: 'service-provider-test.properties', group: 'DEFAULT_GROUP'
-- ::14.025 INFO --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='service-provider-test.properties'}, NacosPropertySource {name='service-provider.properties'}]}
-- ::14.029 INFO --- [ main] e.d.example.dockertest.Application : The following profiles are active: test
-- ::14.043 INFO --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@22c86919: startup date [Mon Nov :: CST ]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6aa61224
-- ::14.699 INFO --- [ 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 来配置。

测试内容:

Alibaba Nacos 学习(二):Spring Cloud Nacos Config-LMLPHP

修改内容

-- ::24.525  INFO  --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4275635b: startup date [Mon Nov  :: CST ]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77
-- ::24.556 INFO --- [.168.50.31_8848] o.s.boot.SpringApplication : Started application in 2.611 seconds (JVM running for 855.251)
-- ::24.556 INFO --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4275635b: startup date [Mon Nov :: CST ]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77
-- ::24.556 INFO --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77: startup date [Mon Nov :: CST ]; root of context hierarchy
-- ::24.562 INFO --- [.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字段,所以不会有刷新纪录。
05-12 00:41