作用:通过config配置中心,我们将配置信息全部保存在git上面,然后拉取下来使用,有修改时,我们只是需要修改git上面的内容即可
首先是对pom文件的解析:
<dependencies>
<!--config中心的-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--eureka client 的pom-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--eureka 集成了hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--需要使用到web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
启动类的代码展示:
@SpringCloudApplication
@EnableConfigServer //表明这是一个config配置中心的启动类
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
配置文件的属性讲解:
server:
port: 8101
spring:
application:
name: config
profiles:
active: dev
cloud:
config:
server:
git:
uri: https://github.com/wait-love/news.git #配置 git 仓库地址
username: ***** #访问 git 仓库的用户名
password: ***196135 #访问 git 仓库的用户密码
label: master #配置仓库的分支
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
在这里插入代码片
同时我们需要给其他模块用到config中心的pom文件如下:
<!--需要用到config中心的配置文件内容-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
然后在其bootstrap.yml文件
spring:
cloud:
config:
name: eurekaclient # 这是表明需要用到的yml文件名部分,如果需要用到多个就可以用,隔开
label: master #表明branch名称
discovery:
enabled: true
serviceId: config #config server中application name
#配置服务中心的地址
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8082/eureka/
应用了config导致的一个问题的发生,就是每次更新了文件配置都需要去启动项目,所以我们可以应用到手动刷新的模式,它是针对client端的
主要的操作流程有:pom文件的导入:配置文件的修改信息:定义一个手动的刷新路径:访问路径:http://localhost:8763/actuator/refresh 刷新这个路径之后,就会从git仓库中拉取到对应的内容了
`<!--需要添加手动刷新的pom文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>``
配置文件的修改内容:(记住是在git仓库中的配置文件信息)
management:
endpoints:
web:
exposure:
include: refresh,health,info
定义一个手动刷新的路径:
@RestController
@RefreshScope //手动刷新的注解,添加这个注解,主要是因为用到git上面的文件内容
public class HelloClient {
@Value("${server.port}")
private String port;
@RequestMapping(value = "hello")
public String test(){
return "hello client" + port;
}
}