分布式配置中心———在git端修改配置文件后,让客户端同时生效

目录结构:
在这里插入图片描述
pom中加入依赖:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	    <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
	    	 <groupId>org.springframework.boot</groupId>
	   		 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置文件中的代码:

spring.application.name=config-client
server.port=9031
eureka.client.serviceUrl.defaultZone=http://user:test@eureka2:8762/eureka/,http://user:test@eureka1:8761/eureka/
eureka.instance.perferIpAddress=true
#默认false,这里设置true,表示开启读取配置中心的配置
spring.cloud.config.discovery.enabled=true
#对应eureka中的配置中心serviceId,默认是configserver
spring.cloud.config.discovery.serviceId=config-server
#指定环境
spring.cloud.config.profile=dev
#git标签
spring.cloud.config.label=master
#springboot 默认开启了权限拦截 会导致 /refresh出现401,拒绝访问
management.security.enabled=false

ConfigClientApplication代码:

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
	public static void main(String[] args) { 
		SpringApplication.run(ConfigClientApplication.class, args);
	}    
}

TestController代码:

@RestController
@RefreshScope   	//刷新bean的作用域
public class TestController {
	@Value("${book.config}")
	private String msg;
	@RequestMapping("/test")
	public String test() {
		return this.msg;
	}
}

然后启动服务端和刚刚的项目客户端。服务端就是我的上一篇博客的项目分布式配置中心
查看:
在这里插入图片描述
然后我们在码云上面修改test中的值:
在这里插入图片描述
再利用post对该地址请求一次:
在这里插入图片描述
然后再查看:
在这里插入图片描述
这样在git端修改配置文件后,就可以让客户端同时生效。
如果大家有什么需要交流或者需要demo的可以加我的qq997355706.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值