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

13.SpringCloud config分布式配置中心和14.SpringCloud Bus 消息总线和16.SpringCloud Sleuth分布式请求链路追踪

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

13.SpringCloud config分布式配置中心和14.SpringCloud Bus 消息总线和16.SpringCloud Sleuth分布式请求链路追踪

1.SpringCloud config概述 1.1 背景

1.2 是什么?

1.3 怎么玩?

1.4能干嘛?

与Github整合配置:

由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)

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

2. Config服务端配置与测试 2.1 git项目准备

用你自己的账号在gitee上新建一个名为sprincloud-config的新Repository,然后克隆到本地

git clone https://gitee.com/kutangguobuku/sprincloud-config.git

克隆到本地后,新建三份配置文件,新增配置,然后提交到gitee
git add config-dev.yml config-test.yml config-prod.yml
git commit -m ‘初始化配置文件’
git push origin master

2.2 新建Module模块cloud-config-center-3344做为Cloud的配置中心模块 2.2.1 改pom

        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
            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
        
    
2.2.2 改yml
server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri:  填写你自己的github路径
          search-paths:
            - springcloud-config
      label: master
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka
2.2.3 新建配置类 ConfigCenterMain3344

@EnableConfigServer 代表开启配置中心

@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
            SpringApplication.run(ConfigCenterMain3344 .class,args);
        }
}

windows下修改hosts文件,增加映射 127.0.0.1 config-3344.com

2.2.4 测试通过Config微服务是否可以从Github上获取配置内容

启动7001,启动3344,访问http://config-3344.com:3344/master/config-dev.yml

3. Config客户端配置与测试 3.1 新建cloud-config-client-3355 3.2 改pom

        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
            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
        
    
3.3 改bootstrap.yml

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

3.3 主启动类与业务类
@SpringBootApplication
public class ConfigClientMain3355 {
    public static void main(String[] args) {
            SpringApplication.run( ConfigClientMain3355.class,args);
        }
}
@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

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

http://config-3344.com:3344/master/config-dev.yml
http://localhost:3355/configInfo

3.5 Config客户端之动态刷新

避免每次更新配置都要重启客户端微服务3355
POM引入actuator监控
修改YML,暴露监控端口
业务类Controller加注解@RefreshScope
修改后发post请求:curl -X POST “http://localhost:3355/actuator/refresh”

management:
  endpoints:
    web:
      exposure:
        include: "*"
4.SpringCloud Bus概述 4.1 是什么?能干嘛?

Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新

Bus支持两种消息代理:RabbitMQ和Kafka

4.2 RabbitMQ环境配置

安装Erlang,选择安装盘符后一路next,下载地址:http://erlang.org/download/otp_win64_21.3.exe
安装RabbitMQ,选择安装盘符后一路next,下载地址:https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe
进入RabbitMQ安装目录下的sbin目录,输入以下命令启动管理功能:rabbitmq-plugins enable rabbitmq_management
访问地址查看是否安装成功:http://localhost:15672/
输入账号密码并登录: guest guest

4.3 新建modulecloud-config-client-3366 4.3.1改pom

    
        org.springframework.cloud
        spring-cloud-starter-config
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
        com.atguigu.springcloud
        cloud-api-commons
        ${project.version}
    
    
        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
    

4.3.2 改yml
server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"
4.3.3 启动类与业务类
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
    public static void main(String[] args) {
            SpringApplication.run( ConfigClientMain3366.class,args);
        }
}
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${server.port}")
    private String serverPort;

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return "serverPort:"+serverPort+"tnn configInfo: "+configInfo;
    }
}
4.3.4 设计思想

1)利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置

  1. 利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置(更加推荐)
    图二的架构显然更加合适,图一不适合的原因如下:
    a.打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新职责
    b.破坏了微服务各节点的对等性
    c.有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改
4.3.5给3344、3355、3366添加消息总线支持

1)给cloud-config-center-3344配置中心服务端添加消息总线支持


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

在spring层级下:

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

给cloud-config-center-3355客户端添加消息总线支持


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

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

给cloud-config-center-3366客户端添加消息总线支持


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

  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
4.3.6测试

4.3.7SpringCloud Bus动态刷新定点通知

curl -X POST “http://localhost:3344/actuator/bus-refresh/config-client:3355”

通知总结:

5.SpringCloud Sleuth分布式请求链路追踪

15.SpringCloud Stream消息驱动现实中用的并不多,一般都是直接用消息中间件。故笔记省略。

5.1概述

为什么会出现这个技术?需要解决哪些问题?
Spring Cloud Sleuth提供了一套完整的服务跟踪的解决方案
在分布式系统中提供追踪解决方案并且兼容支持了zipkin
源码:https://github.com/spring-cloud/spring-cloud-sleuth

5.2搭建链路监控步骤 5.2.1下载安装zipkin

SpringCloud从F版起已不需要自己构建Zipkin server了,只需要调用jar包即可
下载地址:https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
zipkin-server-2.12.9.exec.jar

java -jar zipkin-server-2.12.9-exec.jar

图形化界面:http://localhost:9411/zipkin/

5.2.2.修改服务提供者cloud-provider-payment8001

        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        

在spring标签下添加

zipkin:
  base-url: http://localhost:9411
  sleuth:
    sampler:
    probability: 1
    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        return "hi ,i'am paymentzipkin server fall back,welcome to atguigu,O(∩_∩)O哈哈~";
    }
5.2.3 3.修改服务消费者(调用方)cloud-consumer-order80
 
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
 zipkin:
    base-url: http://localhost:9411
    sleuth:
      sampler:
      probability: 1
 // ====================> zipkin+sleuth
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }

依次启动eureka7001/8001/80,打开浏览器访问:http:localhost:9411,会出现以下界面

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

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

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