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

所属分类: 软件编程 / java 阅读数: 68
收藏 0 赞 0 分享

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

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

spring-bus 需要用到rabbitmq ,所以需要提前准备rabbitmq消息队列环境

配置中心调整

1.配置中心配置引用pom

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-monitor</artifactId>
</dependency>

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

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

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<School> findAll(){
    return repository.findAll();
  }

  public School findById(String choolId){
    Optional<School> 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java实现一个简单的文件上传案例示例代码

这篇文章主要介绍了Java实现一个简单的文件上传案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在spring中使用JdbcTemplate操作数据库的几种方式

这篇文章主要介绍了详解在spring中使用JdbcTemplate操作数据库的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java中的switch case语句使用详解

这篇文章主要介绍了java中的switch case语句使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

深入理解Java 线程池

这篇文章主要介绍了Java 线程池的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解mybatis #{}和${}的区别、传参、基本语法

这篇文章主要介绍了mybatis #{}和${}的区别、传参、基本语法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

这篇文章主要介绍了MyBatis 中 ${}和 #{}的正确使用方法,本文给大家提到了MyBatis 中 ${}和 #{}的区别,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Mybatis中的 ${} 和 #{}区别与用法

这篇文章主要介绍了Mybatis中的 ${} 和 #{}区别与用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Maven安装过程图文详解

这篇文章主要介绍了Maven安装过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

SpringBoot登录拦截配置详解(实测可用)

这篇文章主要介绍了SpringBoot登录拦截配置详解(实测可用),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA 内存溢出案例汇总

这篇文章主要介绍了JAVA 内存溢出案例的汇总,文中讲解非常细致,帮助各位工作学习时避免内存溢出,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多