简介
Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端ConfigServer与客户端ConfigClient两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密 / 解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config 实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于 Spring 构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于 Spring Cloud Config 实现的配置中心默认采用 Git 来存储配置信息,所以使用 Spring Cloud Config 构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。
服务端配置 Config Server(高可用)
添加依赖包文件POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-config-server</artifactId>
<name>springcloud-config-server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置application.yml文件
- application.yml
spring:
application:
name: springcloud-config-server
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
cloud:
config:
# 配置仓库的分支
label: master
server:
git:
# 配置git仓库地址
# uri: git@local-git.com/springcloud-config.git
uri: file:E:/SpringBoot/SpringCloud/springcloud-root/springcloud-config
# 配置仓库路径
search-paths: config-file
# 访问git仓库的用户名
username: zhaojq
# 访问git仓库的用户密码
password: 123456
server:
port: 8130
eureka:
instance:
hostname: eureka-config-server.com
instance-id: eureka-config-server
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
修改C:\Windows\System32\drivers\etc\hosts
127.0.0.1 eureka-config-server.com
配置本地Git仓库
下载安装git服务
https://git-scm.com/
git bash配置:
git config --global user.name "zhaojq"
git config --global user.email "zhaojq@email.com"
git config --global user.password "123456"
创建git本地库
-
application-test.properties
测试环境配置文件
-
application-dev.properties
开发环境配置文件
-
application-pro.properties
正式环境配置文件
添加Config Server启动类
- ConfigServerApplication.java
在启动类加上@EnableConfigServer注解
package org.springcloud.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
测试Config Server
启动项目springcloud-config-server
http请求地址和资源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
访问地址 http://eureka-config-server.com:8130/application/test/master
访问地址 http://eureka-config-server.com:8130/application-test.yml
访问地址 http://eureka-config-server.com:8130/master/application-test.yml
访问地址 http://eureka-config-server.com:8130/application-test.properties
访问地址 http://eureka-config-server.com:8130/master/application-test.properties
访问地址 http://eureka-config-server.com:8130/master/test
证明配置服务中心可以获取配置信息。
客户端配置 Config Client
添加依赖包文件POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-config-client</artifactId>
<name>springcloud-config-client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
配置bootstrap.yml文件
spring:
application:
name: springcloud-config-client
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
profiles:
#读取测试环境配置文件
#active: test
#读取开发环境配置文件
#active: dev
#读取正式环境配置文件
active: pro
cloud:
config:
label: master
fail-fast: true
#指明配置服务中心的网址
uri: http://eureka-config-server.com:8130
server:
port: 8131
eureka:
instance:
hostname: eureka-config-client.com
instance-id: eureka-config-client
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
测试Config Client
启动项目springcloud-config-client
Config Client成功向Config Server读取了正式环境配置文件。
客户端配置 Config Client(高可用)
参考https://blog.csdn.net/miaodichiyou/article/details/104160284
修改springcloud-eureka-provider项目
依赖包文件POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-eureka-provider</artifactId>
<name>springcloud-eureka-provider</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
增加bootstrap.yml文件
spring:
application:
name: springcloud-eureka-provider
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
cloud:
config:
#profile: dev
label: master
fail-fast: true
#指明配置服务中心的网址
uri: http://eureka-config-server.com:8130
#discovery:
#service-id: springcloud-config-server
#enabled: true
eureka:
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
修改application-provider.yml
- application-provider1.yml
spring:
cloud:
config:
profile: dev
server:
port: 8001
eureka:
instance:
hostname: eureka-provider1.com
instance-id: eureka-provider1
- application-provider2.yml
spring:
cloud:
config:
profile: pro
server:
port: 8002
eureka:
instance:
hostname: eureka-provider2.com
instance-id: eureka-provider2
- application-provider3.yml 增加
spring:
cloud:
config:
profile: test
server:
port: 8003
eureka:
instance:
hostname: eureka-provider3.com
instance-id: eureka-provider3
增加文件
- ConfigProviderApplication.java
package org.springcloud.eureka.provider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ConfigProviderApplication {
@Value("${content}")
String content;
@Value("${server.port}")
String port;
@RequestMapping("/config")
public String Home(@RequestParam String name) {
return "Hello "+name+", This is from serverport:" + port+",content="+content;
}
public static void main(String[] args) {
SpringApplication.run(ConfigProviderApplication.class, args);
}
}
依次启动项目
springcloud-eureka-cluster-peer1
springcloud-eureka-cluster-peer2
springcloud-eureka-cluster-peer3
springcloud-config-server
springcloud-eureka-provider1
springcloud-eureka-provider2
springcloud-eureka-provider3
浏览器访问http://eureka-provider1.com:8001/config?name=zhaojq
浏览器访问http://eureka-provider2.com:8002/config?name=zhaojq
浏览器访问http://eureka-provider3.com:8003/config?name=zhaojq
Feign测试
参考https://blog.csdn.net/miaodichiyou/article/details/104304853
修改下列文件:
- FeignConsumer.java
package org.springcloud.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Component
/*指定这个接口所要调用的提供者服务名称 */
@FeignClient(value = "springcloud-eureka-provider",configuration = FeignConfig.class,fallback = FeignHystrix.class)
public interface FeignConsumer {
@GetMapping(value = "/hi")
String sayHiFromEurekaProvider(@RequestParam(value = "name")String name);
@GetMapping(value = "/config")
String getConfigFromConfigServer(@RequestParam(value = "name")String name);
}
- FeignController.java
package org.springcloud.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/*调用提供者的home方法*/
@RestController
public class FeignController {
@Autowired
FeignService feignService;
@GetMapping("/hi")
public String hi(@RequestParam(defaultValue = "zhaojq",required = false)String name){
return feignService.hi(name);
}
@GetMapping("/config")
public String config(@RequestParam(defaultValue = "zhaojq",required = false)String name){
return feignService.config(name);
}
}
- FeignService.java
package org.springcloud.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FeignService {
@Autowired
FeignConsumer feignConsumer;
public String hi(String name){
return feignConsumer.sayHiFromEurekaProvider(name);
}
public String config(String name){
return feignConsumer.getConfigFromConfigServer(name);
}
}
- FeignConsumer.java
package org.springcloud.feign;
import org.springframework.stereotype.Component;
@Component
public class FeignHystrix implements FeignConsumer {
@Override
public String sayHiFromEurekaProvider(String name) {
return "hi,"+name+", use Feign + hystrix, eureka-provider is down!";
}
@Override
public String getConfigFromConfigServer(String name) {
return "hi,"+name+", use Feign + hystrix, eureka-provider is down!";
}
}
启动springcloud-feign
在命令窗口curl http://eureka-feign.com:8101/config,发现Feign已经实现负载均衡