SpringCloud(7)
Config
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的动态的配置管理设施是必不可少的。
spring configServer就可以解决这个问题-2023-11-00-38-26.png)
将相同的配置抽调出来放于ConfigServer中去解决。
服务端
使用configServer通常与git,github相结合,通过在git上修改配置文件,从而让每个客户端自己读取到新的配置文件而不用重启。
- 现在GitHub上建立一个新仓库,这个仓库作为以后会使用到的配置文件中心
- 然后是构建服务端(module,pom,yml,主启动类,业务)
 以下是一些相关的包配置yml1 
 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>设置主启动类1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20server: 
 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/eureka1 
 2
 3
 4
 5
 6
 7
 8
 open class Center3344 {
 }
 fun main(args:Array<String>){
 runApplication<Center3344>(*args)
 }
配置读取规则
-2023-11-05-35-40.png)
可以通过http://config-3344.com:3344/master/config-dev.yml来读取到信息
服务端也被称为分布式配置中心,他是一个独立的微服务应用
客户端
5大操作
pom修改:
| 1 | <dependencies> | 
这里不再写application.yml而是使用bootstrap.yml
applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高
Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的Application Context的父上下文。初始化的时候,
Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定
所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。
| 1 | server: | 
主启动类:
| 1 | 
 | 
业务类:
| 1 | 
 | 
动态刷新问题
实时更新gitee上的内容后,需要服务器自动去更新内容,并完成所有客户端的自动更新。
- 修改客户端模块
 -. pom引入actuator监控-. 修改yml,暴露监控端点1 
 2
 3
 4<dependency> 
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>-. 业务类添加1 
 2
 3
 4
 5
 6# 暴露监控端点 
 management:
 endpoints:
 web:
 exposure:
 include: "*"@RefreshScope注解
 -. 然后要依赖于运维业务人员人为的给客户端发送一个刷新的post请求
 必须是POST请求
 curl -X POST “http://localhost:3355/actuator/refresh“
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 小贺同学的blog!
 评论
