spring cloud config
程序员文章站
2022-03-27 07:57:17
...
spring cloud config 示例
git 的配置地址:https://github.com/zhoucanji/config-repo
服务端
maven 配置文件
<?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.zcj</groupId> <artifactId>myBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>configService</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.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>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring配置文件
spring.config.name=service spring.cloud.config.server.git.uri=https://github.com/zhoucanji/config-repo server.port=8881
服务端入口
package com.zcj; 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); } }
客户端
maven 配置文件
<?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.zcj</groupId> <artifactId>myBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>configClient</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.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-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>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring配置文件
server.port=8882 spring.config.name=client
bootstrap.yml
spring.cloud.config.uri=http://127.0.0.1:8881 spring.cloud.config.label=master spring.cloud.config.profile=pro spring.application.name=application
客户端入口
package com.zcj; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class ConfigClientApplication { @Value("${zcj.name}") private String zcj; public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @RequestMapping("zcj") public String zcj(){ return "zcj.name="+zcj; } }
运行结果
git:
https://github.com/zhoucanji/config-repo/blob/master/application-pro.properties
https://github.com/zhoucanji/config-repo/blob/master/application.properties
查看服务端获取到的git的配置文件
http://127.0.0.1:8881/master/application-pro.properties
http://127.0.0.1:8881/master/application-default.properties
http://127.0.0.1:8881/application/pro/master
http://127.0.0.1:8881/application/default/master
如果客户端bootstrap.properties中spring.cloud.config.profile=pro
如果客户端bootstrap.properties中spring.cloud.config.profile=default
总结:
- config-server 配置服务端,服务管理配置信息
- config-client 客户端,客户端调用server端暴露接口获取配置信息
推荐阅读