肝了很久,冰河整理出这份4万字的SpringCloud与SpringCloudAlibaba学习笔记!!
Posted 冰河
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了肝了很久,冰河整理出这份4万字的SpringCloud与SpringCloudAlibaba学习笔记!!相关的知识,希望对你有一定的参考价值。
写在前面
不少小伙伴让我整理下有关SpringCloud和SpringCloudAlibaba的知识点,经过3天的收集和整理,冰河整理出这份4万字的SpringCloud与SpringCloudAlibaba学习笔记!!
文章已收录到:
https://github.com/sunshinelyz/technology-binghe
https://gitee.com/binghe001/technology-binghe
SpringCloud
服务注册中心
eureka
ap 高可用 分布式容错
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
eureka:
instance:
hostname: eureka7003.com #eureka服务端的实例名称
instance-id: payment8001
prefer-ip-address: true
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
#defaultZone: http://eureka7002.com:7002/eureka/
#单机就是7001自己
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
#server:
#关闭自我保护机制,保证不可用服务被及时踢除
#enable-self-preservation: false
#eviction-interval-timer-in-ms: 2000
Ribbon 启用负载均衡
@EnableEurekaServer
@EnableDiscoveryClient
@LoadBalanced
public RestTemplate getTemp() {
return new RestTemplate();
}
zookepper
cp 强一致 分布式容错
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.1</version>
</dependency>
spring:
application:
name: cloud-zoo-consumer-order
cloud:
zookeeper:
connect-string: 192.168.10.58:2181
@SpringBootApplication
@EnableDiscoveryClient
consul
cp 强一致 分布式容错
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
spring:
application:
name: consul-payment-provider
cloud:
consul:
host: 192.168.10.58
port: 8500
discovery:
service-name: ${spring.application.name}
@SpringBootApplication
@EnableDiscoveryClient
服务调用负载均衡
Ribbon
Ribbon 切换 负载规则
- 在springboot 包扫描外层建立 配置
@Configuration
public class Myrule {
@Bean
public IRule initRule() {
return new RandomRule();
}
}
- 启动类给指定服务加载随机方法
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = Myrule.class)
OpenFeign
- 添加maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- 启动类启用Feign
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
- 新建接口 并注册Feign信息
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") //提供方服务名
public interface Service {
@GetMapping(value = "/payment/get/{id}")
Response<Payment> getPaymentById(@PathVariable("id") Long id);
}
- 提供方接口演示
@GetMapping(value = "/payment/get/{id}")
public Response<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
if (payment != null) {
return Result.success(200, "查询成功,serverPort: " + serverPort, payment);
} else {
return Result.success(444, "没有对应记录,查询ID: " + id, null);
}
}
OpenFeign超时设置
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
OpenFeign 日志打印功能
- 配置Openfeign 日志级别
@Configuration
public class FeignLogConfig {
@Bean
public Logger.Level getLevel() {
return Logger.Level.FULL;
}
}
- yml 项目配置文件中,给指定Feign interface 配置日志级别
logging:
level:
ml.ytooo.feignservice.Service: debug
Hystrix 服务治理
- 服务降级 出险异常时,返回友好提示,防止程序异常或者阻塞
- 服务熔断 保险丝,当超出服务承载能力时,返回提示,拒绝请求
- 服务限流 闸门,配置服务承载能力
Hystrix
服务降级
当服务处理超时或者运行异常时,启动备选方案返回给调用者预期结果
主方法
@EnableCircuitBreaker
需要降级处理的程序
其中
- paymentInfo_TimeOut 为预计超时程序
- paymentInfo_TimeOut_Handler 为超时备选方案
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOut_Handler", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
public String paymentInfo_TimeOut(Integer id) {
int time = 5;
try { TimeUnit.MILLISECONDS.sleep(time * 1000); } catch (InterruptedException e) { e.printStackTrace(); }
return "线程池: " + Thread.currentThread().getName() + " paymentInfo_TimeOut,id: " + id + "\\t" + "O(∩_∩)O哈哈~" + " 耗时(秒): " + time;
}
public String paymentInfo_TimeOut_Handler(Integer id) {
return "线程池: " + Thread.currentThread().getName() + " paymentInfo_TimeOut_Handler,id: " + id + "\\t" + "o(╥﹏╥)o";
}
全局降级处理
配置 defaultFallback 的走自己的降级方法,未配置的走 默认@DefaultProperties 指定的降级方法
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "globle",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
})
public class Controller {
@HystrixCommand
@GetMapping("/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
String result = feignService.paymentInfo_TimeOut(id);
log.info("*****result: " + result);
return result;
}
public String globle() {
return "全局";
}
}
通过OpenFeign 配置其提供方全局降级配置
- 新增feign调用接口的实现类 FeignServiceImpl,实现全部方法并做降级处理
@Service
public class FeignServiceImpl implements FeignService {
@Override
public String paymentInfo_OK(Integer id) {
return "降级 -- paymentInfo_OK";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "降级 -- paymentInfo_TimeOut";
}
}
- feign调用接口添加注解
@FeignClient(value = "CLOUD-PROVIDER-HYSTYRIX-PAYMENT",fallback = FeignServiceImpl.class)
服务熔断
- 服务过载时,拒绝连接请求直接调用降级方法,返回异常
- 请求下降时,慢慢恢复该服务访问,直达完全恢复
配置服务的熔断:
一下配置在 10s 内 ,10次请求有60% 失败,则熔断
HystrixProperty 配置位于 HystrixCommandProperties.class 类中
//=====服务熔断
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = { //
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), // 时间窗口期
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),// 失败率达到多少百分百后跳闸
})
public String paymentCircuitBreaker(@PathVariable("id") Integer id)
{
if(id < 0)
{
throw new RuntimeException("******id 不能负数");
}
String serialNumber = IdUtil.simpleUUID();
return Thread.currentThread().getName()+"\\t"+"调用成功,流水号: " + serialNumber;
}
public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) //熔断后降级方法
{
return "id 不能负数,请稍后再试,/(ㄒoㄒ)/~~ id: " +id;
}
效果: 当连续使用 -100 请求时, 返回 "id 不能负数", 使用100请求也返回 "id 不能负数" ,继续连续使用 100请求, 服务慢慢恢复
服务限流
使用 springcloud 阿里巴巴 sentinel 替代
Gateway 网关
Gateway
Gateway 项目配置
- 添加maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
- 移除以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- yml 配置 (后续移步配置中心)
spring:
application:
name: cloud-gateaway-gateaway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_get
# uri: http://127.0.0.1:8001 #单一节点
uri : lb://CLOUD-PAYMENT-SERVICE /#启用 注册中心集群
predicates:
- Path=/payment/get/**
- 注册进 Eureka 注册中心
Gateway 动态路由
- 开启 网关发现注册中心服务
spring:
application:
name: cloud-gateaway-gateaway
cloud:
gateway:
discovery:
locator:
enabled: true
Gateway 断言
断言是判断转发请求的条件
predicates:
- After=2020-02-21T15:51:37.485+08:00[Asia/Shanghai]
- After,Before,Between 配置转发生效时间
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
}
// 2020-08-24T14:23:57.171+08:00[Asia/Shanghai]
- Cookie 请求需携带指定Cookie才可以访问
predicates:
- Cookie=name,ytooo # key,value
- Header ≈ Cookie
predicates:
- Header=name,ytooo # key,value
Gateway 过滤器
- (默认过滤器) 官方提供一系列过滤器,供我们 在请求转发之前,对请求进行加工处理
filters:
- AddRequestParamter=rowid,1024
- 自定义过滤器
自定义全局过滤器
@Component
@Slf4j
public class GatewayLogFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("=====================进入全局过滤器=====================");
String name = exchange.getRequest().getQueryParams().getFirst("name");
if (null == name) {
exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
SpringCloud Config 分布式配置中心, BUS 消息总线
分布式配置中心 SpringCloud Config
服务端配置
- 建立git仓库 https://github.com/sunshinelyz/cloud-config
- 引入 maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3.启动类使配置生效
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigMain3344.class, args);
}
}
- 配置 配置文件
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/sunshinelyz/cloud-config
search-paths:
- cloud-config
label: master
- 请求访问 : http://127.0.0.1:3344/master/config-dev.yml
客户端配置
- 引入 maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
- 配置 配置文件
spring:
application:
name: cloud-condig-client
cloud:
config:
label: master # 分支
name: config # 配置文件名称
profile: dev # 版本
uri: http://127.0.0.1:3344 # config服务端地址
手动刷新客户端配置
不建议使用
消息总线 Bus
设计逻辑
使用消息总线触发服务端的 bus/refresh 端点,刷新所有客户端config配置
初始条件
客户端,服务端都需要实现Springcloud Config功能
服务端配置
- 引入maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 配置文件中配置消息队列信息
# 配置消息队列
rabbitmq:
host: 192.168.10.58
port: 5672
username: ytooo
password: ytooo
- 配置文件中配置BUS总线暴露信息
# 配置bus暴露端点
management:
endpoints:
web:
exposure:
include: "bus-refresh"
- 配置文件预览
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/sunshinelyz/cloud-config
search-paths:
- cloud-config
label: master
# 配置消息队列
rabbitmq:
host: 192.168.10.58
port: 5672
username: ytooo
password: ytooo
eureka:
instance:
prefer-ip-address: true
instance-id: cloud-config-center-3344
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
# 配置bus暴露端点
management:
endpoints:
web:
exposure:
include: "bus-refresh"
客户端配置
- 引入maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 配置文件中配置消息队列信息
# 配置消息队列
rabbitmq:
host: 192.168.10.58
port: 5672
username: ytooo
password: ytooo
- 配置暴露端点
# 配置暴露端点
management:
endpoints:
web:
exposure:
include: "*"
- 调用配置类添加 @RefreshScope
@RestController
@RefreshScope
public class Controller {
@Value("${config.info}")
private String configInfo;
@GetMapping(value = "/test")
public String test() {
return configInfo;
}
}
刷新配置
POST 请求config服务端 http://127.0.0.1:3344/actuator/bus-refresh
刷新成功
定点通知
POST 请求config服务端 http://127.0.0.1:3344/actuator/bus-refresh/{destination}
destination: 注册中心服务名称:端口号
以上是关于肝了很久,冰河整理出这份4万字的SpringCloud与SpringCloudAlibaba学习笔记!!的主要内容,如果未能解决你的问题,请参考以下文章
阿里大佬在Git肝了一个月,终于完成了24万字的Java面试手册
阿里大佬在Git肝了一个月,终于完成了24万字的Java面试手册
阿里大佬在Git肝了一个月,终于完成了24万字的Java面试手册