spring-cloud之ribbon学习笔记
其他
2021-02-28 15:29:05
阅读次数: 0
ribbon基础功能使用
- 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 启动类添加@EnableDiscoveryClient注解
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonLoadbalancerApp {
public static void main(String[] args) {
SpringApplication.run(RibbonLoadbalancerApp.class, args) ;
}
}
- RestTemplate添加@LoadBalanced注解
@Configuration
public class MyConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate() ;
}
}
- 业务调用代码编写
@RestController
public class HelloController {
@Autowired
private RestTemplate restTemplate ;
@GetMapping("/hello")
public String hello(@RequestParam("name") String name){
String url = "http://CLIENT-A/hello2?name=" + name ;
return restTemplate.getForObject(url, String.class) ;
}
}
- ribbon懒加载
# ribbon懒加载
ribbon.eager-load.enabled=true
ribbon.eager-load.clients=client-a, client-b
全局负载均衡策略配置
- 直接将负载均衡规则注入到spring容器
@Configuration
public class RibbonRuleConfig {
@Bean
public IRule ribbonRule(){
return new RandomRule() ;
}
}
单个服务的负载均衡策略配置(注解版)
- 编写配置类,并避免spring容器自动扫描
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AvoidScan {
}
@Configuration
public class RibbonRuleConfig {
@Bean
public IRule ribbonRule(){
return new RandomRule() ;
}
}
- 启动类添加@RibbonClients配置,以及避免扫描配置
// 不自动扫描AvoidScan注解的类
@RibbonClients({
@RibbonClient(name = "client-a", configuration = RibbonRuleConfig.class)
})
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {AvoidScan.class})})
单个服务的负载均衡策略配置(properties配置)
- 添加application.properties配置
# 单个服务的负载均衡策略配置
# <client-a>.ribbon.*
client-a.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
杂项
- HandlerInterceptor是最常规的,其拦截的http请求是来自于客户端浏览器之类的,是最常见的http请求拦截器;
- ClientHttpRequestInterceptor是对RestTemplate的请求进行拦截的,在项目中直接使用restTemplate.getForObject的时候,会对这种请求进行拦截,经常被称为:RestTempalte拦截器或者Ribbon拦截器;
- RequestInterceptor常被称为是Feign拦截器,由于Feign调用底层实际上还是http调用,因此也是一个http拦截器,在项目中使用Feign调用的时候,可以使用此拦截器;
转载自blog.csdn.net/yichengjie_c/article/details/112636758