栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring cloud config 配置中心 &bus 消息总线&stream消息驱动

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring cloud config 配置中心 &bus 消息总线&stream消息驱动

目录

1.配置中心介绍

2.配置中心搭建

2.1GitHub上创建配置仓库,clone到本地;

2.2pom中添加:

2.3application.yml配置:

2.4主启动类上加@EnableConfigServer

2.5配置读取规则

3.config客户端配置

3.1 pom 配置

3.2 bootstrap.yml配置

4.config动态刷新

4.1 pom 配置:

4.2 暴露监控断点

4.3 配置类上增加@refreshScope注解

4.4需要运维人员发送post请求刷新

5.bus消息总线概述

5.1概述

5.2.configServer端添加消息总线

6.spring cloud stream消息驱动

6.1pom 配置:

6.2yml配置:

6.3 代码

6.4 yml配置

6.5 代码

6.6 消息重复消费



1.配置中心介绍

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态配置管理设施是必不可少的。springcloud提供了Config Server来解决这个问题,

spring cloud config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

spring cloud config 分为服务端和客户端两部分。

服务端也称为分布式配置中心,是一个独立的微服务,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

2.配置中心搭建

2.1GitHub上创建配置仓库,clone到本地;

2.2pom中添加:

    org.springframework.cloud
    spring-cloud-config-server

2.3application.yml配置:
spring:
    cloud:
        config:
            server:
                git:
                  uri: git@github.com:xxxx/springcloud-config.git   git上的仓库名字
                  search-paths:
                       - springcloud-config
            label: master

2.4主启动类上加@EnableConfigServer

2.5配置读取规则

/{label}/{application}-{profile}.yml

3.config客户端配置

3.1 pom 配置

    org.springframework.cloud
    spring-cloud-starter-config

application.yml是用户级别的资源配置项

bootstrap.yml是系统级的,优先级更加高

spring cloud 会创建一个bootstrap context,作为spring 应用的application context的父上下文,初始化的时候,bootstrap context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。

Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖,Bootstrap context和application context有着不同的约定,所以新增一个bootstrap.yml文件,保证bootstrap context 和application context配置的分离。

3.2 bootstrap.yml配置
spring:
    cloud:
        config:
            label: master
            name: config
            profile: dev
            uri: http://localhost:3344

4.config动态刷新

4.1 pom 配置:

    org.springframework.boot
    spring-boot-starter-actuator

加这个的目的是,项目发生了变化,能够被外部监测到

4.2 暴露监控断点
management:
    endpoints:
        web:
          exposure:
            include: "*"

4.3 配置类上增加@refreshScope注解

4.4需要运维人员发送post请求刷新

curl -X POST "http://localhost:3355/actuator/refresh"

每次更改,都需要执行该请求,比较烦

5.bus消息总线概述

5.1概述

分布式自动刷新配置功能:spring cloud bus配合spring cloud config使用可以实现配置的动态刷新

bus支持2种消息代理:rabbitmq 和kafaka

spring cloud bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改,事件推送等,也可以当做微服务间的通信通道

总线:在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。

设计思想:利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置

5.2.configServer端添加消息总线

pom 配置:


   org.springframework.cloud
   spring-cloud-starter-bus-amqp


yml增加rabbitmq配置
rabbitmq:
    host: locahost
    port: 5672
    username: guest
    password: guest
暴露bus刷新配置的端点:
 management:
    endpoints:
        web:
          exposure:
            include: 'bus-refresh'

全局通知:curl -X POST  "http://localhost:3344/actuator/bus-refresh"

定点通知的话:curl -X POST "http//localhost:3344/actuator/bus-refresh/config-client:3355" config-client为服务名 3355 为端口

6.spring cloud stream消息驱动

消息驱动-生产者

6.1pom 配置:

    org.springframework.cloud
    spring-cloud-starter-stream-rabbit

6.2yml配置:
spring:
    cloud:
        stream:
            binders:
                defaultRabbit:
                    type:rabbit
                    environment:
                        spring:
                            rabbitmq:
                                host: localhost
                                port: 5672
                                username: guest
                                password: guest
           bindings:
                output:
                    destination: studyExchange #表示要使用的Exchange名称定义
                    content-type: application/json #设置消息类型,文本则设置为text/plain
                    binder: defaultRabbit

6.3 代码
@EnableBinding(Source.class)
public class  MessageProviderImpl implements IMessageProvider{
    
    @Resource
    private MessageChannel output;

    @Override
    public  String send(){
            
        output.send(MessageBuilder.withPayload("xx").build())

        return null;    
    }
}

消息驱动-消费者

6.4 yml配置
spring:
    cloud:
        stream:
            binders:
                defaultRabbit:
                    type:rabbit
                    environment:
                        spring:
                            rabbitmq:
                                host: localhost
                                port: 5672
                                username: guest
                                password: guest
           bindings:
                input:
                    destination: studyExchange #表示要使用的Exchange名称定义
                    content-type: application/json #设置消息类型,文本则设置为text/plain
                    binder: defaultRabbit

6.5 代码
 
@Component
@EnableBinding(Sink.class)
public class ReceiveMessageListenerController{
   private String serverPort;
  
  @StreamListener(Sink.INPUT)
  public void input(Message message){

        System.out.println(message.getPayload());
    }

}

6.6 消息重复消费

Stream中处于同一个group中的多个消费者是竞争关系,就能够保证消息只会被一个应用消费一次。

配置group 防止重复消费

spring:
    cloud:
        stream:
            binders:
                defaultRabbit:
                    type:rabbit
                    environment:
                        spring:
                            rabbitmq:
                                host: localhost
                                port: 5672
                                username: guest
                                password: guest
           bindings:
                input:
                    destination: studyExchange #表示要使用的Exchange名称定义
                    content-type: application/json #设置消息类型,文本则设置为text/plain
                    binder: defaultRabbit
                    group: testA

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/724388.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号