SpringBoot集成Consul服務接入配置中心

閒話少談,直奔主題java

1、個人研發環境spring

springboot:2.1.8.RELEASEapache

spring.cloud:Greenwich.SR5bootstrap

2、POM文件展現api

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
   ....
   ....
   ....
    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>2.1.8.RELEASE</spring.boot.version>
        <spring.cloud.version>Greenwich.SR5</spring.cloud.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- 引入 Spring Cloud Consul Discovery 相關依賴,將 Consul 做爲註冊中心,並實現對其的自動配置 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
     <!-- 引入 Spring Cloud Consul Config 相關依賴,將 Consul 做爲配置中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
        <!-- 引入 Spring Boot Actuator 組件,由於須要經過它提供健康檢查的接口給 Consul -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、bootstrap.yaml文件的編寫。(通過驗證,配置中心默認加載bootstrap.(yml|yaml|properties)配置文件,完成配置中心功能的初始化)springboot

# consul的配置啓動環境
consul:
  host: 192.168.10.118 # 註冊地址( need )
  port: 8501 # Consul 端口( need )
  acl-token: 495af318-0822-b701-ee8d-80dc6d4d863a # 訪問token ( need )
spring:
  #consul配置
  cloud:
    # Spring Cloud Consul 通用配置項,對應 ConsulProperties 類
    consul:
      host: ${consul.host} # 註冊地址
      port: ${consul.port} # Consul 端口
      # consul配置中心
      config:
        prefix: config
        enabled: true
        format: YAML
        data-key: common
        acl-token: ${consul.acl-token}
      # Spring Cloud Consul Discovery 配置項,對應 ConsulDiscoveryProperties 類
      discovery:
        enabled: true
        # register: true # 註冊自身到consul
        deregister: true # 服務中止時取消註冊
        service-name: ${spring.application.name} # 註冊到 Consul 的服務名,默認爲 `spring.application.name` 配置項
        health-check-path: /actuator/health # 健康檢查的接口,默認爲 /actuator/health,由 Spring Boot Actuator 提供
        acl-token: ${consul.acl-token}

4、consul的系統參數配置app

個人測試地址:http://192.168.10.118:8500/ui/rdc/kv/config/app-exchange-api/common/editmaven

 5、配置完成,系統啓動成功讀取配置......spring-boot

相關文章
相關標籤/搜索