22-springCloud-day03

一. 配置中心config

1.1 config概述

在这里插入图片描述

• Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护。
• 好处:
集中管理配置文件
不同环境不同配置,动态化的配置更新
配置信息改变时,不需要重启即可更新配置信息到服务

1.2 config快速入门

1.2.1 gitee搭建远程仓库

1.编写仓库名称、仓库路径、公开(公开的比较方便)

在这里插入图片描述

2.语言和模板可以不选,一般使用单分支模型,只创建master分支

在这里插入图片描述

3.使用小乌龟工具,将远程仓库clone到本地

在这里插入图片描述

4.使用小乌龟工具将配置文件提交到远程仓库

在这里插入图片描述

在这里插入图片描述

1.2.2 config server搭建

导入依赖
<dependencies>
    <!-- config-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>
编写配置

设置 gitee 远程仓库地址

server:
  port: 9527
spring:
  application:
    name: config-server
  # spring cloud config
  cloud:
    config:
      server:
        # git 的 远程仓库地址
        git:
          uri: https://gitee.com/itheima_cch/itheima-configs.git
      	  label: master # 分支配置
编写启动引导类
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class,args);
    }
}
测试访问

在这里插入图片描述

1.2.3 config client搭建

导入依赖
<!--config client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
配置config

配置config server 地址,读取配置文件名称等信息

创建配置文件bootstrap.yml

# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
  cloud:
    config:
      # 配置config-server地址
      uri: http://localhost:9527
      # 配置获得配置文件的名称等信息
      name: config # 文件名
      profile: dev # profile指定,  config-dev.yml
      label: master # 分支
获取配置值
@Value("${itheima}")
private String itheima;
启动测试

在这里插入图片描述

1.2.4 config client刷新

在 config 客户端引入 actuator 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

获取配置信息类上,添加 @RefreshScope 注解

/**
 * Goods Controller 服务提供方
 */

@RestController
@RequestMapping("/goods")
@RefreshScope // 开启刷新功能
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

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


    @Value("${itheima}")
    private String itheima;
    ...
    }

添加配置

# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
  config:
    # 配置config-server地址
    uri: http://localhost:9527
    # 配置获得配置文件的名称等信息
    name: config # 文件名
    profile: dev # profile指定,  config-dev.yml
    label: master # 分支

management:
  endpoints:
    web:
      exposure:
        include: refresh

management.endpoints.web.exposure.include: refresh 暴露刷新配置端口

发送post请求
curl -X POST http://localhost:8001/actuator/refresh

在这里插入图片描述

1.3 config集成Eureka

在这里插入图片描述

config-server pom.xml中引入eureka-client 坐标

<!-- eureka-client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

在 config 配置文件中配置 eureka 地址

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

启动类中添加注解

package com.itheima.config;

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

@SpringBootApplication
@EnableConfigServer // 启用config server功能
@EnableEurekaClient
public class ConfigServerApp {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApp.class,args);
    }
}

config-provider 工程中bootstrap.yaml中注掉写死的地址,改为从Eureka中获取

# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
  cloud:
    config:
      # 配置config-server地址
      #uri: http://localhost:9527
      # 配置获得配置文件的名称等信息
      name: config # 文件名
      profile: dev # profile指定,  config-dev.yml
      label: master # 分支
      #从注册中心获取config-server地址
      discovery:
      	enabled: true
      	service-id: CONFIG-SERVER

二. 消息总线bus

2.1 bus概述

在这里插入图片描述

Spring Cloud Bus 是用轻量的消息中间件将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序之间相通信。

Spring Cloud Bus 可选的消息中间件包括 RabbitMQ 和 Kafka

2.3 bus快速入门

分别在 config-server 和 config-client中引入 bus依赖:bus-amqp

<!-- bus -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

分别在 config-server 和 config-client中配置 RabbitMQ

spring:  
  #配置rabbitmq信息
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    virtual-host: /

在OrderController上添加@RefreshScope注解

@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {

    @Value("${itheima}")
    private String itheima;
    //...
}

在config-server配置文件 application.yml 中设置暴露监控断点:bus-refresh

# 暴露bus的刷新端点
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

启动测试

curl结果中没有信息,说明成功了

在这里插入图片描述

在这里插入图片描述

三. 服务链路追踪

3.1 Sleuth+Zipkin 概述

Spring Cloud Sleuth 其实是一个工具,它在整个分布式系统中能跟踪一个用户请求的过程,捕获这些跟踪数据,就能构建微服务的整个调用链的视图,这是调试和监控微服务的关键工具。

  • 耗时分析
  • 可视化错误
  • 链路优化

Zipkin 是 Twitter 的一个开源项目,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。

3.2 Sleuth+Zipkin 快速入门

安装启动zipkin : java –jar zipkin.jar

在这里插入图片描述

在这里插入图片描述

访问zipkin web界面。 http://localhost:9411/

在这里插入图片描述

在服务提供方和消费方分别引入 sleuth 和 zipkin 依赖

<!-- sleuth-zipkin -->
<!--<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>-->

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

分别配置服务提供方和消费方。

sleuth-provider

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 百分之十。

sleuth-consumer

server:
  port: 9000
eureka:
  instance:
    hostname: localhost # 主机名
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: feign-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
  zipkin:
    base-url: http://localhost:9411/  # 设置zipkin的服务端路径
  sleuth:
    sampler:
      probability: 1 # 采集率 默认 0.1 百分之十。
logging:
  level:
    com.itheima: debug


启动,测试 http://localhost:9411/

在这里插入图片描述

详细信息

在这里插入图片描述

四. Spring Cloud 总结

在这里插入图片描述

服务治理 : 注册中心 , 提供服务管理功能(服务注册 服务发现 服务状态监管)

  • Eureka
  • Consul
  • Nacos

负载均衡 : 根据不同的负载均衡算法, 将用户请求分发到不同的服务器

  • Ribbon

服务保护 : 熔断器 , 保证系统服务的可用性

  • Hystrix

服务调用 : 简化服务调用过

  • Feign

服务网关 : 系统服务的唯一入口 , 主要负责 鉴权 , 动态路由 , 跨域请求

  • Zuul
  • Spring Cloud Gateway

配置管理 : 配置中心 , 可以实现配置的统一管理(git/svn)

  • Spring Cloud Config
  • Nacos

消息总线 : 服务通信 , 可以实现配置的不重启更新

  • Spring Cloud BUS

服务调用链路追踪 : 追踪服务调用过程, 及时发现有问题或者效率低的服务器

  • Sleuth+Zipkin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值