irpas技术客

Spring Cloud实战(四)-配置中心_yaoyongcsdn

未知 6880

接着上一篇?Spring Cloud实战(三)-监控中心?现在开始搭建配置中心

一.配置中心config-server 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://·/wip/config-repo default-label: master search-paths: cloud-action #native: #search-locations: classpath:/config-repo #配置其他应用所需的配置文件所在位置 rabbitmq: virtual-host: /mqbus host: localhost port: 5672 username: mqbus password: mqbus server: port: 3333 #监控配置 management: endpoints: web: exposure: #公开所有端点 对于生产,您应该仔细选择要公开的端点. include: "*" endpoint: health: show-details: always info: env: enabled: true eureka: instance: lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒 lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒 health-check-url-path: /actuator/health metadata-map: startup: ${random.int} #需要在重启后触发信息和端点更新 client: service-url: defaultZone: http://localhost:1111/eureka/ #eureka-client设置eureka-server的地址 --- spring: config: activate: on-profile: peer1 cloud: config: server: git: #uri: file://${user.home}/config-repo uri: https://gitee.com/wip/config-repo default-label: master search-paths: cloud-action # native: # search-locations: classpath:/config-repo #配置其他应用所需的配置文件所在位置 rabbitmq: virtual-host: /mqbus host: localhost port: 5672 username: mqbus password: mqbus server: port: 3334 #监控配置 management: endpoints: web: exposure: include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点. endpoint: health: show-details: always info: env: enabled: true eureka: instance: lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒 lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒 health-check-url-path: /actuator/health metadata-map: startup: ${random.int} #需要在重启后触发信息和端点更新 client: service-url: defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址 --- spring: config: activate: on-profile: peer2 cloud: config: server: git: #uri: file://${user.home}/config-repo uri: https://gitee.com/wip/config-repo default-label: master search-paths: cloud-action # native: # search-locations: classpath:/config-repo #配置其他应用所需的配置文件所在位置 rabbitmq: virtual-host: /mqbus host: localhost port: 5672 username: mqbus password: mqbus server: port: 3335 #监控配置 management: endpoints: web: exposure: include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点. endpoint: health: show-details: always info: env: enabled: true eureka: instance: lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒 lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒 health-check-url-path: /actuator/health metadata-map: startup: ${random.int} #需要在重启后触发信息和端点更新 client: service-url: defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址 3.ConfigApplication.java package com.example.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @author 86188 */ @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } } 4.测试 环境准备

该模块用到了RabbitMQ,此处我使用了docker快速搭建了RabbitMQ环境,如果你已经有了RabbitMQ环境可以略过.如果没有,可以查看我的其他博客Windows10家庭版上安装docker等其他相关博客.

docker run -dp 5672:5672 -p 15672:15672 --name rabbitmq -v D:/WinLinux/docker/rabbitmq/data:/var/lib/rabbitmq -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=yaoyong rabbitmq:3.8.9-management

启动项目

依次启动之前的项目

java -jar??register-server-0.0.1-SNAPSHOT.jar

java -jar?monitor-server-0.0.1-SNAPSHOT.jar

java -jar?config-server-0.0.1-SNAPSHOT.jar

git上添加一个配置,然后测试 传送门

?测试内容如下

spring: datasource: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306 username: root password: root # rabbitmq: # virtual-host: /mqbus # host: localhost # port: 5672 # username: mqbus # password: mqbus #book: # name: Spring Cloud 微服务实战 # author: 你猜一猜

?Spring Cloud Config官方文档

HTTP 服务具有以下形式的资源:

/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties

例如:

curl localhost:8888/foo/development curl localhost:8888/foo/development/master curl localhost:8888/foo/development,db/master curl localhost:8888/foo-development.yml curl localhost:8888/foo-db.properties curl localhost:8888/master/foo-db.properties

?修改测试内容

spring: datasource: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306 username: root password: root rabbitmq: virtual-host: /mqbus host: localhost port: 5672 username: mqbus password: mqbus

再次访问

可以看到最新的改动,此时,查看RabbitMQ管理页面

?目前来说配置中心也搭建成功了,后面的模块我会继续测试.


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Spring #Cloud实战四配置中心 #接着上一篇Spring