对于分布式的配置我不做过多的解释,总结来说就是哪个系统需要什么配置就去提供配置的服务端去读取配置而不需要本地再进行配置,这样的技术就很多,Apollo,XDiamond,Qconf,Disconf比如我们公司用的是Disconf,SpringCloudConfigServer也是这样类似的一个项目。
我自己学习时通过github作为他的服务器,你也可以选择其他的。
实现如下:
1.配置源建立
首先在github上建立一个空的初始仓库,获取SSH协议的地址,如下面这样
将项目克隆到本地,并且在本地创建文件推上去,文件如上面的application.yml
内容如下:
spring:
profiles:
active:
- dev
---
spring:
profiles: dev #开发环境
application:
name: microservicecloud-config-gysoft-dev
---
spring:
profiles: test #测试环境
application:
name: microservicecloud-config-gysoft-test
# 请保存为UTF-8格式
2.配置服务创建
创建springboot项目
依赖如下:
<dependencies>
<!-- 避免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>
<!-- springCloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
</dependencies>
在启动类里加上注解@EnableConfigServer
创建配置文件application.properties
server.port=3344
spring.application.name=microservicecloud-config
spring.cloud.config.server.git.uri=git@github.com:wws11/CloudConfig.git
这里配置好配置服务地址,配置服务配置文件的的名称
配置windows本地映射,修改hosts文件,增加127.0.0.1 config-3344.com
启动服务,访问
服务搭建完毕。
3.客户端的配置
客户端的实现也很简单。
增加服务器的配置,在本地创建
microservicecloud-config-client.yml推到github上,结果如下
配置的内容就是客户端需要配置的内容,这里的内容如下:
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-test.com:7001/eureka/
dev默认端口是8201,测试默认是8202。
创建springboot项目,核心依赖如下
<!-- SpringCloud Config客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
整个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>com.gysoft</groupId>
<artifactId>microservicecloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-config-client-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>
增加两个配置文件
配置如下:
application.yml这文件配置服务配置名字。
server:
port: 3355
spring:
application:
name: microservicecloud-config-client
boostrap.yml配置如下:
spring:
cloud:
config:
name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
profile: dev #本次访问的配置项
label: master
uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
这里配置的是dev,这就对应着配置服务器里面的dev配置,
在客户端增加Controller,通过Controller去实现配置文件的读取
controller配置如下:
package com.atguigu.springcloud.rest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@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;
}
}
通过@Value的去获取配置文件的值,测试如下:
这样吧就完成了SpringCloud Config Server的基本配置。