spring cloud实践(五) config

在项目多了以后,我们需要一个配置中心来统一管理配置文件,spring cloud config应运而生。

spring cloud config

配置文件

在git上新建文件夹repo_configs;新增配置文件

  • fooservice-dev.yml
  • fooservice-pro.yml
  • fooservice-test.yml

内容为:

1
2
hello: 
msg: hello dev/pro/test
spring cloud config server

新建项目config.center

  • 添加依赖
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • 修改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server:
port: 8011
spring:
application:
name: spring-cloud-config-center
cloud:
config:
server:
git:
#uri: git@github.com:lyyljs/eureka.git # 配置git仓库的地址
uri: https://github.com/lyyljs/eureka.git
search-paths: repo_configs # git仓库地址下的相对地址,可以配置多个,用,分割。
username: lyyljs # git 用户名
password: # git 密码 使用https协议时使用
# ignoreLocalSshSettings: true # 是否忽略默认的ssh配置,默认会使用~/.ssh/known_hosts,etc/ssh/ssh_config
# hostKey: id_eclipse_rsa # 使用自定义的 hostKey
# hostKeyAlgorithm: ssh-rsa # 自定义的算法
# privateKey: # 私钥内容
#knownHostsFile: # knownHost文件位置
  • 启动类添加注解 @EnableConfigServer
1
2
3
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterApplication {
  • 启动查看抓取是否成功
    config_server_load_test

  • 路径转换规则

仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

1
2
3
4
5
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
spring cloud config client

修改producer项目

  • 添加依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
  • 添加配置文件

需要添加 bootstrap.yml , spring cloud config 默认会从中读取相关信息。

1
2
3
4
5
6
7
8
9
10
spring:
cloud:
config:
name: fooservice # 参照转换规则 {application}
profile: dev # 参照转换规则 {profile}
uri: http://localhost:8011/ # 配置中心的具体地址
label: master # git分支
discovery:
enabled: false # 是否使用发现服务如eureka来定位config center,默认false
#serviceId: configserver # 定义该服务id,默认configcenter
  • 注解

无需添加额外注解

  • 新增接口以测试
1
2
3
4
5
6
7
@Value("${hello.msg}")
private String helloMsg;

@RequestMapping("/say/hello")
public String sayHello() {
return helloMsg;
}
  • 测试

config_load_test

spring cloud config 高可用

为 spring cloud config server 集群使用注册中心 eureka

spring cloud config server
  • 添加依赖
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 修改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
server:
port: 8011
register:
port1: 8000
port2: 8001
port3: 8002

eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.register.port1}/eureka/
  • 为启动类添加注解 @EnableDiscoveryClient

  • 启动,在eureka控制台查看,可以看到config center已经注册到了eureka

configCenterWithEureka

spring cloud config client
  • 配置文件

修改 bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
spring:
application:
name: spring-cloud-producer
cloud:
config:
name: fooservice # 参照规则 {application}
profile: dev # 参照规则 {profile}
#uri: http://localhost:8011/ # 配置中心的具体地址
label: master # git分支
discovery:
enabled: true # 是否使用发现服务如eureka来定位config center,默认false
serviceId: spring-cloud-config-center # 定义该服务id,默认configcenter

server:
#port: 9000
#port: 8999
port: 8998
register:
port1: 8000
port2: 8001
port3: 8002

eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.register.port1}/eureka/

enruka配置等从 application.yml 移至 bootstrap.yml

  • 启动验证

config_load_test

refresh

在启动producer后,如果更改了配置文件,producer内仍然是启动时的值,现在改造producer让它更改配置文件后刷新。

  • 添加依赖
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 暴露refresh接口

在application.yml中添加

1
2
3
4
5
management:
endpoints:
web:
exposure:
include: refresh
  • 添加需要refresh的类

在 HelloController 上添加注解 @RefreshScope

1
2
3
4
@RestController
@RefreshScope
@RequestMapping("/hello")
public class HelloController {

spring cloud bus

在更新配置后,如果有多个项目,每个项目都得访问一次refresh接口。spring cloud bus 可以让我们从这个重复的劳动中解放出来。

spring cloud bus需要mq的支持,mq需要支持acmq协议或者使用kafka。

这里使用rabbit mq。

对应用进行改造
  • 添加依赖
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 添加rabbit mq配置

在application.yml中添加mq配置

1
2
3
4
5
6
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
对config server改造
  • 添加依赖
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 修改配置文件

暴露bus-refresh接口,添加mq配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
management:
endpoints:
web:
exposure:
include: bus-refresh

spring:
application:
name: spring-cloud-config-center
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
验证

bus_refresh_config


参考链接