Config

PPG007 ... 2021-12-28 About 2 min

# Config

# 相关依赖

<!-- 服务端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 2020版本SpringCloud读取bootstrap.yml -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 不使用 Eureka 做配置中心

# 服务端配置

server:
    port: 8848
spring:
    cloud:
        config:
            server:
                git:
                    password: 06050704zxl
                    uri: https://gitee.com/pidehen2/spring-cloud-config-learn.git
                    username: 1658272229@qq.com
    application:
        name: config-server
1
2
3
4
5
6
7
8
9
10
11
12

在主启动类上添加 @EnableConfigServer 注解开启服务。

# 查看仓库中配置文件内容的可选路径

查看路径

application 是 yaml 中定义的 spring.application.name 的值,profile 是 yaml 中定义的 spring.config.active.on-profile,label 为分支名。

# 客户端配置

application.yml (用户级配置,优先级低于 bootstrap.yml):

spring:
  application:
    name: config-client
1
2
3

bootstrap.yml (系统级配置,优先级高于 application.yml 低于 git 仓库):

spring:
  cloud:
    config:
      uri: http://localhost:8848 #Config服务地址
      label: master #分支
      name: application #git仓库中文件名
      profile: dev #选择环境
server:
  port: 90 # 端口号,若git中没有配置端口号,这里才会生效
1
2
3
4
5
6
7
8
9

编写 Controller 测试:

@RestController
@RefreshScope
public class TestController {

    @Value("${teacher.name}")
    private String name;

    @RequestMapping("/test")
    public String test(){
        return name;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 使用 Eureka

# 服务端配置

server:
    port: 8848
spring:
    cloud:
        config:
            server:
                git:
                    password: 06050704zxl
                    uri: https://gitee.com/pidehen2/spring-cloud-config-learn.git
                    username: 1658272229@qq.com
    application:
        name: config-server
eureka:
    client:
        service-url:
            defaultZone: http://localhost:7001/eureka/,http://localhost2:7002/eureka/
    instance:
        instance-id: config-server
        prefer-ip-address: true
info:
    app.name: config-server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 客户端配置

bootstrap.yml:

spring:
  cloud:
    config:
      label: master
      discovery:
        enabled: true
        service-id: config-server
  application:
    name: application
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost2:7002/eureka/
  instance:
    instance-id: config-client
    prefer-ip-address: true
info:
  app.name: config-client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 手动刷新

# 添加依赖

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

# 修改 bootstrap.yml

management:
  endpoints:
    web:
      exposure:
        include: "*" # 类似Hystrix,将刷新接口暴露
1
2
3
4
5

# 修改要刷新的类

在需要刷新的类上使用 @RefreshScope 注解。

# 刷新

修改 Git 中的配置文件后,向 [IP]:[port]/actuator/refresh 发送 POST 请求,即可实现手动刷新。

Last update: August 15, 2022 09:32
Contributors: Koston Zhuang , PPG007