Spring Cloud应用实现配置自动刷新过程详解

网友投稿 288 2022-12-17


Spring Cloud应用实现配置自动刷新过程详解

这篇文章主要介绍了Spring Cloud应用实现配置自动刷新过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

通过spring cloud 的消息总线,将配置github 等源代码仓库的变更通知到spring cloud 的所有组件。

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: #bus id 不能使用默认,否则不能刷新

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

spring cloud bus 会使用 bus id 去匹配应用,匹配上才会刷新配置

3.编写测试代码

package com.lvlvstart.spring.demo.school.service;

import com.lvlvstart.spring.demo.school.dao.SchoolRepository;

import com.lvlvstart.spring.demo.school.entity.School;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Optional;

/**

* @author zishu.lv@baodanyun-inc.com

* @description 类描述

* @create 2019/12/9 15:53

*/

@Service

@RefreshScope

public class SchoolService {

@Value("${env}")

private String env;

@Autowired

private SchoolRepository repository;

public List findAll(){

retNxvPOifVOurn repository.findAll();

}

public School findById(String choolId){

Optional school = repository.findById(choolId);

if(school.isPresent()){

return school.get();

}

return null;

}

public String getEnv(){

return env;

}

}

package com.lvlvstart.spring.demo.school.web;

import com.lvlvstart.spring.demo.school.service.SchoolService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("config-demo")

public class ConfigDemoController {

@Autowired

private SchoolService schoolService;

@GetMapping("get-env")

private String getEnv(){

return schoolService.getEnv();

}

}

@RefreshScope 标记上才会被刷新配置

@RefreshScope 在Controller层使用,取不到值

利用githbu webhook 自动实现刷新配置:

Payload URL 需要添加config server 开放的monitor(monitor 为spring 自带地址) ,如果在内网,可以搜索内网穿透工具配置

修改仓库配置后,访问地址:http://localhost:8081/config-demo/get-env 地址也会发生改变

完整代码访问:https://github.com/halouprogramer/spring-cloud-demo


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Jmeter调用java脚本过程详解
下一篇:Spring Cloud基于zuul实现网关过程解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~