码迷,mamicode.com
首页 > 编程语言 > 详细

springcloud(三)-Config&Bus&Stream&Sleuth+Zipkin

时间:2020-12-14 12:58:18      阅读:4      评论:0      收藏:0      [点我收藏+]

标签:管理   discover   bindings   provider   void   发送   信息   cal   master   

SpringCloud-config
SpringCloud-bus
SpringCloud-stream
SpringCloud-sleuth、zipkin


## Config

> 概念:分布式配置中心

> 作用:统一管理各个服务、环境配置、实现动态配置(优点:维护方便、灵活度比较高)
>
> ![image-20200605101411241](img\image-20200605101411241.png)

### 使用步骤

#### 第一步:搭建配置中心微服务

准备阶段:在“码云”创建公共仓库,存放“配置文件”

```tex
配置文件的命名方式:{application}-{profile}.yml 或 {application}-{profile}.properties
application为应用名称
profile用于区分开发环境dev,测试环境test、生产环境prod等

1)导入起步依赖

<!--Eureka客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--Config配置依赖包-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

2)配置--->要配置到bootstrap.yml

server:
  port: 12000
spring:
  application:
    name: config-server
  cloud:
    config: #config相关配置
      server:
        git:
          uri: https://gitee.com/goheima/heima-config.git
      label: master
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

3)代码

@SpringBootApplication
@EnableConfigServer //开启配置服务
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
特点:”配置中心微服务“可以动态获取“仓库中”的配置

第二步:改造微服务加载配置中心配置

准备阶段:把该微服务配置文件上传到“第一步”中创建的仓库里

配置文件的命名方式:{application}-{profile}.yml 或 {application}-{profile}.properties
application为应用名称
profile用于区分开发环境dev,测试环境test、生产环境prod等

1)导入config起步依赖包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

2)配置

#删除原来的application.yml,新建bootstrap.yml配置文件,并进行如下配置
spring:
  cloud:
    config:
      # 要与仓库中的配置文件的application保持一致
      name: user
      # 要与仓库中的配置文件的profile保持一致
      profile: dev
      # 要与仓库中的配置文件所属的版本(分支)一样
      label: master
      discovery:
        # 使用配置中心
        enabled: true
        # 配置中心服务名
        service-id: config-server
        #根据上述service-id、label、name、profile去加载配置文件
        #去"仓库"-master分支查找user-dev.yml配置文件
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

3)代码(省略,不需要代码支持)

特点:”微服务“不可以动态获取“仓库中”的配置。
	原因:“微服务”只会在启动时,去“配置中心微服务”加载一次配置

Bus

概念:基于消息中间件(RabbitMQ、Kafka)消息总线

作用:消息传递;服务监控

Config+Bus实现动态配置使用步骤

第一步:改造配置中心微服务

1)导包

<!--bus依赖包-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-bus</artifactId>
</dependency>
<!--bus底层实现mq依赖包-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

2)配置

#在原来配置的基础上添加MQ连接配置、动态刷新配置文件的配置
spring:
  # 配置rabbitmq信息;如果是都与默认值一致则不需要配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#动态刷新配置文件的配置
management:
  endpoints:
    web:
      exposure:
        # 暴露触发消息总线的地址
        include: bus-refresh

3)代码

//不需要代码支持,代码省略
//需要手动执行请求刷新,动态刷新配置(也可以使用定时任务,定时刷新配置)
/*
请求路径:http://注册中心微服务地址:端口/actuator/bus-refresh
请求方式:post
要求:"bus-refresh"和配置文件中"暴露触发消息总线的地址"保持一致
*/

第二步:改造微服务

1)导包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2)配置

#在原来配置的基础上添加MQ连接配置
spring:
  # 配置rabbitmq信息;如果是都与默认值一致则不需要配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

3)代码

//在需要动态获取配置文件的处理类中添加@RefreshScope注解(刷新配置)

Stream

概念:消息驱动组件

作用:无感知(操作springCloud封装的stream上层接口,感知不到底层具体实现)的操作消息

stream支持RabbitMQ、Kafka

角色:消息生产者、消息平台、消息消费者

使用步骤

消息生产者:

1)导包

<!--spring boot web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- stream -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

2)配置

server:
  port: 8000
spring:
  cloud:
    stream:
      # 定义绑定器,绑定到哪个消息中间件上
      binders:
        itheima_binder: # 自定义的绑定器名称
          type: rabbit # 绑定器类型
          environment: # 指定mq的环境
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
                virtual-host: /
      bindings:
        output: # channel名称
          binder: itheima_binder #指定使用哪一个binder
          destination: itheima_exchange # 消息目的地

3)代码

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
@Component
@EnableBinding(Source.class) //开启绑定
public class MessageProducer {

    @Autowired
    private MessageChannel output;

    public void send(){
        String msessage = "hello stream~~~";
        //发送消息
        output.send(MessageBuilder.withPayload(msessage).build());
        System.out.println("消息发送成功~~~");

    }
}

消息消费方:

1)导包(参考生产方)

2)配置(参考生产方)

3)代码(待完成)

Sleuth+Zipkin

作用:链路追踪(想追踪哪些模块,在哪些模块配置)

优点:1)调试方便 2)针对整个链路中性能低的节点进行优化
总结就是:Sleuth是来跟踪捕捉数据的,Zipkin是用来收集展示数据的

使用步骤:

准备工作:搭建zipkin服务端

# 在zipkin所在包,打开命令窗口,执行下面命令,启动zipkin服务器
java -jar zipkin-server-2.12.9-exec.jar

1)导包

<!--导入zipkin依赖包即可,内置了Sleuth-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

2)配置

server:
  port: 8001

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: feign-provider
  zipkin:
    base-url: http://localhost:9411/  # 设置zipkin的服务端路径

  sleuth:
    sampler:
      probability: 1 # 采集率 默认 0.1 百分之十。

3)代码

//不需要代码支持

实际项目访问流程

前端(PC、APP、小程序)--->nginx(高可用,内网的一台服务器,设置了公网ip+域名)-->网关(集群,内网的一台服务器,设置了公网ip+域名)-->内部服务(内网)

技术图片

1)Gateway全局过滤器、局部过滤器
2)使用Config、Bus组件,实现动态配置(前提:RabbitMQ安装成功并成功启动)
3)Sleuth+Zipkin实现链路追踪

springcloud(三)-Config&Bus&Stream&Sleuth+Zipkin

标签:管理   discover   bindings   provider   void   发送   信息   cal   master   

原文地址:https://www.cnblogs.com/zhang-blog/p/14100583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!