Config的应用
spring boot 2.4.4 spring-cloud-config 2020.0.2
版本父控总览
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiecongcong</groupId>
<artifactId>spring-cloud-config</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>spring-cloud-eureka-server-9090</module>
<module>spring-cloud-config-server-9091</module>
<module>spring-cloud-order-server-8082</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<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>
</project>
-
eureka-server:注册中心
版本信息
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.xiecongcong</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.xiecongcong</groupId> <artifactId>spring-cloud-eureka-server-9090</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
配置文件
spring.application.name=spring-cloud-eureka-server server.port=9090 eureka.client.service-url.defaultZone=http://localhost:9090/eureka
启动类
@EnableEurekaServer @SpringBootApplication public class SpringCloudEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaServerApplication.class, args); } }
-
config-server:配置中心
版本信息
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xiecongcong</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.xiecongcong</groupId> <artifactId>spring-cloud-config-server-9091</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
配置信息
spring: application: name: spring-cloud-config-server-9091 cloud: config: server: git: uri: git地址 username: git用户名 password: git密码 server: port: 9091 eureka: client: service-url: defaultZone: http://localhost:9090/eureka
启动类
@EnableConfigServer @EnableEurekaClient @SpringBootApplication public class SpringCloudConfigServer9091Application { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigServer9091Application.class, args); } }
-
config-client
版本信息
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>spring-cloud-config</artifactId> <groupId>com.xiecongcong</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.xiecongcong</groupId> <artifactId>order-server-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
配置信息
spring.application.name=order-server-provider server.port=8082 eureka.client.service-url.defaultZone=http://localhost:9090/eureka spring.config.import=optional:configserver:http://localhost:9091
启动类
@SpringBootApplication public class OrderServerProviderApplication { public static void main(String[] args) { SpringApplication.run(OrderServerProviderApplication.class, args); } }
controller
@RestController public class ConfigController { @Value("${name}") private String name; @GetMapping("/config") public String getConfig(){ return name; } }
-
git配置(码云)
启动注册中心 —> 配置中心 —> order server
访问http://localhost:8082/config
注意: 当配置中心更改配置文件时,config client是无法实时感知到的。
解决方法: spring cloud bus