----------------------------------------------------------java
spring-cloud快速入门工程之j360-cloud-all:(欢迎star、fork)git
https://github.com/xuminwlt/j360-cloud-allgithub
spring cloud系列博客web
J360-cloud SpringCloud系列二:服务发现Discovery Service
spring
SpringCloud构建在Springboot基础上,如何使用SpringBoot请转移到shell
spring-boot入门工程之j360-boot:(欢迎star、fork)bootstrap
https://github.com/xuminwlt/j360-boot服务器
spring-boot官方地址app
http://projects.spring.io/spring-boot/
分布式
Spring Cloud分布式配置服务由服务器端和客户端共同组成,Server端提供配置信息的存储管理,客户端完成配置信息的调度,工程结构以下
套用一个图来解释该部分在springCloud分布式系统中的结构
上面的图片说明了四个微服务以及各个服务之间的依赖关系。
configuration service 处于最顶端,黄色标识,并且被其余微服务所依赖。
discovery service 处于最低端,蓝色标识,同时也被其余服务所依赖。
- bootstrap.yml
- application.yml
- 【注】建议采用bootstrap+application组合形式,通常bootstrap配置工程不可变参数,且在启动时须要设定的内容,application配置可变参数
一、POM依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
二、注解
- configserver提供了HTTP,为外部资源提供API配置的服务,只须要在Springboot程序中提供@EnableConfigServer注解便可。
- @EnableConfigServer
- 增长如下依赖 eureka(可选,做为discover服务器必选)
三、配置文件
bootstrap.properties
spring.application.name=configserver spring.cloud.config.uri=http://localhost:8888
application.properties
server.port=8888 spring.cloud.config.server.git.uri=https://github.com/xuminwlt/config-repo
四、spring-boot:run
http://localhost:8888/configserver/application.properties
一、POM
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
二、配置
bootstrap.properties
spring.application.name=client spring.cloud.config.uri=http://localhost:8888
application.properties
server.port=8080 apply.message=local message user.username=xumin
三、spring-boot:run
四、http://localhost:8080/env 查看环境变量
五、http://localhost:8080/refresh post方法,更新client中的配置
【注】环境变量刷新
- post -> localhost:8080/refresh
- 定时
- 手动
默认的环境变量库使用git服务,也可使用文件系统的服务
一、git服务
https://github.com/xuminwlt/config-repo
二、文件服务
使用 spring.profiles.active=native
环境变量仓库使用3个变量:
- {application} 在客户端映射到"spring.application.name"
- {profile} 在客户端映射到"spring.active.profiles",使用逗号分隔
- {label}这是一个服务器端功能标签“版本”的一组配置文件
当环境变量参数经过git更新后,客户端自动更新:
1:可使用 post /refresh来执行,客户端调用更新功能,configServer pull git完成后,更新环境变量信息,客户端获取最新的环境变量,而且更新
2:定时器执行:每隔X时间段更新一次
3:根据须要执行:只须要将定时器调用的更新接口暴露成服务形式调用
多客户端更新服务
1:经过Spring Cloud Bus更新
2:其余广播形式调用接口
调用接口:
/** * 手动刷新git更新方案,每20S执行一次,能够修改为手动执行,同/refresh * @link RefreshEndpoint * */ public void refresh() { Map<String, Object> before = extract(context.getEnvironment() .getPropertySources()); addConfigFilesToEnvironment(); Set<String> keys = changes(before, extract(context.getEnvironment().getPropertySources())).keySet(); scope.refreshAll(); context.publishEvent(new EnvironmentChangeEvent(keys)); } @Configuration protected static class Empty { } private void addConfigFilesToEnvironment() { ConfigurableApplicationContext capture = null; try { capture = new SpringApplicationBuilder(Empty.class).showBanner(false) .web(false).environment(context.getEnvironment()).run(); MutablePropertySources target = context.getEnvironment().getPropertySources(); for (PropertySource<?> source : capture.getEnvironment().getPropertySources()) { String name = source.getName(); if (!standardSources.contains(name)) { if (target.contains(name)) { target.replace(name, source); } else { if (target.contains("defaultProperties")) { target.addBefore("defaultProperties", source); } else { target.addLast(source); } } } } } finally { while (capture != null) { capture.close(); ApplicationContext parent = capture.getParent(); if (parent instanceof ConfigurableApplicationContext) { capture = (ConfigurableApplicationContext) parent; } else { capture = null; } } } } private Map<String, Object> changes(Map<String, Object> before, Map<String, Object> after) { Map<String, Object> result = new HashMap<String, Object>(); for (String key : before.keySet()) { if (!after.containsKey(key)) { result.put(key, null); } else if (!equal(before.get(key), after.get(key))) { result.put(key, after.get(key)); } } for (String key : after.keySet()) { if (!before.containsKey(key)) { result.put(key, after.get(key)); } } return result; } private boolean equal(Object one, Object two) { if (one == null && two == null) { return true; } if (one == null || two == null) { return false; } return one.equals(two); } private Map<String, Object> extract(MutablePropertySources propertySources) { Map<String, Object> result = new HashMap<String, Object>(); for (PropertySource<?> parent : propertySources) { if (!standardSources.contains(parent.getName())) { extract(parent, result); } } return result; } private void extract(PropertySource<?> parent, Map<String, Object> result) { if (parent instanceof CompositePropertySource) { try { for (PropertySource<?> source : ((CompositePropertySource) parent) .getPropertySources()) { extract(source, result); } } catch (Exception e) { return; } } else if (parent instanceof EnumerablePropertySource) { for (String key : ((EnumerablePropertySource<?>) parent).getPropertyNames()) { result.put(key, parent.getProperty(key)); } } }