Spring Cloud Config 配置中心实现

Config 配置中心

ConfigServer

  1. 导入jar包
		<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>

​ 2. 启动配置中心

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class SpringCloudConfigApplication {

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

}
  1. 配置文件
# 应用名称
spring.application.name=spring-cloud-config
server.port=9091
#设置激活环境
spring.profiles.active=xianning
#设置从本地读取配置信息 1
#spring.profiles.active=native

#模式使用git 需要配置 git 地址 用户名 密码  default-label 不设置默认使用 master
spring.cloud.config.server.git.uri=http://10.0.200.37/cload/talroad-config.git
spring.cloud.config.server.git.username=test
spring.cloud.config.server.git.password=123456
spring.cloud.config.server.git.default-label=${spring.profiles.active}
# 设置缓存默认保存路径
#spring.cloud.config.server.git.basedir=
#设置从本地读取配置信息 2
#spring.cloud.config.server.native.search-locations=file:d:/properties/
#指定路径下 搜索  扫描包,做到文件隔离。  相对路径
#spring.cloud.config.server.git.search-paths=

#需要配置 eureka 否则客户端无法使用
eureka.client.service-url.defaultZoon=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
  1. 相关配置解读
    1. 本地缓存路径
      在这里插入图片描述

通过spring.cloud.config.server.git.basedir=,可以配置默认缓存本地路径。

2. 设置从本地读取配置
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=file:d:/properties/
  1. 设置扫描路径
spring.cloud.config.server.git.search-paths=

ConfigClient

  1. 引入jar包
  		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
2. 修改配置
spring.application.name=air-online-monitor
server.port=8081
spring.profiles.active=kaiping
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.client.service-url.defaultZoon=http://localhost:8761/eureka/

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=spring-cloud-config
spring.cloud.config.profile=${spring.profiles.active}
spring.cloud.config.label=${spring.profiles.active}

注意

  • 因为配置文件有优先级关系,使用注册中心时,需要修改application.properties的名字,改为bootstrap.properties(最高优先级)。

  • 配置中心配置会自动覆盖本地配置

  • 配置读取配置中心某个环境配置时,会自动加载公共配置文件。如本地配置导入环境为kaiping,实际加载会加载air-online-monitor-kaiping.yml和air-online-monitor.yml两个文件。

备注

URL和配置文件关系

a p p l i c a t i o n / {application}/ application/{profile}/${lable}

http://localhost:8091/ l a b l e / {lable}/ lable/{application}-${profile}.yml

  • ${application} 应用名称
  • ${profile} 环境dev/prd/test
  • ${lable} git分支

客户端自动更新

使用actuator

  • 引入jar包
 		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  • 增加配置
# 允许远程访问 端点
management.endpoints.web.exposure.include=*
  • 增加注解
@RefreshScope
@RestController
public class TestController {
	...
}

使用bus

基于RabbitMQ

  1. 服务端增加jar包
 		<!-- 添加监控 -->
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>
		<!-- 添加BUS RabbitMQ -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
  1. 配置文件修改
# 还是需要这个配置
management.endpoints.web.exposure.include=*
  1. 客户端也需要引入jar包
  2. 配置WebHook

在这里插入图片描述

http://2d59c0be2dc4.ngrok.io/monitor?path=*
注意path=* 表示发送至所有节点。否则其他节点不会更新。
如果想针对单个节点进行更新通知可以改为path="节点服务名称"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值