spring_boot_config实践

简介

官网: https://docs.spring.io/spring-cloud-config/docs/current/reference/html/

springcloud config 项目是一个解决分布式系统的配置管理方案。

服务多、环境多、改配置不停服。

spring_cloud_config业务架构图

springcloud config 分为服务端和客户端,服务端负责将本地 git 或者 svn 中存储的配置文件发布成 REST 风格的接口,客户端可以从服务端 REST 接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置,这需要每个客户端通过 POST 方法触发各自的 /refresh 接口。

搭建

匹配规则

1
2
3
4
5
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
1
2
3
4
5
6
curl localhost:8888/foo/development
curl localhost:8888/foo/development/master
curl localhost:8888/foo/development,db/master
curl localhost:8888/foo-development.yml
curl localhost:8888/foo-db.properties
curl localhost:8888/master/foo-db.properties

application 服务名称
profile 环境名称,开发、测试、生产:dev qa prd
lable 仓库分支、默认master分支

匹配原则:从前缀开始。

创建git仓库

创建仓库config_center

创建配置文件 eureka-dev.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring:
application:
name: eureka

server:
port: 8761

eureka:
instance:
hostname: localhost
client:
#设置服务注册中心的URL
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

management:
endpoints:
web:
exposure:
include: '*'

创建配置文件 eureka-test.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring:
application:
name: eureka

server:
port: 8762

eureka:
instance:
hostname: localhost
client:
#设置服务注册中心的URL
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

management:
endpoints:
web:
exposure:
include: '*'

创建config-server

1
2
3
4
5
6
7
8
9
<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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server:
port: 9001
# 应用名称
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/doufengle/config_center.git
username: xxxx
password: xxxx
eureka:
client:
# 刷新注册表的间隔时间
registry-fetch-interval-seconds: 5
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
# 心跳续约间隔
lease-renewal-interval-in-seconds: 10
# 告诉server在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认90秒
lease-expiration-duration-in-seconds: 10
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}

}

测试接口:

http://localhost:9001/eureka-dev.yml

http://localhost:9001/eureka-test.yml

创建config-client(固定地址)

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

修改 application配置文件为bootstrap配置文件

1
2
3
4
5
6
7
8
9
10
spring:
application:
name: eureka
profiles:
active: dev
cloud:
config:
uri: http://localhost:9001/
profile: ${spring.profiles.active}
label: master

这时启动着 config-server、 再启动eureka-server。

发现启动端口为8761。

更改 active: dev 为 test时启动端口为8762。

创建config-client (eureka)

创建service-b-dev.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 应用名称
spring:
#zipkin
zipkin:
base-url: http://localhost:9411/
#采样比例1
sleuth:
sampler:
rate: 1

server:
port: 8763

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

logging:
level:
root: DEBUG
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

Service-b 项目修改 application配置文件为bootstrap配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
# 应用名称
spring:
application:
name: service-b
profiles:
active: dev
cloud:
config:
profile: ${spring.profiles.active}
label: master
discovery:
service-id: config-server
enabled: on

启动service-b服务。

刷新

手动刷新

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

service-b-dev.yml 添加 开启actuator中的refresh端点

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
# 应用名称
spring:
#zipkin
zipkin:
base-url: http://localhost:9411/
#采样比例1
sleuth:
sampler:
rate: 1

server:
port: 8763

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

logging:
level:
root: DEBUG

management:
endpoints:
web:
exposure:
include: '*'

Controller中添加@RefreshScope注解

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
29
@RequestMapping("/test")
@RestController
@RefreshScope
public class TestController {

@Value("${server.port}")
String port;

@Autowired
private ServiceaClient serviceaClient;

@RequestMapping("/test1")
public String test() throws InterruptedException {
return "i am service b from port:" + port;
}


@RequestMapping("/test3")
public String test3() {
return "i am service b from port:" + port;
}


@RequestMapping("/test2")
public String test2() {
return "调用:" + serviceaClient.test();
}

}

调用接口 http://localhost:8763/test/test1

显示 8763端口

修改 git仓库中的端口 8764。

刷新接口调用:http://localhost:8764/actuator/refresh post

http://localhost:8763/test/test1

显示8764端口。

自动刷新

erlang安装

http://www.erlang.org/downloads

RabbitMQ安装

https://www.rabbitmq.com/install-generic-unix.html

mac 安装

安装

1
brew install rabbitmq

启动

1
brew services start rabbitmq;

管理界面

1
rabbitmq-plugins enable rabbitmq_management

相关端口

端口 说明
5672 RabbitMQ的通讯端口
25672 RabbitMQ的节点间的CLI通讯端口
15672 RabbitMQ HTTP_API的端口,管理员用户才能访问,用于管理RabbitMQ,需要启动Management插件
1883,8883 MQTT插件启动时的端口
61613、61614 STOMP客户端插件启用的时候的端口
15674、15675 基于webscoket的STOMP端口和MOTT端口

1
2
3
4
5
6
7
8
9
10
11
12
13
#新建用户
rabbitmqctl add_user 账号 密码
#给用户分配操作权限
rabbitmqctl set_user_tags 账号 administrator
#修改密码
rabbitmqctl change_password Username Newpassword 修改密码
#删除用户
rabbitmqctl delete_user Username 删除用户
#查看所有用户
rabbitmqctl list_users 查看用户清单
#为用户设置访问权限
rabbitmqctl set_permissions -p / 用户名 ".*" ".*" ".*"
rabbitmqctl set_permissions -p / root ".*" ".*" ".*"

用户权限

角色 权限
administrator 可以登录控制台、查看所有信息、可以对rabbitmq进行管理
monitoring 监控者,登录控制台,查看所有信息
policymaker 策略制定者,登录控制台,指定策略
managment 普通管理员,登录控制台

配置

Config-server

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
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
29
30
31
32
33
34
35
36
server:
port: 9001
# 应用名称
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/doufengle/config_center.git
username: xxxx
password: xxxx
# rabbitmq 地址配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
# 刷新注册表的间隔时间
registry-fetch-interval-seconds: 5
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
# 心跳续约间隔
lease-renewal-interval-in-seconds: 10
# 告诉server在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认90秒
lease-expiration-duration-in-seconds: 10

management:
endpoints:
web:
exposure:
include: "*"

启动config-server服务。

访问http://localhost:15672/#/queues ,mq会有一条临时队列。

客户端配置:

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

bootstrapyaml

1
2
3
4
5
6
7
8
9
10
11
12
13
# 应用名称
spring:
application:
name: service-b
profiles:
active: dev
cloud:
config:
profile: ${spring.profiles.active}
label: master
discovery:
service-id: config-server
enabled: on

service-b-dev.yml 添加rabbitmq

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
29
30
31
32
# 应用名称
spring:
#zipkin
zipkin:
base-url: http://localhost:9411/
#采样比例1
sleuth:
sampler:
rate: 1
# rabbitmq 地址配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
server:
port: 8763

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

logging:
level:
root: DEBUG

management:
endpoints:
web:
exposure:
include: '*'
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
29
@RequestMapping("/test")
@RestController
@RefreshScope
public class TestController {

@Value("${server.port}")
String port;

@Autowired
private ServiceaClient serviceaClient;

@RequestMapping("/test1")
public String test() throws InterruptedException {
return "i am service b from port:" + port;
}


@RequestMapping("/test3")
public String test3() {
return "i am service b from port:" + port;
}


@RequestMapping("/test2")
public String test2() {
return "调用:" + serviceaClient.test();
}

}

webhooks实现自动刷新配置

在git 的webhooks配置自动刷新的地址。(配置config-server的http://localhost:9001/actuator/bus-refresh)

单个服务通知

发送 post请求 http://localhost:8763/actuator/bus-refresh 至单个服务的端点。


spring_boot_config实践
http://hanqichuan.com/2022/06/30/spring_cloud/spring_cloud_config实践/
作者
韩启川
发布于
2022年6月30日
许可协议