目录
二,RestTemplate开启ribbon的负载均衡,@LoadBalanced
四,技术资料(springcloudgateway和Hystrix)
springcloudgateway集成hystrix非常简单,官网的说明很清晰明了。
一,pom引入依赖。
<!-- 熔断和限流 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
网关中用到了ribbon,
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
二,RestTemplate开启ribbon的负载均衡,@LoadBalanced
package com.example.gate.config;
import java.nio.charset.Charset;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
//import com.example.gate.service.DbRouteDefinitionRepository;
//com.netflix.client.config.CommonClientConfigKey
//com.netflix.loadbalancer.RetryRule
/**
* 这个配置类是公共的配置。
* 如果某一项配置信息比较多,就单独弄一个配置类。
*
*
* @author lsy
*
*/
@Configuration
public class GateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
// 支持中文编码
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
// SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(5000);// 单位为ms
httpRequestFactory.setConnectTimeout(5000);// 单位为ms
return httpRequestFactory;
}
// @Bean
// public RestTemplate restTemplate(RestTemplateBuilder builder){
// return builder.build();
// }
// @Bean
// public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
// return builder.routes()
// .route("path_route", r -> r.path("/get")
// .uri("http://httpbin.org"))
//
// .route("host_route", r -> r.host("*.myhost.org")
// .uri("http://httpbin.org"))
//
// .route("rewrite_route", r -> r.host("*.rewrite.org")
// .filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}"))
// .uri("http://httpbin.org"))
//
// .route("hystrix_route", r -> r.host("*.hystrix.org")
// .filters(f -> f.hystrix(c -> c.setName("slowcmd")))
// .uri("http://httpbin.org"))
//
// .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
// .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
// .uri("http://httpbin.org"))
//
// .route("limit_route", r -> r
// .host("*.limited.org").and().path("/anything/**")
// .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
// .uri("http://httpbin.org"))
//
// .build();
// }
// @Bean
// public RouteDefinitionWriter routeDefinitionWriter() {
// return new InMemoryRouteDefinitionRepository();
// }
//
// @Bean
// public DbRouteDefinitionRepository dbRouteDefinitionRepository() {
// return new DbRouteDefinitionRepository();
// }
}
三,yml配置和熔断降级的fallback接口。
package com.example.gate.hystrix;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bin