Config配置中心介绍
通过全局的配置中心,来统一管理零散的微服务,使配置一处修改处处生效
配置中心分为:服务端和客户端架构如下。
系统级配置和用户级配置
创建bootstrap.yml为系统级配置
创建application.yml为用户级别配置
系统级别的配置优先级更高
配置流程
1.在github上创建仓库–例如springcloud-config
2.在仓库添加yml文件,例如config-dev.yml这里的命名规范必须加-号
3.创建服务端配置
4.在客户端配置
服务器端配置
pom配置
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
<scope>test</scope>
</dependency>
</dependencies>
yml配置
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: git@github.com:paulfuke/springcloud-config.git # github的地址
search-paths:
- springcloud-config # github仓库名称
label: master #分支
# 访问方式见文档
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigMain3344.class,args);
}
}
核心代码
暂无
url访问方式(5种)
推荐第三种
域名:端口号/github分支名/应用名/文件.yml
例如范文springcloud-config仓库的master分支下的config-dev.yml就是localhost:端口号/master/config-dev.yml
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
客户端配置
pom配置
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
<scope>test</scope>
</dependency>
</dependencies>
yml配置
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master #分支名
name: config #application名称
profile: dev #profile名称 综合就是根据url进行访问github上的master/config-dev.yml
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
核心代码
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
public String configInfo;
@GetMapping("/info")
public String configInfo(){
return configInfo;
}
}
config动态刷新
注意:当github上的配置改变的时候服务端当下次请求时会立刻发生变化,但是客户端不会变化,客户端需要重启服务才能改变,如何做到动态刷新呢?
1.在yml中加入
management:
endpoints:
web:
exposure:
include: "*"
2.在controller上加入
@RefreshScope
3.发送post请求告诉客户端刷新一下
http://localhost:3355/actuator/refresh
存在的问题
此时动态刷新需要一条条的执行,如何方便快捷的进行消息发送,需要用到BUS消息总线