编程知识 cdmana.com

Springboot integrated consult service access configuration center

Talk less , Go straight to the theme

One 、 My R & D environment

springboot:2.1.8.RELEASE

spring.cloud:Greenwich.SR5

Two 、POM Document display

<?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>
        <!--  introduce  Spring Cloud Consul Discovery  Related dependencies , take  Consul  As a registry , And realize its automatic configuration  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
     <!--  introduce  Spring Cloud Consul Config  Related dependencies , take  Consul  As configuration center  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
        <!--  introduce  Spring Boot Actuator  Components , Because it needs to provide health check interface to  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、 ... and 、bootstrap.yaml Compilation of documents .( After verification , The configuration center is loaded by default bootstrap.(yml|yaml|properties) The configuration file , Complete the initialization of configuration center function )

# consul Configure the boot environment for 
consul:
  host: 192.168.10.118 #  Registered address ( need )
  port: 8501 # Consul  port ( need )
  acl-token: 495af318-0822-b701-ee8d-80dc6d4d863a #  visit token ( need )
spring:
  #consul To configure 
  cloud:
    # Spring Cloud Consul  General configuration item , Corresponding  ConsulProperties  class 
    consul:
      host: ${consul.host} #  Registered address 
      port: ${consul.port} # Consul  port 
      # consul Configuration center 
      config:
        prefix: config
        enabled: true
        format: YAML
        data-key: common
        acl-token: ${consul.acl-token}
      # Spring Cloud Consul Discovery  Configuration item , Corresponding  ConsulDiscoveryProperties  class 
      discovery:
        enabled: true
        # register: true #  Register yourself to consul
        deregister: true #  Unregister when service is stopped 
        service-name: ${spring.application.name} #  Sign up to  Consul  Service name of , The default is  `spring.application.name`  Configuration item 
        health-check-path: /actuator/health #  Health check interface , The default is  /actuator/health, from  Spring Boot Actuator  Provide 
        acl-token: ${consul.acl-token}

Four 、consul System parameter configuration of

My test address :http://192.168.10.118:8500/ui/rdc/kv/config/app-exchange-api/common/edit

  5、 ... and 、 Configuration complete , The system started successfully and read the configuration ......

版权声明
本文为[Mr.Simm]所创,转载请带上原文链接,感谢

Scroll to Top