Table of Contents
1.Config 介绍
https://spring.io/projects/spring-cloud-config#overview
https://github.com/akeung/springclouddemo-config
Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以集中管理所有环境中应用程序的外部属性。客户端和服务器上的概念与Spring Environment
和PropertySource
抽象,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。当应用程序从开发人员到测试人员进入生产过程进入生产过程时,您可以管理这些环境之间的配置,并确保应用程序具有它们迁移时所需的一切。服务器存储后端的默认实现使用git,因此它轻松支持配置环境的带标签的版本,并且可用于管理内容的各种工具。添加替代实现并将其插入Spring配置很容易。
2.github 上存放配置文件
https://github.com/akeung/springclouddemo-config
3.服务端构建
config-center-3344
1.pom.xml中添加依赖
<dependencies>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.ak.demo</groupId>
<artifactId>api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2.代码实现
yml文件配置
server:
port: 3344
spring:
application:
name: config-center
cloud:
config:
server:
git:
uri: https://github.com/akeung/springclouddemo-config
search-paths:
- springclouddemo-config #搜索目录
label: master #读取分支
eureka:
client:
register-with-eureka: true #表示向注册中心注册自己 默认为true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
主启动类
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigCenter3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenter3344.class,args);
}
}
3.访问规则
https://docs.spring.io/spring-cloud-config/docs/2.2.4.RELEASE/reference/html/
访问规则:通过这套规则在浏览器上直接访问
#http://localhost:3344/config/dev/master
/{application}/{profile}[/{label}]
#http://localhost:3344/config-dev.yml
/{application}-{profile}.yml
#http://localhost:3344/master/config-dev.yml
/{label}/{application}-{profile}.yml
#http://localhost:3344/config-dev.properties
/{application}-{profile}.properties
#http://localhost:3344/master/config-dev.properties
/{label}/{application}-{profile}.properties
4.客户端构建
config-client-3370
1.pom.xml中添加依赖
<dependencies>
<!--config-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.ak.demo</groupId>
<artifactId>api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2.代码实现
bootstrap..yml文件配置
applicaiton.yml 是用户级的资源配置项
bootstrap.yml 是系统级的,优先级更加高
Spring Cloud会创建一个"Bootstrap Context" ,作为Spring应用的Application Context的父上下文。初始化的时候,`BootstrapContext负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的Environment.
Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离
要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的。
因为bootstrap.yml是比application.yml先加载的. bootstrap.yml优先级高于application.ym
server:
port: 3370
spring:
application:
name: config-client
cloud:
config: #根据访问配置中心的访问规则配置
label: master #读取分支
name: config #配置文件名称
profile: dev #后缀名称
uri: http://localhost:3344 #配置中心地址
eureka:
client:
register-with-eureka: true #表示向注册中心注册自己 默认为true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
主启动类
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ConfigClient3370 {
public static void main(String[] args) {
SpringApplication.run(ConfigClient3370.class,args);
}
}
测试
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("config/info")
public String getConfigInfo() {
return configInfo;
}
}
3.动态刷新配置现
当github上更新配置后,我们的服务要重新启动才能使用新的配置,否则配置无法生效。故需要 动态的修改配置。
1.必须依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.修改yml,暴露监控端口
#暴露监控端点
management:
endpoints:
web:
exposure:
include: refresh
# include: "*"
3.@RefreshScope修饰控制类
4.POST 请求执行刷新
curl -X POST "http://localhost:3370/actuator/refresh"