springcloud config配置中心

本文介绍了如何利用SpringCloudConfig搭建配置中心,通过将配置文件存储于Git仓库,实现配置的集中管理和动态更新。配置服务端连接Git仓库,各微服务作为客户端从配置服务端获取配置信息,避免了配置的重复修改,提高了代码的解耦性。
摘要由CSDN通过智能技术生成

springcloud config配置中心

​ 在实际开发中,每一个微服务都有一个配置文件,可能有多个微服务的配置文件是一样的,如果我们在代码里面把数据库等各种配置信息直接写死,那么将无法动态改变这些配置信息,并且每个配置文件都得重复修改,config配置中心就就是解决这一问题的,它允许我们使用git等平台来几种管理配置文件,我们只需要在git上建立一份包含微服务各个环境的配置文件,再建立一个专门连接git并获取配置信息的微服务(配置服务端),然后要使用配置信息的微服务(配置客户端)直接连接配置服务端即可获取相关配置信息,这样便不用再代码里面写死配置,实现了解耦。

image-20201015192728432

1.将springcloud注册中心eureka文章中7001注册中心的配置文件放到git上,首先新建一个配置服务端

image-20201015193216625

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.建立远程仓库并编写配置文件

image-20201015193449799

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配置中心的配置进行修改,实现配置的解耦

image-20201015193907357

4.测试

image-20201015194104085

image-20201015194125487

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值