Spring Cloud应用实现配置自动刷新过程详解
这篇文章主要介绍了SpringCloud应用实现配置自动刷新过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
通过springcloud的消息总线,将配置github等源代码仓库的变更通知到springcloud的所有组件。
spring-bus需要用到rabbitmq,所以需要提前准备rabbitmq消息队列环境
配置中心调整
1.配置中心配置引用pom
org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.cloud spring-cloud-config-monitor
2.配置中心配置
spring: application: name:spring-config cloud: config: server: git: uri:https://github.com/halouprogramer/spring-config-repository.git #username:*** #password:*** basedir:~/temp/gitlab rabbitmq:#增加rabbitmq的配置 host:192.168.114.129 port:5672 username:admin password:admin eureka: client: service-url: defaultZone:http://localhost:8761/eureka/ instance: prefer-ip-address:true server: port:3636 management: endpoints: web: exposure: include:"*"
业务微服务中需要做的修改
1.添加pom
org.springframework.cloud spring-cloud-starter-bus-amqp
2.配置文件
spring:
application:
name:spring-school
cloud:
config:
discovery:
enabled:true
service-id:SPRING-CONFIG
profile:dev
bus:#busid不能使用默认,否则不能刷新
id:${spring.application.name}:${spring.cloud.config.profile}:${random.value}
profiles:
active:dev
rabbitmq:
host:192.168.114.129
port:5672
username:admin
password:admin
eureka:
client:
service-url:
defaultZone:http://localhost:8761/eureka/
instance:
prefer-ip-address:true
#配置超时时间
feign:
client:
config:
default:
connectTimeout:5000
readTimeout:5000
#logger-level:bus
springcloudbus会使用busid去匹配应用,匹配上才会刷新配置
3.编写测试代码
packagecom.lvlvstart.spring.demo.school.service;
importcom.lvlvstart.spring.demo.school.dao.SchoolRepository;
importcom.lvlvstart.spring.demo.school.entity.School;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.cloud.context.config.annotation.RefreshScope;
importorg.springframework.stereotype.Service;
importjava.util.List;
importjava.util.Optional;
/**
*@authorzishu.lv@baodanyun-inc.com
*@description类描述
*@create2019/12/915:53
*/
@Service
@RefreshScope
publicclassSchoolService{
@Value("${env}")
privateStringenv;
@Autowired
privateSchoolRepositoryrepository;
publicListfindAll(){
returnrepository.findAll();
}
publicSchoolfindById(StringchoolId){
Optionalschool=repository.findById(choolId);
if(school.isPresent()){
returnschool.get();
}
returnnull;
}
publicStringgetEnv(){
returnenv;
}
}
packagecom.lvlvstart.spring.demo.school.web;
importcom.lvlvstart.spring.demo.school.service.SchoolService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.cloud.context.config.annotation.RefreshScope;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("config-demo")
publicclassConfigDemoController{
@Autowired
privateSchoolServiceschoolService;
@GetMapping("get-env")
privateStringgetEnv(){
returnschoolService.getEnv();
}
}
@RefreshScope标记上才会被刷新配置
@RefreshScope在Controller层使用,取不到值
利用githbuwebhook自动实现刷新配置:
PayloadURL需要添加configserver开放的monitor(monitor为spring自带地址),如果在内网,可以搜索内网穿透工具配置
修改仓库配置后,访问地址:http://localhost:8081/config-demo/get-env地址也会发生改变
完整代码访问:https://github.com/halouprogramer/spring-cloud-demo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。