创建一个新的springBoot web 应用命名为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”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.4
<java.version>11</java.version>
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-config
3.0.4
org.springframework.cloud
spring-cloud-starter-bootstrap
3.0.3
org.springframework.cloud
spring-cloud-dependencies
2020.0.2
pom
import
添加bootstrap.properties
spring.application.name=didispace
注册服务的时候使用服务的ip地址
eureka.instance.prefer-ip-address=true
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
#指定的环境
spring.cloud.config.profile=dev
#指定分支,当使用git的时候,默认是master
spring.cloud.config.label=master
#Config server的uri
spring.cloud.config.uri=http://localhost:7001/
配置项目启动端口号
server.port=7002
注意:spring.application.name要和你git仓库中的配置前缀相对应
这里需要格外注意,上面这些属性必须配置在 bootstrap.properties 中,这样config-server中的配置信息才能被正确加载
修改主配置类
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
创建一个RESTful接口来返回配置中心的from 属性
通过@value ("${from} ")绑定配置服务中配置的from属性,具体实现如下:
//@RefreshScope是为了可以动态刷新这个Controller的Bean