Config

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的动态的配置管理设施是必不可少的。

spring configServer就可以解决这个问题

将相同的配置抽调出来放于ConfigServer中去解决。

服务端

使用configServer通常与git,github相结合,通过在git上修改配置文件,从而让每个客户端自己读取到新的配置文件而不用重启。

  1. 现在GitHub上建立一个新仓库,这个仓库作为以后会使用到的配置文件中心
  2. 然后是构建服务端(module,pom,yml,主启动类,业务)
    以下是一些相关的包
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</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>
    </dependencies>
    配置yml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    server:
    port: 3344

    spring:
    application:
    name: cloud-config-center # 注册进Eureka服务器的微服务名
    cloud:
    config:
    server:
    git:
    uri: https://github.com/zfx-t/springcloud-config.git
    search-paths:
    - springcloud-config
    #### 搜索目录
    label: main

    eureka:
    client:
    service-url:
    defaultZone: http://localhost:7001/eureka
    设置主启动类
    1
    2
    3
    4
    5
    6
    7
    8
    @SpringBootApplication
    @EnableConfigServer
    open class Center3344 {

    }
    fun main(args:Array<String>){
    runApplication<Center3344>(*args)
    }

配置读取规则


可以通过http://config-3344.com:3344/master/config-dev.yml来读取到信息
服务端也被称为分布式配置中心,他是一个独立的微服务应用

客户端

5大操作
pom修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependencies>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>

这里不再写application.yml而是使用bootstrap.yml
applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高

Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment
Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap contextApplication Context有着不同的约定
所以新增了一个bootstrap.yml文件,保证Bootstrap ContextApplication Context配置的分离。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称上述3个综合:master分支上config-dev.ymL的配置文件被读http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地k

#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka

主启动类:

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableEurekaClient
open class Client3355 {

}

fun main(args: Array<String>) {
runApplication<Client3355>(*args)
}

业务类:

1
2
3
4
5
6
7
8
9
10
11
@RestController
class ConfigClientController {

@Value("\${config.info}")
private lateinit var configInfo:String

@GetMapping("/configInfo")
fun getConfigInfo(): String {
return configInfo
}
}

动态刷新问题

实时更新gitee上的内容后,需要服务器自动去更新内容,并完成所有客户端的自动更新。

  1. 修改客户端模块
    -. pom引入actuator监控
    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    -. 修改yml,暴露监控端点
    1
    2
    3
    4
    5
    6
    # 暴露监控端点
    management:
    endpoints:
    web:
    exposure:
    include: "*"
    -. 业务类添加@RefreshScope注解
    -. 然后要依赖于运维业务人员人为的给客户端发送一个刷新的post请求
    必须是POST请求
    curl -X POST “http://localhost:3355/actuator/refresh