😹 作者: gh-xiaohe
😻 gh-xiaohe的博客
😽 觉得博主文章写的不错的话,希望大家三连(✌关注,✌点赞,✌评论),多多支持一下!!!
超大型公司才会用到处理大规模微服务的技术。
Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护。
好处:
1、集中管理配置文件
2、不同环境不同配置,动态化的配置更新 dev test
3、配置信息改变时,不需要重启即可更新配置信息到服务
创建gitee 远程仓库,新建config-dev.yml文件,内容如下:
server:port: 8000eureka:instance:hostname: localhost # 主机名prefer-ip-address: true # 将当前实例的ip注册到eureka server 中。默认是false 注册主机名ip-address: 127.0.0.1 # 设置当前实例的ipinstance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} # 设置web控制台显示的 实例idlease-renewal-interval-in-seconds: 3 # 每隔3 秒发一次心跳包lease-expiration-duration-in-seconds: 9 # 如果9秒没有发心跳包,服务器呀,你把我干掉吧~client:service-url:defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
spring:application:name: eureka-provider # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径gh: xiaohe
org.springframework.cloud spring-cloud-config-server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication
@EnableConfigServer // 启用config server功能
public class ConfigServerApp {public static void main(String[] args) {SpringApplication.run(ConfigServerApp.class,args);}
}
server:port: 9527spring:application:name: config-server# spring cloud configcloud:config:server:# git 的 远程仓库地址git:uri: https://gitee.com/ydlclass_cch/ydlclass-configs.gitlabel: master # 分支配置
http://localhost:9527/master/config-dev.yml
案例 : 01eureka-provider-8000 改造
org.springframework.cloud spring-cloud-starter-config
bootstrap.yml
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:cloud:config:# 配置config-server地址uri: http://localhost:9527# 配置获得配置文件的名称等信息name: provider # 文件名profile: dev # profile指定, config-dev.ymllabel: master # 分支
GoodsController
@Value("${gh}")private String xiaoHe;goods.setTitle(goods.getTitle()+"|端口号:"+ port + " xiaoHe" + xiaoHe );
http://localhost:8000/goods/findById/9
gitee的配置文件发生改变 微服务也应该发生变化 , 通过查看 微服务 中的值 没有发生改变,配置中心的值已经发生改变
解决办法:通知微服务
org.springframework.boot spring-boot-starter-actuator
获取配置信息类上,添加 @RefreshScope 注解
@RefreshScope // 开启刷新功能
bootstrap.yml
management:endpoints:web:exposure:include: '*'
curl -X POST http://localhost:8000/actuator/refresh 告诉你要进行刷新,或者使用ApiPost 等软件
测试:值已经改变
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
主启动
@EnableEurekaClient
bootstrap.yml
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:cloud:config:# 配置config-server地址# uri: http://localhost:9527# 配置获得配置文件的名称等信息name: config # 文件名profile: dev # profile指定, config-dev.ymllabel: master # 分支discovery:enabled: trueservice-id: CONFIG-SERVER # 不写死 找具体的配置中心
# 打开端点
management:endpoints:web:exposure:include: '*'
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
主启动
@EnableEurekaClient
application.yml
eureka:client:service-url:defaultZone: http://localhost:8761/eureka
远程的配置文件更新了,运维只需要发一个请求,所有用到这个配置文件的几百个应用更新了。
Spring Cloud Bus 是用轻量的消息中间件将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序之间相通信。
Spring Cloud Bus 可选的消息中间件包括 RabbitMQ 和 Kafka 。
window 安装教程 :https://www.cnblogs.com/jfl-xx/p/10250184.html
文件分享 :https://share.weiyun.com/1JUdp5qf
作用:消息的生产者 已发送消息 , 发送到 MQ 中 让消息的消费者,来监听到消息
三大功能:异步、解耦、削峰填谷
结构
- 1、生产者: 和交换机建立一个 常链接 后通过 常链接中的 channel(频道、管道),把消息发动到 Exchange(交换机)
- 2、RabbitMQ 通过绑定关系,把消息通过交换机 分发到一个或者 多个对列中
- 3、消费者:监听一个对列,消费者 和一个对列 建立一个常链接, 对列中有了消息,就会通过常链接 中的 channel 推送到我们的消费者中
RabbitMQ 提供了 6 种工作模式: 简单模式、工作对列 work queues、Publish/Subscribe 发布与订阅模式、Routing 路由模式、Topics 主题模式、RPC 远程调用模式(远程调用,不太算 MQ;是一个思想,暂不作介绍)。
org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-starter-actuator
server:port: 9527spring:application:name: config-server# spring cloud configcloud:config:server:# git 的 远程仓库地址git:uri: https://gitee.com/ydlclass_cch/ydlclass-configs.gitlabel: master # 分支配置#配置rabbitmq信息rabbitmq:host: localhostport: 5672username: guestpassword: guestvirtual-host: /# 将自己注册到eureka中
eureka:client:service-url:defaultZone: http://localhost:8761/eureka# 暴露bus的刷新端点
management:endpoints:web:exposure:include: 'bus-refresh'
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:cloud:config:# 配置config-server地址# uri: http://localhost:9527# 配置获得配置文件的名称等信息name: config # 文件名profile: dev # profile指定, config-dev.ymllabel: master # 分支# 从注册中心去寻找config-server地址discovery:enabled: trueservice-id: CONFIG-SERVER#配置rabbitmq信息rabbitmq:host: localhostport: 5672username: guestpassword: guestvirtual-host: /management:endpoints:web:exposure:include: '*'
# 暴露bus的刷新端点
management:endpoints:web:exposure:include: 'bus-refresh'
bus自动在mq中建立交换机
只要微服务起起来,自动在mq中,建立队列,和交换机绑定
curl -X POST http://localhost:9527/actuator/bus-refresh
问题:更换消息对列,所属所涉及到的代码都会发生改变
解决问题:Stream
1、Spring Cloud Stream 是一个构建消息驱动微服务应用的框架。
2、Stream 解决了开发人员无感知的使用消息中间件的问题,因为Stream对消息中间件的进一步封装,可以做到代码层面对中间件的无感知,甚至于动态的切换中间件,使得微服务开发的高度解耦,服务可以关注更多自己的业务流程。
3、Spring Cloud Stream目前支持两种消息中间件RabbitMQ和Kafka
就像jdbc一样
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-stream-rabbit
@SpringBootApplication
public class StreamProducerApp8000 {public static void main(String[] args) {SpringApplication.run(StreamProducerApp8000.class,args);}
}
server:port: 8000spring:cloud:stream:# 定义绑定器,绑定到哪个消息中间件上binders:ydlclass_binder: # 自定义的绑定器名称type: rabbit # 绑定器类型environment: # 指定mq的环境spring:rabbitmq:host: localhostport: 5672username: guestpassword: guestvirtual-host: /bindings:output: # channel名称binder: ydlclass_binder #指定使用哪一个binderdestination: ydlclass_exchange # 消息目的地(交换机)
MessageProducer
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;
import java.nio.charset.StandardCharsets;@Component
@EnableBinding(Source.class)//这个类是stream的发送者
public class MessageProducer {@AutowiredMessageChannel output;public void send(String msg){output.send(MessageBuilder.withPayload(msg).build());System.out.println("MessageProducer 确实发了消息了!");}
}
ProducerController
import com.ydlclass.stream.producer.MessageProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("test")
public class ProducerController {@AutowiredMessageProducer messageProducer;//业务逻辑@GetMapping("/send")public String send(){//发消息String msg="使用stream来发消息了!";messageProducer.send(msg);return "success!";}
}
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-stream-rabbit
@SpringBootApplication
public class StreamConsumerApp9000 {public static void main(String[] args) {SpringApplication.run(StreamConsumerApp9000.class,args);}
}
server:port: 9000spring:cloud:stream:# 定义绑定器,绑定到哪个消息中间件上binders:ydlclass_binder: # 自定义的绑定器名称type: rabbit # 绑定器类型environment: # 指定mq的环境spring:rabbitmq:host: localhostport: 5672username: guestpassword: guestvirtual-host: /bindings:input: # channel名称binder: ydlclass_binder #指定使用哪一个binderdestination: ydlclass_exchange # 消息目的地
MessageListener
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;/*** 消息接收类*/
@EnableBinding(Sink.class) // 这个类是stream 的消费者监听类
@Component
public class MessageListener {@StreamListener(Sink.INPUT) // 接到到消息,立马执行下面代码public void receive(Message message){System.out.println(message);System.out.println(message.getPayload());}
}
问题?
想看一下,一个请求过来,设计到多少个微服务,这就是所谓的链路追踪
Spring Cloud Sleuth 其实是一个工具,它在整个分布式系统中能跟踪一个用户请求的过程,捕获这些跟踪数据,就能构建微服务的整个调用链的视图,这是调试和监控微服务的关键工具。
Zipkin 是 Twitter 的一个开源项目,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。
Zipkin 和 Sleuth共同完成:Sleuth 手机数据 传到 Zipkin 上
org.springframework.cloud spring-cloud-starter-zipkin
provider
spring:application:name: feign-provider# 收集的数据传到 zipkin上 zipkin:base-url: http://localhost:9411/ # 设置zipkin的服务端路径# 收集这些信息sleuth:sampler:probability: 1 # 采集率 默认 0.1 百分之十。
consumer
spring:application:name: feign-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径# 收集的数据传到 zipkin上 zipkin:base-url: http://localhost:9411/ # 设置zipkin的服务端路径# 收集这些信息sleuth:sampler:probability: 1 # 采集率 默认 0.1 百分之十。