Consul3-使用consul作为配置中心
在前面的文章中学习了consul在windows下的安装配置,然后consul作为spring boot的服务发现和注册中心,详细的参考:
https://blog.csdn.net/j903829182/article/details/80960802
https://blog.csdn.net/j903829182/article/details/80960917
在这里将学习consul作为springboot的配置中心,有spring cloud config的功能。这里还是以前面consul文章里面的代码为基础进行学习,不在进行重复的代码。
consul作为配置中心,需要引入配置的jar包,pom.xml的内如修改如下:
-
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0</modelVersion>
-
-
<groupId>com.jack</groupId>
-
<artifactId>consul_study1</artifactId>
-
<version>0.0.1-SNAPSHOT</version>
-
<packaging>jar</packaging>
-
-
<name>consul_study1</name>
-
<description>Demo project for Spring Boot</description>
-
-
<!--<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>2.0.3.RELEASE</version>
-
<relativePath/> <!– lookup parent from repository –>
-
</parent>-->
-
-
<parent>
-
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-parent -->
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-parent</artifactId>
-
<version>Finchley.RELEASE</version>
-
<relativePath/>
-
</parent>
-
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
-
-
-
<properties>
-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-
<java.version>1.8</java.version>
-
</properties>
-
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
-
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
-
<!--<version>2.0.0.RELEASE</version>-->
-
</dependency>
-
-
<!--feign依赖 配置-->
-
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-feign</artifactId>
-
<version>1.4.5.RELEASE</version>
-
</dependency>
-
-
-
<dependency>
-
<groupId>com.jack</groupId>
-
<artifactId>consul-api</artifactId>
-
<version>1.0.0</version>
-
</dependency>
-
-
<!--consul中健康检查需要用到actuator,不添加会check failing-->
-
<!--<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-actuator</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-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>
上面主要是引入了:
-
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
-
<!--<version>2.0.0.RELEASE</version>-->
-
</dependency>
1,修改配置文件
1)添加 bootstrap.yml配置文件
-
spring:
-
cloud:
-
consul:
-
host: localhost
-
#host: 00.0.100.200
-
port: 8500
-
#enabled将此值设置为“false”禁用Consul配置
-
config:
-
enabled: true #默认是true --
-
format: YAML # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
-
#data-key: configuration #表示consul上面的KEY值(或者说文件的名字) 默认是data
-
data-key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data
-
#prefix设置配置值的基本文件夹
-
#defaultContext设置所有应用程序使用的文件夹名称
-
#profileSeparator设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值
2)添加application-dev.yml配置文件,配置如下:
-
spring:
-
cloud:
-
consul:
-
host: localhost
-
port: 8500
-
discovery:
-
#healthCheckPath: ${management.contextPath}/health
-
healthCheckPath: /health
-
healthCheckInterval: 15s
-
instance-id: consul1
-
enabled: true
-
enabled: true
-
application:
-
name: consul1
-
server:
-
port: 8081
3)修改application.yml配置文件
-
spring:
-
profiles:
-
active: dev
以上就是一些配置文件的信息了,都是在resource目录下。
2,java代码
1)添加一个配置类
-
package com.jack.consul_study1.config;
-
-
import org.springframework.boot.context.properties.ConfigurationProperties;
-
-
/**
-
* create by jack 2018/7/15
-
*/
-
@ConfigurationProperties(prefix = "student")
-
public class StudentConfig {
-
private String name;
-
private int age;
-
private String sex;
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
-
public String getSex() {
-
return sex;
-
}
-
-
public void setSex(String sex) {
-
this.sex = sex;
-
}
-
-
@Override
-
public String toString() {
-
return "StudentConfig{" +
-
"name='" + name + '\'' +
-
", age=" + age +
-
", sex='" + sex + '\'' +
-
'}';
-
}
-
}
2)修改测试的控制器,添加测试代码
-
package com.jack.consul_study1.controller;
-
-
import com.jack.consul_study1.api.Chinese;
-
import com.jack.consul_study1.config.StudentConfig;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.beans.factory.annotation.Value;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
-
/**
-
* create by jack 2018/7/8
-
*/
-
@RestController
-
@RequestMapping("/test")
-
public class TestController {
-
@Autowired
-
private Chinese chinese;
-
-
@Value("${myName}")
-
private String myName;
-
-
@Autowired
-
private StudentConfig studentConfig;
-
-
@RequestMapping("/hello")
-
public String testHello(String name){
-
System.out.println("my name is : "+myName);
-
return chinese.sayHello(name);
-
}
-
-
@RequestMapping("/myname")
-
public String testHello(){
-
System.out.println("my name is : "+myName);
-
return myName;
-
}
-
-
@RequestMapping("/config")
-
public String testConfig(){
-
System.out.println(studentConfig.toString());
-
return studentConfig.toString();
-
}
-
-
-
-
}
3)主类添加注解@EnableConfigurationProperties
-
package com.jack.consul_study1;
-
-
import com.jack.consul_study1.config.StudentConfig;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
-
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
-
import org.springframework.cloud.openfeign.EnableFeignClients;
-
-
-
@SpringBootApplication
-
@EnableDiscoveryClient
-
@EnableFeignClients
-
@EnableConfigurationProperties({StudentConfig.class})
-
public class ConsulStudy1Application {
-
-
public static void main(String[] args) {
-
SpringApplication.run(ConsulStudy1Application.class, args);
-
}
-
}
注意:属性配置类的class需要添加到springboot的属性配置注解里面,eg:
@EnableConfigurationProperties({StudentConfig.class})
不添加的话,不能通过@Autowired注解,注入属性配置类,那么就需要在属性配置类上使用spring的bean注解,标记时一个bean
到这里,代码已经完成了,启动consul服务器,下面在consul里面进行配置了
3,consul配置
consul作为配置中心的参考文档:https://springcloud.cc/spring-cloud-dalston.html#spring-cloud-consul-config
1)创建配置,如下图
输入key和value
key为:config/consul1,dev/data
value:
myName: jack
student:
name: jack
age: 18
sex: 男
注意value用的是yml格式的配置,冒号后面有一个空格
4,运行程序测试
1)测试通过@Value注入
-
@Value("${myName}")
-
private String myName;
测试url:http://localhost:8081/test/myname
结果如下:
2)测试通过@ConfigurationProperties进行属性配置
测试url:http://localhost:8081/test/config
总结:
到这里consul的简单使用就完成了,consul有两个功能,一个是consul作为注册中心,另一个是consul作为配置中心。在本文中consul作为配置中心,有一个点需要注意,通过@Value注入的属性,修改consul的配置后,属性不能立即生效,需要服务重启。而通过@ConfigurationProperties注入的属性,修改consul的配置后,属性会立即生效,所以建议如果需要动态生效的配置,最好使使用@ConfigurationProperties进行属性的注入。
源代码地址:源码url
欢迎加群:331227121,一起学习交流
原文地址:https://blog.csdn.net/j903829182/article/details/81050507
Consul3-使用consul作为配置中心的更多相关文章
- Spring Cloud Consul使用——配置中心
1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- Consul作为配置中心,配置Asp.Net Core应用程序
前言 最近项目逐步转向基于.Net Core,目前dotnet core 虽然已出3.0了但还没有特别成熟的框架,要实现微服务,必须要解决配置中心的问题 .不管是不是微服务,节点多了配置文件一个个更改 ...
- SpringCloud使用Consul作为分布式配置中心
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_36027670/article/de ...
- Consul微服务的配置中心体验篇
Spring Cloud Consul 项目是针对Consul的服务治理实现.Consul是一个分布式高可用的系统,具有分布式.高可用.高扩展性 Consul Consul 是 HashiCorp 公 ...
- Spring Cloud 系列之 Consul 配置中心
前面我们已经学习过 Spring Cloud Config 了: Spring Cloud 系列之 Config 配置中心(一) Spring Cloud 系列之 Config 配置中心(二) Spr ...
- Spring Cloud配置中心之Consul
Consul不仅可以作为Spring Cloud中服务的注册中心,也可以作为其配置中心,这样一个系统就可以实现服务发现和统一配置,减少系统维护的麻烦,其中在使用Consul作为配置中心使用的过程中可以 ...
- .Net Core with 微服务 - Consul 配置中心
上一次我们介绍了Elastic APM组件.这一次我们继续介绍微服务相关组件配置中心的使用方法.本来打算介绍下携程开源的重型配置中心框架 apollo 但是体系实在是太过于庞大,还是让我爱不起来.因为 ...
- 服务注册发现、配置中心集一体的 Spring Cloud Consul
前面讲了 Eureka 和 Spring Cloud Config,今天介绍一个全能选手 「Consul」.它是 HashiCorp 公司推出,用于提供服务发现和服务配置的工具.用 go 语言开发,具 ...
- Spring Boot 配置 - Consul 配置中心
▶ Spring Boot 依赖与配置 Maven 依赖 <dependencyManagement> <dependencies> <dependency> &l ...
随机推荐
- csdn的富文本编辑器(html)格式的文章,添加的代码格式粘贴到项目中总是不对,我这里是maven的pom文件。
在用富文本编辑器(html)格式编辑的时候,总是出现将pom文件的配置从csdn粘贴到项目中发现很多红,是因为自己在设置的时候有问题. 因为我们的是pom.xml,这里原来设置的java呀或者js为了 ...
- Mysql之DQL------基础查询
#笔记内容来自于B站尚硅谷教学视频(av49181542)use myemployees; 查询表中的单个字段 SELECT last_name FROM employees; 查询表中的多个字段 # ...
- 【CF900D】Unusual Sequences
题目 智力下降严重 显然要反演了呀 首先必须满足\(x|y\),否则答案是\(0\) 我们枚举这个数列的\(gcd\)是\(d\)或者\(d\)的倍数 于是答案就是 \[\sum_{x|d}[d|y] ...
- springcloud系列12 config的使用
config组件分为server端和client端 config的原理: 就是当我们将配置文件放置在git上面,那么configserver就会去拉取相关配置文件至本地: 可以看到我本地是拉去了配置文 ...
- printk函数
一个不同是 printk 允许你根据消息的严重程度对其分类, 通过附加不同的记录级别或者 优先级在消息上. 你常常用一个宏定义来指示记录级别. 例如, KERN_INFO, 我们之前曾 在一些打印语句 ...
- 跟我一起使用socket.io创建聊天应用
安装express插件 新建index.js var app = require('express')(); var http = require('http').Server(app); app.g ...
- 浏览器禁用Cookie后的Session处理
1. 实现购物车, 可以基于Cookie, 也可以基于Session, 若服务器性能较差, 可以考虑基于Cookie实现购物车 2. 解决方案: URL重写 把用户可能点的每一个超链接后面,都跟上用户 ...
- postgresql数据库安装后的pgadmin4中无法加载本地连接解决办法
postgresql 在安装最后一步提示the database cluster initialisation failed, 而后点开pgadmin4发现如下图所示 经过百度搜索找出问题原因, 由于 ...
- 逻辑回归代码demo
程序所用文件:https://files.cnblogs.com/files/henuliulei/%E5%9B%9E%E5%BD%92%E5%88%86%E7%B1%BB%E6%95%B0%E6%8 ...
- hibernate使用truncate清空表 截断表
public void truncateTable(Session session, String tableNameInDb) { String sql = " truncate tabl ...