本项目采用版本选择:
springboot的版本是2.1.2.RELEASE
springcloud的版本是Greenwich.SR1
springcloud仓库使用的是Gitee
1、下载并安装RabbitMq
下载地址:http:/ /www.rabbitmq.com/download.html
Spring Cloud大型企业分布式微服务云架构源码请加一七九一七四三三八零
2、SpringCloud Config Server端配置
config-server pom.xml的配置如下
复制代码 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.2.RELEASE com.lee.self config 0.0.1-SNAPSHOT config Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone
config-server的bootstrap.yml的配置如下:
server: port: 9010spring: application: name: config cloud: config: server: git: #码云的用户名和密码及地址 username: **** password: **** uri: https://gitee.com/lijiaxi118/blog-repoeureka: client: service-url: defaultZone: http://localhost:9001/eureka/management: endpoints: web: exposure: include: "bus-refresh"复制代码
3、SpringCloud Config Client端配置
config-client pom.xml配置如下:
复制代码 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.2.RELEASE com.lee.self user 0.0.1-SNAPSHOT user Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger-ui 2.4.0 io.springfox springfox-swagger2 2.4.0 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone
config-client的bootstrap.yml配置:
spring: application: name: user cloud: config: discovery: service-id: config enabled: true profile: proeureka: client: service-url: defaultZone: http://localhost:9001/eureka/server: port: 9005ribbon: eureka: enabled: true复制代码
4、测试
启动注册中心eureka
启动config server 访问localhost:9010/user-dev.yml监测是否启动成功
启动config client
在client项目中写一个测试类如下
测试类如下
@Component@RefreshScope@Data@ConfigurationProperties(prefix = "spring.redis")public class RedisConfig { private String host; private String port;}@RestController@RequestMapping("/test")public class ConfigController { @Autowired private RedisConfig redisConfig; @RequestMapping("/print") public String print() { return "host:" +"\t" + redisConfig.getHost()+"====Port:"+redisConfig.getPort(); }}复制代码
页面访问http://localhost:9005/test/print,查看结果
修改gitee中的user-dev.yml文件
使用postman的post请求访问http://localhost:9010/actuator/bus-refresh
继续浏览器访问http://localhost:9005/test/print查看结果是否更新
自此config的刷新基本完成,最后一步在gitee中配置webhooks,每一次push修改之后都会自动的通知到各个客户端
4、配置仓库中的webhooks
点击gitee中的管理——》webhooks,填入外网访问的路径即可。