spring cloud config server搭建
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>config</name>
<description>config project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>2.1.0.RELEASE</spring.cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- eureka依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<!-- 內建Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
#記住每個屬性和值之間,要在冒号後面加一個空格
#指定啟用端口
server:
port: 8767
eureka:
instance:
hostname: peer3
client:
registerWithEureka: false
fetchRegistry: true
serviceurl:
defaultZone: http://peer1:8761/eureka/
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://github.com/MichardChen/struts
searchPaths: WebContent
label: master
#指定git的uri,
uri是git倉庫位址
searchPaths是指倉庫路徑,也就是配置檔案目錄
label是倉庫分支
username通路git倉庫的使用者名
password通路git倉庫使用者名密碼,如果倉庫是公開倉庫,username和password可以不寫
Application
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
格式化json
{
"name": "webname",
"profiles": ["dev"],
"label": null,
"version": "f8eedc8a1f42654d1403b79d07b6d394dc52db4e",
"state": null,
"propertySources": [{
"name": "https://github.com/MichardChen/struts/WebContent/application-dev.properties",
"source": {
"server.port": "8080",
"foo": "99",
"webname": "spring"
}
}, {
"name": "https://github.com/MichardChen/struts/WebContent/application-dev.yml",
"source": {
"server.port": 8767,
"eureka.instance.hostname": "peer3",
"eureka.client.registerWithEureka": false,
"eureka.client.fetchRegistry": true,
"eureka.client.serviceurl.defaultZone": "http://peer1:8761/eureka/",
"spring.application.name": "config",
"spring.cloud.config.server.git.uri": "https://github.com/MichardChen/struts",
"spring.cloud.config.server.git.searchPaths": "WebContent"
}
}, {
"name": "https://github.com/MichardChen/struts/WebContent/application.yml",
"source": {
"server.port": 8767,
"eureka.instance.hostname": "peer3",
"eureka.client.registerWithEureka": false,
"eureka.client.fetchRegistry": true,
"eureka.client.serviceurl.defaultZone": "http://peer1:8761/eureka/",
"spring.application.name": "config",
"spring.cloud.config.server.git.uri": "https://github.com/MichardChen/struts",
"spring.cloud.config.server.git.searchPaths": "WebContent"
}
}]
}
我們發現讀取了github對應的目錄下的檔案,
http請求位址和資源檔案映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
===================================
建立config client
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>config-client</name>
<description>config project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>2.1.0.RELEASE</spring.cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- eureka依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<!-- 內建Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
bootstrap.yml
#記住每個屬性和值之間,要在冒号後面加一個空格
#指定啟用端口
server:
port: 8768
eureka:
instance:
hostname: peer3
client:
registerWithEureka: false
fetchRegistry: true
serviceurl:
defaultZone: http://peer1:8761/eureka/
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
uri: http://localhost:8767/
IndexController.class
@RestController
@RequestMapping("/index")
public class IndexController {
@Value("${webname}")
String webname;
@RequestMapping("/test")
public String index(){
return webname;
}
}
通路/index/test,請求成功
使用springcloud bus實作自動更新配置,這裡大概說下,我們使用kafka,server加入依賴。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
application.yml加入
#是否需要權限拉去,預設是true,如果不false就不允許你去拉取配置中心Server更新的内容
management:
security:
enabled: false
client需要加入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
@RestController
//這裡面的屬性有可能會更新的,git中的配置中心變化的話就要重新整理,沒有這個注解内,配置就不能及時更新
@RefreshScope
public class TestController {
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
@RequestMapping("/test")
public String test(){
return this.name+this.age;
}
}
這個使用kafka,實作自動更新的,可以參考https://www.cnblogs.com/huangjuncong/p/9077099.html