在前面的文章中學習了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