SpringCloud(十)高可用的配置中心-Spring Cloud Config+Eureka+Spring Cloud Bus
配置中心高可用(Spring Cloud Config+Eureka+Spring Cloud Bus)
配置中心也可像其他springcloud中的服务一样,注册在服务中心,基于eureka可实现配置中心的高可用。
基于git的web hook与Springcloud bus可实现配置变更后推送至相应服务。
整体架构如图。
配置中心修改
基于配置中心进行改造,首先将Config Server引入eureka注册至服务中心。
添加eureka客户端依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.hanson</groupId> <artifactId>ConfigCenter</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ConfigCenter</name> <description>Demo project for Spring Boot</description> <!-- 修改parent支持eureka client --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 修改spring-cloud.version 为Edgware.SR2--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR2</spring-cloud.version> </properties> <dependencies> <span style="color: #ff0000;"><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency></span> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project> |
代码添加@EnableDiscoveryClient,使其注册至服务中心。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.hanson.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer <span style="color: #ff0000;">@EnableDiscoveryClient</span> public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } |
添加eurekaserver的相关配置
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
spring.application.name=config-server server.port=8888 #配置git仓库地址 #gitlab需要.git结尾 spring.cloud.config.server.git.uri=http://huhl@gitlab.hzbd.com/Finance/Config-Repository.git #spring.cloud.config.server.git.uri=https://github.com/hanlin5566/SpringCloud-Netflix #spring.cloud.config.server.git.uri=https://github.com/spring-cloud-samples/config-repo #配置仓库路径,支持通配符 #spring.cloud.config.server.git.searchPaths=/spring-cloud-code/Config-Repository/client* #gitlab spring.cloud.config.server.git.searchPaths=var* #springcloud github #spring.cloud.config.server.git.searchPaths=foo,bar* #配置仓库的分支 spring.cloud.config.label=master #访问git仓库的用户名 spring.cloud.config.server.git.username=huhanlin@huizhongcf.com #访问git仓库的密码 spring.cloud.config.server.git.password=hanlin5566 <span style="color: #ff0000;">#Eureka server的配置 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/</span> |
启动多服务时可在vm中添加参数,配置不同端口。
–server.port=8889
此时配置中心已经注册在了服务中心
配置中心客户端修改
基于配置中心进行客户端改造,首先将Config Client引入eureka注册至服务中心。
添加eureka客户端依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.hanson</groupId> <artifactId>server-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>server-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <span style="color: #ff0000;"><version>1.5.10.RELEASE</version></span> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <span style="color: #ff0000;"><spring-cloud.version>Edgware.SR2</spring-cloud.version></span> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <span style="color: #ff0000;"><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </span> <span style="color: #ff0000;"><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency></span> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project> |
添加eurekaserver的相关配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
spring.application.name=consumer server.port=8080 #与git上的${application}相对应 spring.cloud.config.name=var #仓库的分支 spring.cloud.config.label=master #dev开发环境配置文件 #test测试环境 #pro正式环境 spring.cloud.config.profile=dev <span style="color: #ff0000;">#指定从配置中心读取文件 spring.cloud.config.discovery.enabled=true #配置中心的servieId,即服务名 spring.cloud.config.discovery.serviceId=config-server #服务中心配置 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ </span> #直接从配置中心读取配置 ##confServer的地址 #spring.cloud.config.uri= http://localhost:8888/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.hanson.serverclient; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController <span style="color: #ff0000;">@EnableDiscoveryClient</span> //使用该注解后,可以在运行时直接刷新Bean,并在下次方法调用时,得到一个全新的实例。 @RefreshScope //注解@RefreshScope指示Config客户端在服务器配置改变时,也刷新注入的属性值 public class ServerClientApplication { public static void main(String[] args) { SpringApplication.run(ServerClientApplication.class, args); } @Value("${init.failed.exit}") // 读取gitlab配置文件中的属性,如果我们读取到了值,说明客户端是OK的 private String exited; @RequestMapping(value = { "" }, method = RequestMethod.GET) public String hello() { return exited; } } |
访问http://localhost:8080/ ,此时已经通过配置中心返回了配置。
配置更新自动发布 (SpringCloud Bus+git webHook)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。SringCloud Bus可以依赖kafka和rabbitmq通讯,本例我们使用rabbitmq进行通讯。
在server client中添加rabbitmq的配置
#rabbitMQ配置
spring.rabbitmq.host=192.168.1.106
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
{
“timestamp”: “2018-03-22T10:16:07.260+0000”,
“status”: 401,
“error”: “Unauthorized”,
“message”: “Full authentication is required to access this resource.”,
“path”: “/bus/refresh”
}
如果报这个错误则添加spring-boot-starter-actuator,并且配置management.security.enabled=false,忽略权限拦截。
此时通过post http://localhost:8080/bus/refresh 就可以将git上的变更刷新。
通过webhook动态推送刷新
其实很简单,就是在git中配置webhook,当有配置变动时,推送一个post请求至指定的url,就实现刷新了。
- 点击settings
- 点击integrations
- 之后点击add
- 可以点击test进行测试。
此时当git上的配置有修改时,通过webhook会向http://localhost:8080/bus/refresh 发送post请求,之后相应服务就会自动刷新配置。
之前的false已经刷新为true
发表评论