美文网首页
Springboot启用Consul作为配置中心

Springboot启用Consul作为配置中心

作者: Airness | 来源:发表于2023-11-13 11:58 被阅读0次

背景

已经启用Consul作为服务发现一段时间,Consul中提供了Key-Value存储服务,并且可进行持久化,可当做配置中心。因此记录如何启用接入Consul Key-Value启用Springboot的配置服务。

Springboot配置

<?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>3.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2022.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

properties配置

与Springboot相似的,在consul config中,会启用以下几个目录作为配置的读取路径;

  • config/demo,dev/
  • config/demo/
  • config/application,dev/
  • config/application/

Consul会在以上目录中读取data-key指定的配置文件名来读取配置(这里将data-key设置为application.yml),并且在提供了ActuatorEndpoint的情况下,可通过/refresh来主动刷新配置,或者通过Config Watch来实现监听指定的配置。

该配置方式仅在SpringCloud 2020版本以后可用,2020以前兼容版本可参考官方文档

添加config配置(config import方式)

spring:
  application:
    name: demo
  cloud:
    consul:
      host: localhost
      port: 8500
      config:
        format: yaml
        data-key: application.yml
  config:
    import: consul:/config/demo;/dev

上述配置默认读取config/demo/application.yml配置,同时导入了dev/application.yml的配置。格式需要配置正确,否则可能导致无法正确解析配置。

后续更新使用ConfigWatch来实现配置监听与刷新

参考文档

相关文章

网友评论

      本文标题:Springboot启用Consul作为配置中心

      本文链接:https://www.haomeiwen.com/subject/eitrwdtx.html