spring boot項目再也不讀取本身的配置文件,而是統一去配置中心讀取屬於本身的配置文件java
IDEA上面建立一個空的maven工程,命名爲configuration-service
。git
mkdir -p src/main/java/hello
<?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>io.spring</groupId> <artifactId>configuration-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
這個Maven配置主要是引入Spring Cloud的配置中心服務器的配置。github
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServiceApplication { public static void main(String[] args) { SpringApplication.run(ConfigServiceApplication.class, args); } }
這裏主要是啓用該服務爲配置中心服務。web
server.port=8888 spring.cloud.config.server.git.uri=${HOME}/Downloads/config
這裏配置配置中心服務端口爲8888,以及讀取的配置文件位置目錄配置。spring
在IDEA上面建立Maven空工程,命名爲configuration-client
。apache
mkdir -p src/main/java/hello
<?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>io.spring</groupId> <artifactId>configuration-client</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
這裏是spring boot的正常配置。bootstrap
package hello; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController public class MessageRestController { @Value("${message:Hello default}") private String message; @RequestMapping("/message") String getMessage() { return this.message; } }
這裏暴露一個請求,這個請求是從配置文件中讀取本身的配置參數message
。bash
server.port=8082 spring.application.name=a-bootiful-client management.endpoints.web.exposure.include=*
這裏主要就是設置當前服務端口和服務名,已經開放相關運維接口,這些運維接口主要就是localhost:8082/actuator/refresh
的post請求,用於刷新配置中心的配置參數的。服務器
spring.application.name=a-bootiful-client # N.B. this is the default: spring.cloud.config.uri=http://localhost:8888
這裏主要是配置當前服務名稱,以及配置中心的服務地址。注意這裏服務名,必須與配置中心讀取的文件名保持一致,即配置中心的配置文件名必須爲a-bootiful-client.properties
纔可以讀取到文件。app
配置中心主要使用git來管理配置文件,也就是說每次對配置文件的修改,必須使用git來提交後,配置中心纔會承認。
cd ~/Downloads/config git init vi a-bootiful-client.properties git add a-bootiful-client.properties git commit -m "建立a-bootiful-client.properties文件"
a-bootiful-client.properties內容以下:
message=zyl
vi a-bootiful-client.properties git commit -am "修改配置a-bootiful-client.properties文件"
a-bootiful-client.properties內容以下:
message=zyl2
這個適合就須要先調用刷新接口,再進行測試。
再調用測試接口:
上面就是對Spring Cloud的配置中心這個部門的基本使用,這裏仍是涉及到配置文件更新後,使用git版本控制工具提交後,各個節點怎麼批量更新的問題。
源代碼在這裏: