如何使用分布式配置中心

这里是修真院后端小课堂,每篇分享文从

八个方面深度解析后端知识/技能,本篇分享的是:

【如何使用分布式配置中心】

大家好,我是IT修真院深圳分院第十一期学员,一枚正直纯洁善良的JAVA程序员。

今天给大家分享一下,修真院官网JAVA任务十的一个知识点:如何使用分布式配置中心?

1 背景介绍

1.1 SpringCloud简介

springCloud是基于SpringBoot的一整套实现微服务的框架。他提供了微服务开发所需的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等组件。

SpringBoot旨在简化创建产品级的 Spring 应用和服务,简化了配置文件,使用嵌入式web服务器,含有诸多开箱即用微服务功能

2 知识剖析

2.1 什么是分布式配置中心?

在微服务架构的系统中,使用一个统一的微服务进行配置文件的加载,由这个微服务向其他服务提供配置文件,这个微服务就是配置中心。

2.2 什么是SpringCloudBus?

Spring cloud bus即SpringCloud框架的消息总线。其本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。利用bus的机制可以做很多的事情,其中配置中心客户端刷新就是典型的应用场景之一。

2.3 使用SpringCloudBus实现自动更新配置的步骤有哪些?

1) 设置配置中心,config-server,从git获取配置信息;

2) 在service中调用config-server中的相关配置;

3) 在service中导入SpringCloudBus相关依赖,配置RabbitMq;

4) 在需要刷新配置的Controller上添加@RefreshScope注解

5) 在GitHub上配置WebHook,设置更新properties文件后向config-server发送post请求。

3 常见问题

如何使用SpringCloudBus实现自动更新配置

4 解决方案

1、见编码实战

5 编码实战

config-server的yml配置

server:

port: 8091

eureka:

instance:

prefer-ip-address: true

client:

registerWithEureka: true

fetchRegistry: true

serviceUrl:

  defaultZone: http://localhost:8090/eureka/

spring:

application:

    name: config-server

cloud:

    config:

      server:

        git:

          uri: https://github.com/

          search-paths: respo

      label: master

    bus:

      enabled: true

      trace:

        enabled: true



rabbitmq:

  host: localhost

  port: 5672

  username: guest

  password: guest

management:

endpoints:

web:

  exposure:

    include: bus-refresh

service的yml配置

server:

port: 8081

spring:

application:

    name: service-hi

cloud:

  bus:

    enabled: true

    trace:

      enabled: true

  config:

    lable: master

    profile: dev

    uri: http://193.112.45.68:8091/

rabbitmq:

  host: 193.112.45.68

  port: 5672

  username: guest

  password: guest

eureka:

client:

registerWithEureka: true

fetchRegistry: true

serviceUrl:

  defaultZone: http://193.112.45.68:8090/eureka/

instance:

hostname: localhost

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class Hello {

@Value("${type}")

private String type;





@RequestMapping(value = "/hello")

public String te() {

    return type ;

}

}

import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RefreshScope

public class Hi {

@Value("${type}")

private String type;





@RequestMapping(value = "/hi")

public String te() {

    return type ;

}

}

6 扩展思考

7 参考文献

优快云、百度百科

8 更多讨论

8.1 @RefreshScope的作用是什么?

该注解用来标注需要刷新的类,只有加上了这个注解,才会对这个类所引用的配置进行刷新。

8.2 服务注册中心、服务配置中心和具体的服务模块是不是必须要放在同一个服务器上?

不需要,只需要在yml文件中指定地、开放相关的端口即可。

8.3 GitHub的webHook功能如何配置?

1) 找到连接的库,打开Setting页面;

2) 选择webHook,新增webhook;

3) 填写发送POST请求的地址及何时发送POST请求

技能树.IT修真院

“我们相信人人都可以成为一个工程师,现在开始,找个师兄,带你入门,掌控自己学习的节奏,学习的路上不再迷茫”。

这里是技能树.IT修真院,成千上万的师兄在这里找到了自己的学习路线,学习透明化,成长可见化,师兄1对1免费指导。

快来与我一起学习吧~http://www.jnshu.com/login/1/24864700

腾讯视频:https://v.qq.com/x/page/c0719197opo.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值