zoukankan      html  css  js  c++  java
  • 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

    请关于一下啦^_^

    微信公众号

  • 相关阅读:
    python json模块(15)
    python random模块(14)
    python time模块(13)
    python sys模块(12)
    python zip函数(11)
    python递归函数(10)
    python 浅拷贝和深拷贝(9)
    python is 和 == 区别(8)
    python 可变数据类型和不可变数据类型(7)
    python局部变量和全局变量(6)
  • 原文地址:https://www.cnblogs.com/haloujava/p/12119022.html
Copyright © 2011-2022 走看看