First, Introduction
The Ribbon is a load balancer released by Netfix, which helps control the behavior of HTTP and TCP clients. After configuring the service provider address list for the Ribbon, the Ribbon can automatically help service consumers to request based on a load-balancing algorithm. The Ribbon defaults to a number of load-balancing algorithms, such as polling, randomization, and, of course, a custom load-balancing algorithm for the Ribbon.
In spring Cloud, when the Ribbon is used in conjunction with Eureka, the Ribbon automatically obtains a list of service provider addresses from Eureka Server and requests one of the service provider instances based on the load balancing algorithm.
Second, the integration of the Ribbon for service consumers
2.1. Introduction of Ribbon Dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId> Spring-cloud-starter-ribbon</artifactid></dependency>
2.2. Add @loadbalanced annotations for resttemplate (application)
@Bean @loadbalancedpublic resttemplate resttemplate () {return new resttemplate ();
2.3. Call Other MicroServices
@GetMapping ("/user/{id}") Public user findbyid (@PathVariable long id) throws exception { serviceinstance serviceinstance = This.loadBalancerClient.choose ("Spring-ribbon-eureka-client2"); // prints which node is currently selected system.out.println ("serviceid : " + serviceinstance.getserviceid () ); system.out.println ("hoost : " + serviceinstance.gethost ()); system.out.println ("port : " + serviceinstance.getport ()); system.out.println ("============================================================"); if (Null == id) { return null; } return this.resttemplate.getforobject (" http://spring-ribbon-eureka-client2/" + id, user.class);}
Description
1. Spring-ribbon-eureka-client2 is the name of the application that the service provider registers with the Eureka server
2, this test opened two Spring-ribbon-eureka-client2 service providers, one port is 8080, and the other is 8083
2.4. Testing
Visit: HTTP://192.168.1.83:8082/USER/1
Effect:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9B/C3/wKioL1lm9neDpTJeAAAVQZz47WE284.jpg "title=" 1499920075 (1). jpg "alt=" wkiol1lm9nedptjeaaavqzz47we284.jpg "/>
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9B/C3/wKioL1lm9uyhKyAPAAA9xMPUhGM811.jpg "title=" 1499920194 (1). jpg "alt=" wkiol1lm9uyhkyapaaa9xmpuhgm811.jpg "/>
This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1947059
Springcloud (7): Ribbon for client side load balancing-consumer integration ribbon