spring cloud config 大家都知道是配置中心,是用于根据不同环境加载不同配置文件,话不多说,直接上干货!
以下是基于springboot V2.4.2版本,springcloud (V3.0.0)版本,注册中心使用consul
spring config server
-
config server 的pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tongxue</groupId>
<artifactId>spring-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
-
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class SpringConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConfigServerApplication.class, args);
}
}
-
application.properties
server.port=9999
spring.application.name=config-server
# 注册到consul中心
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.prefer-ip-address=false
#注册到consul的服务名称
spring.cloud.consul.discovery.service-name=${spring.application.name}
#配置文件地址
spring.cloud.config.server.git.uri=https://github.com/leozhang136/spring-config.git
spring config client
-
config client 配置pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tongxue</groupId>
<artifactId>spring-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</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-consul-discovery</artifactId>
</dependency>
<!--actuator自动装载-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-actuator</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
-
启动类
@Configuration
@SpringBootApplication
@EnableDiscoveryClient
public class SpringConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConfigClientApplication.class, args);
}
}
-
测试读取配置参数
@RestController
public class UserController {
@Value("${user.userName}")
private String userName;
@Value("${user.sex}")
private String sex;
@Value("${user.age}")
private Integer age;
@RequestMapping(value = "/user")
public String getUserInfo(){
return "userName:"+userName+" sex:"+sex+" age:"+age;
}
}
-
application.properties
server.port=8088
spring.application.name=config-client
spring.profiles.active=test
# consul
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.prefer-ip-address=false
#注册到consul的服务名称
spring.cloud.consul.discovery.service-name=${spring.application.name}
# 通过uri进行拉取
#spring.cloud.bootstrap.enabled=true
#spring.config.import=optional:configserver:http://localhost:9999
#spring.cloud.config.fail-fast=true
##spring.cloud.config.name=study
#spring.cloud.config.profile=${spring.profiles.active}
#spring.cloud.config.label=master
# 通过服务进行拉取
spring.cloud.config.discovery.enabled=true
spring.config.import=optional:configserver:
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.fail-fast=true
spring.cloud.config.profile=${spring.profiles.active}
spring.cloud.config.label=master
看到Fetching config from server at : http://LAPTOP-TE97R99D:9999/ 表示去拉取远程配置信息