基於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 自動修改配置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM