springcloud config配置中心
在实际开发中,每一个微服务都有一个配置文件,可能有多个微服务的配置文件是一样的,如果我们在代码里面把数据库等各种配置信息直接写死,那么将无法动态改变这些配置信息,并且每个配置文件都得重复修改,config配置中心就就是解决这一问题的,它允许我们使用git等平台来几种管理配置文件,我们只需要在git上建立一份包含微服务各个环境的配置文件,再建立一个专门连接git并获取配置信息的微服务(配置服务端),然后要使用配置信息的微服务(配置客户端)直接连接配置服务端即可获取相关配置信息,这样便不用再代码里面写死配置,实现了解耦。
1.将springcloud注册中心eureka文章中7001注册中心的配置文件放到git上,首先新建一个配置服务端
server:
port: 3344
---
spring:
application:
name: springcloud-config-server
cloud:
config:
#连接远程仓库
server:
git:
uri: https://gitee.com/jianxiaojianghu/springcloud-config.git #HTTPS
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.huang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-server-3344</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
@SpringBootApplication
@EnableConfigServer
public class Config_Server_3344 {
public static void main(String[] args) {
SpringApplication.run(Config_Server_3344.class,args);
}
}
2.建立远程仓库并编写配置文件
spring:
profiles:
active: dev
---
server:
port: 7001
spring:
profiles: dev
application:
name: springcloud-eureka
#eureka配置
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #false表示自己为注册中心
service-url: #监控地址
defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7002.com:7002/eureka/
---
server:
port: 7002
spring:
profiles: test
application:
name: springcloud-eureka
#eureka配置
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #false表示自己为注册中心
service-url: #监控地址
defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7002.com:7002/eureka/
3.对7001配置中心的配置进行修改,实现配置的解耦
4.测试