Load balancing and Ribbon

Service providers and consumers
eureka service registration is to discover
what eureka cluster configuration
ribbon is
a tool to achieve client load balancing, what is load balancing, in simple terms, it is to spread the user's request to multiple services, so as to achieve the system The high availability is actually how the client chooses the best service. In reality, there may be multiple services, but if the client calls the same service every time, the service may crash. The purpose of load balancing is to let the client Choose the current best service.

Simple classification of load balancing

  • Centralized LB
    uses independent LB facilities between consumers and providers of services, such as Nginx: reverse proxy server! , The device is responsible for forwarding the access request to the service provider through a certain strategy!
  • Progressive LB
    integrates the LB logic into the consumer. The consumer knows which addresses are available from the registry, and then chooses a suitable server from these addresses!
    Ribbon belongs to the process LB, it is just a class library, integrated in the
    consumer process, the consumer uses it to obtain the address of the service provider

Ribbon integration
Import dependencies on the consumer side. If you don’t know much about eureka, you can see the link at the beginning of the article.
Insert picture description here
Start the class. Join eureka to register and
Insert picture description here
add the annotation @LoadBalanced. When using RestTemplate, if there is this annotation on the RestTemplate, then this RestTemplate calls the remote Address, will take the load balancer.
Insert picture description here
Modify the address
Insert picture description here
to access through the registered service provider service name. This service provider service includes multiple services such as 7001, 7002, 7003, etc.
Insert picture description here
Start the service consumer and then access the access successfully. It is worth noting that the client can be directly after the integration of ribbon and eureka Calls don’t care about the ip address and port number,
Insert picture description here
but how do we see the real effect, so let’s verify it.
First, we add two additional databases and different service providers.

Both db2 and db3 have the same structure except that the data in it is different. Everything else is the same.
Insert picture description here
After adding different service provider files, you can copy them. Pay attention to modify the yml file and
Insert picture description here
start the eureka service 7001 7002 start the service provider 8001 8002 and then start 80 The consumer of the port
will hang up due to the performance of my computer. So here I only open two
Insert picture description here
. The first time I visit, I can see that the data he is going to fetch is dbcloud1.
Insert picture description here

The data obtained for the second time is that dbcloud2 has been
Insert picture description here
verified successfully!

Guess you like

Origin blog.csdn.net/old_wzhou/article/details/111469405