Spring Cloud Ribbon--智能路由

一、Ribbon负载均衡

还是先通过一个demo直观的体验一下spring cloud Ribbon负载均衡技能

1、引入pom依赖


		org.springframework.boot
		spring-boot-starter-parent
		1.3.5.RELEASE
		 
	

	
		
			org.springframework.cloud
			spring-cloud-starter-ribbon
		

	
		org.springframework.cloud
		spring-cloud-starter-hystrix
	

		
			org.springframework.cloud
			spring-cloud-starter-eureka
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	
2、编写服务消费业务类ConsumerController

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
    }
}
3、编写启动类

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class, args);
    }
}
4、添加properties配置文件

spring.application.name=ribbon-consumer
server.port=3333

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
启动RibbonApplication main方法后,刷新Eureka监控平台,可见除去上文已注册到Eureka上的两个compute-service实例之外,还添加了当前ribbon-consumer服务。


5、演示Ribbon负载均衡功能

访问http://localhost:3333/add ,调用compute-service服务的add方法,执行http://COMPUTE-SERVICE/add?a=10&b=20,访问两次,可见compute-service服务1控制台打印出1次调用信息,compute-service服务1打印出第二次调用信息,表明两次调用均发到了两个不同的服务实例。这就是ribbon提供的服务智能路由、负载均衡功能。

二、How does Ribbonwork?

在RibbonApplication类中,@LoadBalanced注解就是实现服务智能路由的关键,通过查看@LoadBalanced源码,发现该注解用于标记一个RestTemplate bean,并使用LoadBalancerClient来配置它。LoadBalancerClient便是Ribbon实现负载均衡的关键入口

/**
 * Annotation to mark a RestTemplate bean to be configured to use a LoadBalancerClient
 * @author Spencer Gibb
 */
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Qualifier
public @interface LoadBalanced {
}
LoadBalancerClient接口源码如下

public interface LoadBalancerClient {
	/**
	 * Choose a ServiceInstance from the LoadBalancer for the specified service
	 * @param serviceId the service id to look up the LoadBalancer
	 * @return a ServiceInstance that matches the serviceId
	 */
	ServiceInstance choose(String serviceId);//根据传入的服务名serviceId,从负载均衡器中挑选一个对应服务的实例。

	/**
	 * execute request using a ServiceInstance from the LoadBalancer for the specified
	 * service
	 * @param serviceId the service id to look up the LoadBalancer
	 * @param request allows implementations to execute pre and post actions such as
	 * incrementing metrics
	 * @return the result of the LoadBalancerRequest callback on the selected
	 * ServiceInstance
	 */
	 T execute(String serviceId, LoadBalancerRequest request) throws IOException;
	//使用从负载均衡器中挑选出的服务实例来执行请求内容。
	/**
	 * Create a proper URI with a real host and port for systems to utilize.
	 * Some systems use a URI with the logical serivce name as the host,
	 * such as http://myservice/path/to/service.  This will replace the
	 * service name with the host:port from the ServiceInstance.
	 * @param instance
	 * @param original a URI with the host as a logical service name
	 * @return a reconstructed URI
	 */
	URI reconstructURI(ServiceInstance instance, URI original);
	//
}
大致流程如下:通过 LoadBalancerInterceptor拦截器对 RestTemplate的请求进行拦截,并利用Spring Cloud的负载均衡器 LoadBalancerClient将以逻辑服务名为host的URI转换成具体的服务实例的过程。

三、Ribbon负载均衡策略

Spring Cloud Ribbon--智能路由_第1张图片


你可能感兴趣的:(Spring Cloud Ribbon--智能路由)