概述
接着上一篇 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://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud-action</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 实现配置文件自动刷新[https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#_embedding_the_config_server] -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
</dependencies>
</project>
2.application.yml
spring:
application:
name: config-server
profiles:
active: single
---
#单机版
spring:
config:
activate:
on-profile: single
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: 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管理页面
目前来说配置中心也搭建成功了,后面的模块我会继续测试.
最后
以上就是丰富白开水为你收集整理的Spring Cloud实战(四)-配置中心一.配置中心config-server的全部内容,希望文章能够帮你解决Spring Cloud实战(四)-配置中心一.配置中心config-server所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复