SpringCloud分布式配置中心
- SpringCloud分布式配置中心
SpringCloud Config为微服务架构中的微服务提供集中心化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
它与Eureka一样分为服务端和客户端
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端获取和加载配置信息,加密/解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及业务相关的配置内容,并在启动的时候从配置中心获取并配置信息,配置服务器默认从git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端来方便的管理和访问配置内容。
-
- 创建一个git仓库
-
- 本地新建仓库并克隆git上刚才创建的仓库
-
- 本地仓库中新建application.yml并上传
文件内容
spring: profiles: active: - dev --- spring: profiles: dev #开发环境 application: name: microservicecloud-config-shun-dev --- spring: profiles: test #测试环境 application: name: microservicecloud-config-shun-test #请保存为UTF-8格式
|
上传
远程仓库中
-
- SpringCloud config服务端与github远程通信
其实就是使用SpringCloud从github远程仓库读取配置文件
-
-
- 父工程下新建maven module
-
-
-
- pom文件
-
<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> <parent> <groupId>czs</groupId> <artifactId>microserviceone</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microserviceone-serverconfig-3344</artifactId>
<dependencies> <!-- springCloud Config服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback --> <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit</artifactId> <version>4.10.0.201712302008-r</version> </dependency> <!-- 图形化监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 熔断 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <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-jetty</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> </dependency> <!-- 热部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
</project> |
|
|
-
-
- 启动类-------注意@EnableConfigServer注解
-
package shun.cloud.config;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;
/** * @author czs * @version 创建时间:2018年8月27日 下午9:57:15 */ @EnableConfigServer // 服务端,要与github通信就必须开启 @SpringBootApplication public class ConfigApp_3344 {
public static void main(String[] args) { SpringApplication.run(ConfigApp_3344.class, args); }
}
|
-
-
- yml配置文件
-
server: port: 3344
spring: application: name: microservicecloud-config cloud: config: server: git: uri: [email protected]:chenzongshun/microserviceConfigOne.git #GitHub上面的git仓库名字 |
-
-
- 访问测试
-
Git上的配置
访问到的配置
访问方式
http://127.0.0.1:3344/application-dev.yml
http://127.0.0.1:3344/master/application-test.yml
http://127.0.0.1:3344/application/dev/master
-
- SpringCloud config客户端配置与测试
即客户端通过服务端获得github上的配置
-
-
- 本地仓库新建microservicecloud-config-client.yml并上传
-
spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://127.0.0.1:7001/eureka/ --- server: port: 8202 spring: profiles: test application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://127.0.0.1:7001/eureka/ |
-
-
- 新建microserviceone-clientconfig-3355项目
-
-
-
- pom文件
-
<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> <parent> <groupId>czs</groupId> <artifactId>microserviceone</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microserviceone-clientconfig-3355</artifactId> <dependencies> <!-- SpringCloud Config客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <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-jetty</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> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
|
-
-
- bootstrap.yml与application.yml
-
Github上面的配置文件名字---->下面两个yml会用到
Bootstarp.yml--------->注意红色字体
spring: cloud: config: name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名,如果项目有application.yml,那么微服务名称就必须要是这个名字 profile: test #本次访问的配置项 label: master uri: http://localhost:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址 |
application.yml--------->注意红色字体
spring: application: name: microservicecloud-config-client #如果有bootstrap.yml文件,那么这个名字就必须是bootstrap.yml里面获取git上配置文件的去掉后缀的文件名字 |
怎么样?没有看见设置端口吧?因为待会会连接上config服务端,从服务端获取git上之前上传的yml文件中,就包含了端口号,通过bootstrap.yml中的profile来选择启用哪种配置
-
-
- 获取服务端信息类以及启动类
-
测试返回类
package shun.cloud.test;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
/** * 从服务端获取到程序的名字,默认配置方式,服务端口,然后打印并返回 * @author Administrator */ @RestController public class ConfigClientRest {
@Value("${spring.application.name}") private String applicationName;
@Value("${eureka.client.service-url.defaultZone}") private String eurekaServers;
@Value("${server.port}") private String port;
@RequestMapping("/config") public String getConfig() { String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; System.out.println("******str: " + str); return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; } }
|
启动类
package shun.cloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/** * @author czs * @version 创建时间:2018年8月28日 上午11:23:29 */ @SpringBootApplication public class Clientconfig3355_App {
public static void main(String[] args) throws Exception { SpringApplication.run(Clientconfig3355_App.class, args); }
}
|
-
-
- 开始进行访问测试
-
启动3344
等待3344服务启动完毕后再次启动3355,如果报错找不到那就再来一遍,还报错查找其它原因
先来看看github上面的文件内容
先来看看test环境
再来看看配置成dev环境