springcloud config的简单实例
因为案例中有服务端和客户端多个服务,所以先创建一个父项目,其他服务为子项目
父项目只需引入springboot依赖即可
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/>
</parent>
一 服务注册与发现-eureka
eureka是springcloud的五大组件之一,先创建eureka服务,注意该项目为子项目,路径在父项目之后,如图
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
```java
如图
配置文件
添加application.yml配置文件
server:
port: 8081
spring:
application:
name: eureka-server
#eureka的配置项有3类:
#client,instance,server
eureka:
instance:
hostname: localhsot
client:
service-url:
defaultZone:http://${eureka:instance:hostname}:${server:port}/eureka/:
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: true #线上阶段应该打开自我保护机制,避免因为网络抖动(网络分区故障)而产生的服务剔除
renewal-percent-threshold: 0.85 #服务续约的比例,服务列表
eviction-interval-timer-in-ms: 5000
创建启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
eureka项目架构如图:
点击启动类的运行按钮,开始运行
启动成功
可以在浏览器输入localhost:8081查看eureka页面
springCloud config服务端
创建config子项目
依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
</dependencies>
创建配置文件
server:
port: 8082
tomcat:
uri-encoding: utf-8
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://gitee.com/qfp17393120407/spring-cloud-config.git #gitee上的仓库名称,此处为http方式
#搜索目录
search-paths:
- springcloud-config
username: 173****0407
password: 123455
#读取的分支
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka
注意:springcloud config配置中心是为了统一管理配置文件,所以需要将配置放到github仓库或gitee,我使用的是gitee,github经常连不上,不推荐。(没号的可以在gitee上建一个账号,网址gitee.com)
gitee创建配置文件
在gitee上创建一个新的仓库
然后在创建一个配置文件
注意:填写代码中的gitee的配置时,url为gitee仓库中点击“克隆/下载”按钮—HTTP----复制后的内容,用户名和密码为gitee的用户名和密码,如图
最后启动服务,在浏览器输入localhost:8082/master/config-dev.yml,读取到配置信息。
spring Cloud config客户端
创建子项目后添加依赖
依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
配置文件
创建名为bootstrap.yml的配置文件
server:
port: 8083
spring:
application:
name: config-client
cloud:
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述三个综合:master分支上config-dev.yml的配置文件被读取http://localhost:8082/master/config-dev.yml
uri: http://localhost:8082
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka
management:
endpoints:
web:
exposure:
include: "*"
启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}
读取gitee中的配置
在springcloud config的客户端中读取gitee中的配置
创建controller
@RestController
@RefreshScope
public class ClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
启动服务后访问http://localhost:8083/configInfo
读取成功!
配置中心是为了能够修改配置后无需重启服务,动态刷新。修改gitee的config-dev.yml文件后,需要需要发送POST请求:"http://localhost:8083/actuator/refresh,可以通过postman软件发送
请求,如图
发送成功后再次访问http://localhost:8083/configInfo,即可发现显示的是已更新内容。