微服务入门之config+bus

一、前言

1.1、分布式面临的问题

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理……/(ㄒoㄒ)/~~

1.2、config 和 bus 的概述

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config分为服务端和客户端两部分。服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

微服务入门之config+bus

Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动态刷新。
Bus支持两种消息代理:RabbitMQ 和 Kafka

微服务入门之config+bus

二、实操

2.1、config配置

2.1.1、Config服务端配置与测试

  1. 新建Module模块cloud-config-center-3344它即为Cloud的配置中心模块cloudConfig Center
  2. POM

        mscloud
        com.atguigu.springcloud
        1.0-SNAPSHOT

    4.0.0

    cloud-config-center-3344

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

            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client

            org.springframework.boot
            spring-boot-starter-web

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

            org.springframework.boot
            spring-boot-devtools
            runtime
            true

            org.projectlombok
            lombok
            true

            org.springframework.boot
            spring-boot-starter-test
            test

  1. YML
server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: git@github.com:zzyybs/springcloud-config.git #GitHub上面的git仓库名字
        ####搜索目录
          search-paths:
            - springcloud-config
      ####读取分支
      label: master

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  1. 主启动类
package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * @auther zzyy
 * @create 2019-11-15 14:32
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344
{
    public static void main(String[] args) {
            SpringApplication.run(ConfigCenterMain3344.class, args);
    }
}
  1. windows下修改hosts文件,增加映射
    127.0.0.1 config-3344.com
  2. 测试通过Config微服务是否可以从GitHub上获取配置内容
    启动微服务3344
    访问http://config-3344.com:3344/master/config-dev.yml

2.1.2、Config客户端配置与测试

  1. 新建cloud-config-client-3355
  2. POM

        mscloud
        com.atguigu.springcloud
        1.0-SNAPSHOT

    4.0.0

    cloud-config-client-3355

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

            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client

            org.springframework.boot
            spring-boot-starter-web

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

            org.springframework.boot
            spring-boot-devtools
            runtime
            true

            org.projectlombok
            lombok
            true

            org.springframework.boot
            spring-boot-starter-test
            test

  1. bootstrap.yml
    为什么有了application.yml还要bootstrap.yml?

applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高
要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,
因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  1. 修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version
  2. 主启动类
package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @auther zzyy
 * @create 2020-02-08 11:09
 */
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355
{
    public static void main(String[] args)
    {
        SpringApplication.run(ConfigClientMain3355.class,args);
    }
}
  1. 业务类
package com.atguigu.springcloud.controller;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;

/**
 * @auther zzyy
 * @create 2019-11-15 15:00
 */
@RestController
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return configInfo;
    }
}
  1. 测试
    启动Config配置中心3344微服务并自测
    启动3355作为Client准备访问http://localhost:3355/configInfo
    成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息

2.1.3、分布式配置的动态刷新问题

修改GitHub上的配置文件,刷新3344发现ConfigServer配置中心立刻响应
刷新3355发现ConfigClient客户端没有任何响应,重启3355,才得到修改后的配置。
难到每次运维修改配置文件,客户端都需要重启??噩梦?引入@RefreshScope注解

2.1.4、修改3355模块

1、POM引入actuator监控


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

2、修改YML,暴露监控端口

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取
      uri: http://localhost:3344 #配置中心地址k

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3、@RefreshScope业务类Controller修改

package com.atguigu.springcloud.controller;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;

/**
 * @auther zzyy
 * @create 2019-11-15 15:00
 */
@RestController
@RefreshScope
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}

4、需要运维人员发送Post请求刷新3355
curl -X POST “http://localhost:3355/actuator/refresh

2.2、bus配置

1、pom


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

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

2、yml

rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

##rabbitmq相关配置,暴露bus刷新配置的端点
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'

3、测试
修改Github上配置文件增加版本号
curl -X POST “http://localhost:3344/actuator/bus-refresh” 一次发送,处处生效
获取配置信息,发现都已经刷新了

三、总结

微服务入门之config+bus

Original: https://www.cnblogs.com/lovexiao/p/16266418.html
Author: 潇潇love涛涛
Title: 微服务入门之config+bus

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/593147/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Spring(五):IoC创建对象的方式

    一、bean被创建的时间 考虑一个问题,我们都知道spring通过xml的配置创建bean,那么bean是什么时间被创建的呢?是在我们getBean()的时候创建的吗? 我们来做一…

    Java 2023年6月15日
    088
  • mongodb在双活(主备)机房的部署方案和切换方案设计

    概述 现在很多高可用系统为了应对极端情况,比如主机宕机、网络故障以及机房宕机等灾难的发生,通常会部署主备架构(双机房),或者双活架构(双机房),甚至多活架构(三个机房或者以上),m…

    Java 2023年6月6日
    078
  • 深入浅出Nginx实战与架构

    本文主要内容如下(让读者朋友们深入浅出地理解Nginx,有代码有示例有图): 1.Nginx是什么? 2.Nginx具有哪些功能? 3.Nginx的应用场景有哪些? 4.Nginx…

    Java 2023年5月30日
    068
  • jdk8系列二、jdk8方法引用、重复注解、更好的类型推断、新增注解

    一、方法引用 方法引用使得开发者可以直接引用现存的方法、Java类的构造方法或者实例对象。方法引用和Lambda表达式配合使用,使得java类的构造方法看起来紧凑而简洁,没有很多复…

    Java 2023年5月30日
    084
  • Java之万年历

    @ 二、Java之万年历 2.1 要求 2.2 思路 2.3 源代码 2.4 结果截图 二、Java之万年历 2.1 要求 输入年份; 输入月份; 输出某年某月的日历。 2.2 思…

    Java 2023年6月5日
    095
  • dubbo源码分析4(spring配置文件解析机制)

    我们知道dubbo一般也不会单独使用的吧,都会和spring一起使用,知道为什么吗? 因为dubbo是基于spring的扩展机制进行扩展的,所以首先我们要知道spring提供了一种…

    Java 2023年6月6日
    077
  • 标准输出流与日志文件

    一、PrintStream 标准的字节输出流,默认输出到控制台 1.构造方法 PrintStream(File file) 使用指定的文件创建一个新的打印流,而不需要自动换行。 P…

    Java 2023年6月9日
    069
  • jQuery常使用的方法

    jQuery常使用的方法 1,jQuery中正则表达式的使用 var reg = /^(([1-9]\d*)|0)$/ ; if (!reg.test(createCost)){ …

    Java 2023年6月5日
    0101
  • 重启rabbitmq服务

    重启rabbitmq服务通过两个命令来实现: rabbitmqctl stop :停止rabbitmq rabbitmq-server restart : 重启rabbitmq 因…

    Java 2023年5月30日
    0128
  • 全局字体设置 || 老年模式

    通过设置字号,同步改变全局的字体。长文干货,建议点赞收藏。 实现方式有多种: 1.通过AppTheme主题设置 通过配置不同的字体主题,然后设置更换主题来改变全局的字体样式,主题中…

    Java 2023年6月7日
    0102
  • Vue3+Vue-cli4项目中使用腾讯滑块验证码

    Vue3+Vue-cli4项目中使用腾讯滑块验证码 简介: 滑块验证码相比于传统的图片验证码具有以下优点: 验证码的具体验证不需要服务端去验证,服务端只需要核验验证结果即可。 验证…

    Java 2023年6月8日
    069
  • 8.Hystrix隔离术

    Hystrix隔离之ThreadPoolKey Hystrix可以不填写ThreadPoolKey 默认Hystrix会使用GroupKey命名线程池 在Setting中加入and…

    Java 2023年6月8日
    085
  • 30个类手写Spring核心原理之动态数据源切换(8)

    本文节选自《Spring 5核心原理》 阅读本文之前,请先阅读以下内容: 30个类手写Spring核心原理之自定义ORM(上)(6) 30个类手写Spring核心原理之自定义ORM…

    Java 2023年6月7日
    094
  • Lambda-让人又爱又恨的“->”

    写在前边 聊到Java8新特性,我们第一反应想到的肯定是Lambda表达式和函数式接口的出现。要说ta到底有没有在一定程度上”优化”了代码的简洁性呢?抑或是…

    Java 2023年6月5日
    081
  • JavaScript基本语法

    JavaScript(js)注释 js的注释非常简单,就和java一样, //, /**/,分别对应多行注释和单行注释,当然要在 <script>….</sc…

    Java 2023年6月5日
    085
  • test

    test posted @2022-05-03 22:29 且吃茶去 阅读(24 ) 评论() 编辑 //my Original: https://www.cnblogs.com/…

    Java 2023年6月8日
    075
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球