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

 更新时间:2019年12月31日 15:08:33   作者:指尖,写不尽  
这篇文章主要介绍了Spring Cloud应用实现配置自动刷新过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了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

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

相关文章

  • Spring中属性文件properties的读取与使用详解

    Spring中属性文件properties的读取与使用详解

    这篇文章主要介绍了Spring中属性文件properties的读取与使用详解的相关资料,需要的朋友可以参考下
    2017-02-02
  • Java Enum和String及int的相互转化示例

    Java Enum和String及int的相互转化示例

    这篇文章主要介绍了Java Enum和String及int的相互转化示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Scala实现冒泡排序、归并排序和快速排序的示例代码

    Scala实现冒泡排序、归并排序和快速排序的示例代码

    这篇文章主要介绍了Scala实现冒泡排序、归并排序和快速排序的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-06-06
  • SpringBoot整合MyBatis Plus实现基本CRUD与高级功能

    SpringBoot整合MyBatis Plus实现基本CRUD与高级功能

    Spring Boot是一款用于快速构建Spring应用程序的框架,而MyBatis Plus是MyBatis的增强工具,本文将详细介绍如何在Spring Boot项目中整合MyBatis Plus,并展示其基本CRUD功能以及高级功能的实现方式,需要的朋友可以参考下
    2024-02-02
  • 新手了解java 类,对象以及封装基础知识

    新手了解java 类,对象以及封装基础知识

    JS是一门面向对象语言,其对象是用prototype属性来模拟的,本文介绍了如何封装JS对象,具有一定的参考价值,下面跟着小编一起来看下吧,希望对你有所帮助
    2021-07-07
  • Spring boot实现一个简单的ioc(1)

    Spring boot实现一个简单的ioc(1)

    这篇文章主要为大家详细介绍了Spring boot实现一个简单的ioc,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • 异常点/离群点检测算法——LOF解析

    异常点/离群点检测算法——LOF解析

    这篇文章主要介绍了异常点/离群点检测算法——LOF解析,通过图解文字描述的方式详细的解析了该算法,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • spring boot 常见http请求url参数获取方法

    spring boot 常见http请求url参数获取方法

    这篇文章主要介绍了spring boot 常见http请求url参数获取,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • JAVA超级简单的爬虫实例讲解

    JAVA超级简单的爬虫实例讲解

    下面小编就为大家带来一篇JAVA超级简单的爬虫实例讲解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • JavaWeb如何实现禁用浏览器缓存

    JavaWeb如何实现禁用浏览器缓存

    这篇文章主要介绍了JavaWeb如何实现禁用浏览器缓存,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02

最新评论