编程知识 cdmana.com

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

闲话少谈,直奔主题

一、我的研发环境

springboot:2.1.8.RELEASE

spring.cloud:Greenwich.SR5

二、POM文件展示

<?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>

三、bootstrap.yaml文件的编写。(经过验证,配置中心默认加载bootstrap.(yml|yaml|properties)配置文件,完成配置中心功能的初始化)

# 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}

四、consul的系统参数配置

我的测试地址:http://192.168.10.118:8500/ui/rdc/kv/config/app-exchange-api/common/edit

 五、配置完成,系统启动成功读取配置......

版权声明
本文为[Mr.Simm]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/MrSi/p/13961890.html

Scroll to Top