zoukankan      html  css  js  c++  java
  • 基于Consul的配置中心

    上一篇

    pring boot2X整合Consul一服务注册与发现

    将consul作为springboot的配置中心

    1.添加依赖

    <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-consul-config</artifactId>
    </dependency>

    2.启用,配置bootstrap.properties | yml 

    server.port=8010
    spring.application.name=myconsul
    spring.cloud.consul.host=192.168.99.100
    spring.cloud.consul.port=8500
    spring.cloud.consul.discovery.health-check-path=/actuator/health
    spring.cloud.consul.discovery.service-name=${spring.application.name}
    spring.cloud.consul.discovery.heartbeat.enabled=true
    spring.cloud.consul.discovery.prefer-ip-address=true
    spring.cloud.consul.config.enabled=true
    spring.cloud.consul.config.profile-separator=-
    spring.cloud.consul.config.format=properties
    spring.cloud.consul.config.prefix = config
    spring.cloud.consul.config.data-key = data
    # 启用配置自动刷新
    spring.cloud.consul.config.watch.enabled=true
    # 【疑问】请求 consul api 的延迟,单位:秒
    spring.cloud.consul.config.watch.wait-time=1
    # 刷新频率,单位:毫秒
    spring.cloud.consul.config.watch.delay=10000

    3.创建配置文件

    使用@ConfigurationProperties 特性,标记类为配置文件

    复制代码
    package com.xyz.provider;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.stereotype.Component;
    
    @ConfigurationProperties("order")
    @RefreshScope
    @Data
    @Component
    public class OrderProperties {
        private Integer discount = 100;
    }
    复制代码

    4.控制器

    package com.xyz.provider.controller;
    
    import com.xyz.provider.OrderProperties;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class demoController {
        @Autowired
        private OrderProperties orderProperties;
    
        @RequestMapping("/search")
        public String searchDiscount() {
            return orderProperties.toString();
        }
    }

    5.启动类

    package com.xyz.provider;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class ProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class, args);
        }
    
    }

    6.测试

    启动Consul

    启动项目

    在浏览器打开Consul页面  http://192.168.99.100:8500

    添加配置

     测试 GTE http://172.27.0.17:8010/search

    输出

      OrderProperties(discount=60)

    修改配置,再次执行,会发现配置会自动修改

    通过单线程 ThreadPoolTaskScheduler 自动修改配置

  • 相关阅读:
    Shiro自定义密码匹配认证
    logback 发送邮件和自定义发送邮件;java类发送邮件
    webVR全景图多种方案实现(pannellum,aframe,Krpano,three,jquery-vrview)
    前端接受后端文件流并下载的几种方法
    回流(reflow)与重绘(repaint)
    JS数组去重的几种常见方法
    React 生命周期
    浅谈React工作原理
    如何在Vue项目中使用vw实现移动端适配
    移动端web整理 移动端问题总结,移动web遇到的那些坑
  • 原文地址:https://www.cnblogs.com/baby123/p/12769398.html
Copyright © 2011-2022 走看看