SpringCloud技术指南系列(八)配置管理之Consul配置中心

Spring Boot应用的配置文件有多种:

  1. 我们可以将配置内容写入application.yml
  2. 设置多个profile,也可以用多个application-{profile}.properties文件配置
  3. 命令行参数
  4. 自定义配置文件
  5. 配置中心

以上,除了配置中心,其他方式都不能动态去改变配置,并且生效,只有配置中心可以动态修改配置并生效。当然,并不是所有配置都能生效的,Spring加载后不再变化的配置,是不可能动态改变的,比如启动参数、zuul/gateway代理转发配置.

这篇,我们要讲的就是如何使用consul做配置中心对配置进行管理。


ConsulConfig组件和博文中的代码略有不同,但原理是一样的,代码都已经过验证。


一、引入依赖

需要引入spring-boot-starter-web和spring-cloud-starter-consul-config.

依赖如下:

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.pomit</groupId>
		<artifactId>springcloudwork</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>ConsulConfig</artifactId>
	<name>ConsulConfig</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
	</properties>
	
	<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-config</artifactId>
		</dependency>
	</dependencies>
</project>

父模块pom文件可以在https://www.pomit.cn/spring/SpringCloudWork/pom.xml获取。

二、配置文件

这里使用yaml文件写配置,配置文件分为两个,bootstrap.yml和application.yml:

  • bootstrap.yml的优先级高于application.yml。
  • 配置中心的相关配置必须放在bootstrap.yml中,否则无效。

bootstrap.yml:

server:
   port: 8814
spring:
   application:
      name: consulConfig
   cloud:
      consul:
         host: 127.0.0.1
         port: 8500
         config:
            enabled: true
            watch:
               enabled: true
            prefix: config
            defaultContext: ${spring.application.name}
            format: YAML

这里面,包含了端口、应用名、consul配置中心信息。

spring.application.name是标识了应用名。

spring.cloud.consul.host/port是consul服务器地址。

spring.cloud.consul.config.enabled开启配置中心功能。

spring.cloud.consul.config.watch.enabled是开启配置变更监控。

spring.cloud.consul.config.prefix是consul的配置路径前缀。

spring.cloud.consul.config.defaultContext是配置路径前缀后的路径,标识配置的唯一路径。

spring.cloud.consul.config.format是标识配置中心的配置是yaml文件。

application.yml:

spring:
   profiles:
      active: loc

application.yml中仍可以配置一些常用配置,我这里虽然指定了spring.profiles.active:loc,但是我没建loc配置文件,不会有影响。

三、测试配置管理

3.1 Consul上做配置

打开consul的界面,我本地是:127.0.0.1:8500

  • 点击 Key/Value, 新建config目录(新建目录和新建文件是一样的,只不过新建目录后面要加/);
  • 然后在config目录下新建consulConfig目录(上面的spring.cloud.consul.config.defaultContext)。;
  • 然后在consulConfig目录下新建data文件,写入yaml配置。

springboot 集成 nacos集群 springboot consul集群_Springcloud

比如我这里写了个welcom.value配置。

welcom:
   value: vbvcbbb

注意:直接在网页上写配置,可能会有问题,建议在yaml编辑器里写好了粘贴过来。

3.2 启动类

启动类就是普通的Springboot启动,无需其他注解。

ConsulConfigApplication :

package cn.pomit.springbootwork.consulconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsulConfigApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConsulConfigApplication.class, args);
	}
}
3.3 测试配置变更逻辑

我们使用@Value("${welcom.value}")来注入3.1中配置的welcom.value属性。并在类上加@RefreshScope注解。

  • 我们可以将welcom.value属性写入配置文件中,但要注意,配置中心的配置优先级高于本地配置文件
  • 如果本地配置文件不存在,配置中心也没有,启动会报错的
  • 注意,需要变更配置的类上,要加@RefreshScope注解,否则不会刷新配置。

ConfigInfoService :

package cn.pomit.springbootwork.consulconfig.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@RefreshScope
@Service
public class ConfigInfoService {

	@Value("${welcom.value}")
	private String welcomValue;

	public String testValue() {
		System.out.println(welcomValue);

		return welcomValue;
	}

	public String getWelcomValue() {
		return welcomValue;
	}

	public void setWelcomValue(String welcomValue) {
		this.welcomValue = welcomValue;
	}

}
3.4 测试Web

ConsulConfigRest :

package cn.pomit.springbootwork.consulconfig.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.pomit.springbootwork.consulconfig.model.ResultModel;
import cn.pomit.springbootwork.consulconfig.service.ConfigInfoService;

@RestController
@RequestMapping("/consulConfig")
public class ConsulConfigRest {
	@Autowired
	ConfigInfoService configInfoService;

	@RequestMapping(value = "/value", method = { RequestMethod.GET })
	public ResultModel value() {
		return ResultModel.ok(configInfoService.testValue());
	}
}

四、过程中用到的实体

过程中用到了ResultModel实体,是和上一篇中的实体对应的,作为统一的实体来用。

ResultModel: