Spring Cloud Ribbon
The Spring Cloud Ribbon is a set of client-side load balancing tools implemented on the Netflix ribbon. It is a client load balancer based on HTTP and TCP. It can set the server-side list to poll access to achieve a balanced load by configuring Ribbonserverlist in the client.
When the ribbon is used in conjunction with Eureka, Ribbonserverlist is discoveryenabledniwsserverlist rewritten to obtain a list of service instances from the Eureka registry. It also replaces iping with niwsdiscoveryping, which delegates responsibilities to Eureka to determine whether the server has been started.
When the ribbon is used in conjunction with Consul, Ribbonserverlist is consulserverlist to expand the list of service instances from consul. It is also implemented by Consulping as the Iping interface.
When we use the Spring cloud ribbon, whether it's combined with Eureka or consul, we're introducing Spring cloud Eureka or Spring cloud Consul relies on the automation configuration to load the above mentioned configuration, so we can quickly implement the load balancing of inter-service calls in spring cloud.
Let's look at a concrete example of how to use the Spring Cloud Ribbon to implement service invocation and client-side load balancing.
In the following example, we will use the previously built Eureka-server as the service registry, Eureka-client as the service provider. And based on the spring Cloud Ribbon implementation of the consumer, we can according to Eureka-consumer implementation of the content can be easily changed to complete, the specific steps are as follows:
Copy a service consumer project according to Eureka-consumer, named: Eureka-consumer-ribbon. Add the following dependencies in the Pom.xml:
<dependencies> ... <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies>
Modify the main class of the application. Add @loadbalanced annotations for Resttemplate:
@EnableDiscoveryClient @SpringBootApplication public class Application { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
Modify the controller. Remove the previous step of selecting instances and stitching URLs through loadbalancerclient and initiating requests directly through Resttemplate.
@RestController public class DcController { @Autowired RestTemplate restTemplate; @GetMapping("/consumer") public String dc() { return restTemplate.getForObject("http://eureka-client/dc", String.class); } }
As you can see here, in addition to removing the original logic related to Loadbalancerclient, our first URL parameter is somewhat special for resttemplate use. The host location requested here is not in the form of a specific IP address and port, but is made up of a service name. So why can such a request be called successful? Because the spring Cloud ribbon has an interceptor that is able to make the actual call here, it automatically picks up the service instance and replaces the actual requested IP address and port with the service name, which completes the invocation of the service interface.
After you have finished writing your code, the reader can start Eureka-server, eureka-client, and Eureka-consumer-ribbon. To track how the Eureka-consumer-ribbon service consumes the/DC interface of the Eureka-client service, and can also observe its load balancing effect by initiating multiple eureka-client services.
From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects.
Source source technical support for complete projects 1791743380
Spring Cloud Building MicroServices architecture-service consumption (Ribbon)