(转)Spring Cloud
Posted free_wings
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)Spring Cloud相关的知识,希望对你有一定的参考价值。
(二期)23、微服务框架spring cloud(二)
解决方向
- 为网络其请求设置超时时间
- 使用断路器模式
- 包裹请求
- 跳闸机制
- 资源隔离
- 监控
- 回退机制
- 自我修复
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
@EnableHystrix
或者
@EnableCircuitBreaker
@HystrixCommand(fallbackMethod = "getUsernameFallback")
@GetMapping("/username")
public Object getUsername() {
return restTemplate.getForObject("http://EUREKA-CLIENT-2/username", String.class);
}
public Object getUsernameFallback() {
return "fall back";
}
默认提示:@HystrixCommand可以和@DefaultProperties配合使用
@hystrixCommand注解参数详解
/**
* Feign的fallback测试类
* 使用@FeignClient的fallback属性指定回退类
*/
@FeignClient(name = "microservice-provider-user", fallback = FeignClientFallback.class)
public interface UserFeignClient {
@GetMapping(value = "/{id}")
User findById(@PathVariable("id") Long id);
}
/**
* 回退类FeignClientFallback需实现Feign Client接口
* FeignClientFallback也可以是public class,没有区别
*/
@Component
class FeignClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
User user = new User();
user.setId(-1L);
user.setUsername("默认用户");
return user;
}
}
feign:
hystrix:
enabled: true
@FeignClient(name = "eureka-client-2", fallbackFactory = Demo2ClientFallbackFactory.class)
public interface Demo2Client {
@GetMapping("/username")
public String getUsername();
}
@Component
public class Demo2ClientFallbackFactory implements FallbackFactory<Demo2Client> {
public Demo2Client create(Throwable cause) {
return new Demo2Client() {
@Override
public String getUsername() {
System.out.println(cause.toString());
return "hello world fallbackfactory";
}
};
}
}
在实际开发中,并不是所有的请求都要提前预备好服务降级问题,如果我就是要将服务调用失败的信息展示给用户,那么此时就没有必要添加断路器了。
Hystrix断路器模式的状态监控
<!--actuator用于应用监控管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
management:
endpoint:
health:
show-details: always
#说明断路器已经打开
Hystrix circuit short-circuited and is OPEN
https://www.cnblogs.com/java-synchronized/p/7927726.html
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
# 路由规则配置
zuul:
routes:
api-a:
path: /usernameByFeign
serviceId: eureka-client-1
- PRE:
- ROUTING:
- POST:
ERROR:
public class PreLogFilter extends ZuulFilter {
/**
* pre请求之前
* route用于将请求路由转到微服务
* post路由到微服务以后执行
* error在其他阶段发生错误的时候执行
* @return
*/
@Override
public String filterType() {
return "pre";
}
/**
* 执行顺序
* @return
*/
@Override
public int filterOrder() {
return 0;
}
/**
* true返回一个boolean判断该过滤器是否要执行
* @return
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* 过滤器执行具体内容
* @return
* @throws ZuulException
*/
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
System.out.println("---》路由获取所有参数值" + request.getParameterMap().toString());
System.out.println(request.getHeader("cookie"));
return null;
}
}
@Bean
PreLogFilter preLogFilter() {
return new PreLogFilter();
}
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0-jre</version>
</dependency>
import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
/**
* 限流
*/
public class RateLimiterFilter extends ZuulFilter {
//guava的令牌桶算法
private static final RateLimiter RATE_LIMITER = RateLimiter.create(100);
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return -5;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run(){
if(!RATE_LIMITER.tryAcquire()) {
throw new RuntimeException("占坑失败~~");
}
System.out.println("-------->没限流");
return null;
}
}
@Bean
RateLimiterFilter rateLimiterFilter() {
return new RateLimiterFilter();
}
以上是关于(转)Spring Cloud的主要内容,如果未能解决你的问题,请参考以下文章
spring cloud中利用sidecar整合异构语言(转)
Microservices Reference Architecture - with Spring Boot, Spring Cloud and Netflix OSS--转