Summary:
- Load balancing framework that supports pluggable load balancing rules
- Support for multiple protocols, such as Http, UDP, etc.
- Provide load balancing client (the caller using the service client)
Ribbon sub-module
ribbon-core
Including load balancing, and load balancing rules in this package
ribbon-eureka
Load balancing class provided to clients erurka
ribbon-httpclient
rest contain client load balancing feature
Load Balancer components
1, a load balancer, at least the following functions
To maintain the server ip and other information
According to a particular logical server selection
2, in order to achieve the basic load balancing, load balancing the Ribbon has three sub-module
Rule: Rule
Ping
ServerList
Analog @LoadBalanced principle
@LoadBalanced annotations to achieve load balancing by interceptor
1, a new comment
package spring.cloud.spring_member.controller.locdbalaned;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义负载均衡的注解
*
* @param
* @return
* @date 2019-06-16 13:57
*/
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MyLoadBalanced {
}
2, register RestTemplate instances into the container, and add custom annotations
package spring.cloud.spring_member.controller.locdbalaned;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.awt.*;
/**
* 类的描述 TODO
*
* @author wangwei
* @Date 2019-06-16 14:04
*/
@RestController
@Configuration
public class MyController {
@Bean
@MyLoadBalanced
RestTemplate tplA() {
return new RestTemplate();
}
@Bean
@MyLoadBalanced
RestTemplate tplB() {
return new RestTemplate();
}
@RequestMapping(value = "/call", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public String call() {
RestTemplate tpl = tplA();
//被拦截器拦截,拦截器内部实现负载均衡
String json = tpl.getForObject("http://app-member-server/call", String.class);
return json;
}
}
3, custom interceptors, load balancing logic can be written, todo
package spring.cloud.spring_member.controller.locdbalaned;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
/**
* 自定义拦截器
*
* @author wangwei
* @Date 2019-06-16 14:08
*/
public class MyInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
System.out.println("=========自定义拦截器");
System.out.println("=========" + httpRequest.getURI());
//可以在这里写负载均衡逻辑代码
return null;
}
}
4, the class definition configuration, automatic assembly RestTemplate to tpls, and add custom interceptor to RestTemplate interceptor
package spring.cloud.spring_member.controller.locdbalaned;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* 定义配置类,给restTemplate的拦截器增加自定义的拦截器
* @Date 2019-06-16 14:01
*/
@Configuration
public class MyConfig {
@Autowired(required = false)
@MyLoadBalanced
private List<RestTemplate> tpls = Collections.emptyList();
@Bean
public SmartInitializingSingleton Ibinitalizing() {
return new SmartInitializingSingleton() {
@Override
public void afterSingletonsInstantiated() {
System.out.println(tpls.size());
for(RestTemplate restTemplate : tpls){
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
interceptors.add(new MyInterceptor());
restTemplate.setInterceptors(interceptors);
}
}
};
}
}
4、