SpringCloud--Config+Bus--分布式配置中心

1、Config–分布式配置中心

1.1、什么是配置中心?

​ 对于一个微服务架构的应用来说,每一个服务都有自己的一个配置文件,当微服务的数量比较多时,如果修改一些公用的配置,则需要将所有的配置文件都单独进行修改,这对运维人员来说是非常耗时耗力的。那么有没有一种方法,可以将众多微服务中公用的配置信息抽取出来,放到一个地方集中管理,然后各个微服务再去这个地方拉取对应的配置信息,达到公用配置统一管理的目的。所以配置中心就出现了。

1.2、SpringCloud Config

​ 互联网行业常用的配置中心有SpringCloud Config、Alibaba Nacos、携程的Apollo等,这里只说明Config的使用方式。

​ SpringCloud Config采用C/S架构,为微服务架构中的微服务提供集中化的外部配置支持,分为服务端和客户端。官方文档:

​ https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

​ 服务端:也称分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息的访问接口。配置服务器默认采用git来储存配置信息,这样有助于对环境配置进行版本控制,并且可以通过git客户端工具来方便的管理和访问配置内容。而Config Server只需要和git配置服务器连接就行了。

​ 客户端:需要获取配置的各个微服务,在启动的时候从服务端获取指定的配置信息。
在这里插入图片描述

1.3、Config服务端配置与测试

​ 因为Config Server默认采用git来储存配置信息,所以此处选择github作为储存服务器,并与之整合。

​ 第一步:环境准备

​ 首先,在github上新建springcloud-config仓库并创建如下文件:
在这里插入图片描述

​ 注意:一般情况下,我们都是使用application-profile.yml/properties的方式命名配置文件!

​ 配置文件中书写一下测试内容:

#dev环境
config:
	info: "master branch,springcloud-config/appname-dev.yml version=1"
#测试环境
config:
	info: "master branch,springcloud-config/appname-test.yml version=1"
#生产环境
config:
	info: "master branch,springcloud-config/appname-prod.yml version=1"

​ 然后,从github上下载刚才创建好的仓库,命令(SSH方式):

git clone [email protected]:XXXXXX/springcloud-config.git

​ 得到下列目录:
在这里插入图片描述

​ 第二步:创建Config Server模块

​ pom:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

​ application.yml:

server:
  port: 3344
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          #配置第一步中创建的仓库地址(http方式)
          uri: https://github.com/dengbuquan/springcloud-config.git
          #搜索目录
          search-paths:
            - springcloud-config
      #读取分支
      label: master
#注册进Eureka
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

​ 主启动类:

@SpringBootApplication
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# Spring Cloud Examples Spring Cloud 使用的各种示例,以最简单、最实用为标准 [Spring Cloud 中文索引](https://github.com/ityouknow/awesome-spring-cloud)  |   [Spring Boot学习示例代码](https://github.com/ityouknow/spring-boot-examples)  |   [参与贡献](https://github.com/ityouknow/spring-cloud-examples/issues) [English](README_EN.md)  |   **[github地址](https://github.com/ityouknow/spring-cloud-examples)**  |   **[码云地址](https://gitee.com/ityouknow/spring-cloud-examples)** Spring Cloud 使用的各种示例,以最简单、最实用为标准 **[Spring Boot 2.0 最全使用教程](https://github.com/ityouknow/spring-boot-leaning)** - [spring-cloud-eureka](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-eureka):eureka server单机、双机、集群示例 - [eureka-producer-consumer](https://github.com/ityouknow/spring-cloud-examples/tree/master/eureka-producer-consumer):利用eureka实现服务提供与调用示例 - [spring-cloud-hystrix](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-hystrix):Hystrix熔断的使用示例 - [hystrix-dashboard-turbine](https://github.com/ityouknow/spring-cloud-examples/tree/master/hystrix-dashboard-turbine):熔断监控Hystrix Dashboard和Turbine的示例 - [spring-cloud-config-git](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-config-git):配置中心git版本示例 - [spring-cloud-config-svn-refresh](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-config-svn-refresh):配置中心svn版本示例,客户端refresh版本示例 - [spring-cloud-config-eureka](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-config-eureka):配置中心服务化和高可用代码示例 - [spring-cloud-config-eureka-bus](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-config-eureka-bus):配置中心和消息总线示例(配置中心终结版) - [gateway-service-zuul](https://github.com/ityouknow/spring-cloud-examples/tree/master/gateway-service-zuul):Spring Cloud Zuul使用初级篇 网关 均衡负载 - [spring-cloud-zuul](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-zuul):Spring Cloud Zuul使用高级篇 Filter 鉴权 熔断 重试 - [spring-cloud-sleuth-zipkin](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-sleuth-zipkin): 利用Sleuth、Zipkin对Spring Cloud应用进行服务追踪分析 - [spring-boot-admin-eureka](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-boot-admin-eureka): 使用Spring Boot Admin 对Spring Cloud集群进行监控示例 - [spring-cloud-consul](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-consul): Spring Cloud 使用 Consul 作为服务中心示例 - [spring-cloud-gateway](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-gateway): Spring Cloud 使用 gateway 的相关示例 学习系列: - [springcloud(一):大话Spring Cloud](http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html) - [springcloud(二):注册中心Eureka](http://www.ityouknow.com/springcloud/2017/05/10/springcloud-eureka.html) - [springcloud(三):服务提供与调用](http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html) - [springcloud(四):熔断器Hystrix](http://www.ityouknow.com/springcloud/2017/05/16/springcloud-hystrix.html) - [springcloud(五):熔断监控Hystrix Dashboard和Turbine](http://www.ityouknow.com/springcloud/2017/05/18/hystrix-dashboard-turbine.html) - [springcloud(六):配置中心git示例](http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html) - [springcloud(七):配置中心svn示例和refresh](http://www.ityouknow.com/springcloud/2017/05/23/springcloud-config-svn-refresh.html) - [springcloud(八):配置中心服务化和高可用](http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html) - [springcloud(九):配置中心和消息总线(配置中心终结版)](http://www.ityouknow.com/springcloud/2017/05/26/springcloud-config-eureka-bus.html) - [springcloud(十):服务网关zuul](http://www.ityouknow.com/springcloud/2017/06/01/gateway-service-zuul.html) - [springcloud(十一):服务网关Zuul高级篇](http://www.ityouknow.com/springcloud/2018/01/20/spring-cloud-zuul.html) - [springcloud(十二):使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪](http://www.ityouknow.com/springcloud/2018/02/02/spring-cloud-sleuth-zipkin.html) - [springcloud(十三):Spring Cloud Consul 使用详解](http://www.ityouknow.com/springcloud/2018/07/20/spring-cloud-consul.html) - [Spring Cloud (十四):Spring Cloud 开源软件都有哪些?](http://www.ityouknow.com/springcloud/2018/08/06/spring-cloud-open-source.html) - [springcloud(十五):服务网关 Spring Cloud GateWay 初级篇](http://www.ityouknow.com/springcloud/2018/12/12/spring-cloud-gateway-start.html) - [springcloud(十六):服务网关 Spring Cloud GateWay 服务化和过滤器](http://www.ityouknow.com/springcloud/2019/01/19/spring-cloud-gateway-service.html) - [springcloud(十七):服务网关 Spring Cloud GateWay 熔断、限流、重试](http://www.ityouknow.com/springcloud/2019/01/26/spring-cloud-gateway-limit.html) 综合篇: - **[Spring Cloud在国内中小型公司能用起来吗?](http://www.ityouknow.com/springcloud/2017/09/11/can-use-springcloud.html)** - **[中小型互联网公司微服务实践-经验和教训](http://www.ityouknow.com/springcloud/2017/10/19/micro-service-practice.html)** - **[从架构演进的角度聊聊Spring Cloud都做了些什么?](http://www.ityouknow.com/springcloud/2017/11/02/framework-and-springcloud.html)**

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值