Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端ConfigServer与客户端ConfigClient两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密 / 解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config 实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于 Spring 构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于 Spring Cloud Config 实现的配置中心默认采用 Git 来存储配置信息,所以使用 Spring Cloud Config 构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。
4.0.0
com.springcloud
springcloud-root
0.0.1-SNAPSHOT
springcloud-config-server
springcloud-config-server
http://maven.apache.org
UTF-8
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
junit
junit
3.8.1
test
org.springframework.boot
spring-boot-maven-plugin
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: [email protected]/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服务
https://git-scm.com/
git bash配置:
git config --global user.name "zhaojq"
git config --global user.email "[email protected]"
git config --global user.password "123456"
在启动类加上@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);
}
}
启动项目springcloud-config-server
http请求地址和资源文件映射如下:
访问地址 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
证明配置服务中心可以获取配置信息。
4.0.0
com.springcloud
springcloud-root
0.0.1-SNAPSHOT
springcloud-config-client
springcloud-config-client
http://maven.apache.org
UTF-8
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
junit
junit
3.8.1
test
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/
启动项目springcloud-config-client
Config Client成功向Config Server读取了正式环境配置文件。
参考https://blog.csdn.net/miaodichiyou/article/details/104160284
4.0.0
com.springcloud
springcloud-root
0.0.1-SNAPSHOT
springcloud-eureka-provider
springcloud-eureka-provider
http://maven.apache.org
UTF-8
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
junit
junit
3.8.1
test
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/
spring:
cloud:
config:
profile: dev
server:
port: 8001
eureka:
instance:
hostname: eureka-provider1.com
instance-id: eureka-provider1
spring:
cloud:
config:
profile: pro
server:
port: 8002
eureka:
instance:
hostname: eureka-provider2.com
instance-id: eureka-provider2
spring:
cloud:
config:
profile: test
server:
port: 8003
eureka:
instance:
hostname: eureka-provider3.com
instance-id: eureka-provider3
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
参考https://blog.csdn.net/miaodichiyou/article/details/104304853
修改下列文件:
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);
}
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);
}
}
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);
}
}
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已经实现负载均衡