Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Ribbon in Spring Cloud

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, the editor will share with you the relevant knowledge about how to use Ribbon in Spring Cloud. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. What is Ribbon?

Spring Cloud Ribbon is a set of tools to achieve client-side load balancing. Note that it is on the client side, and of course there are load balancing tools on the server side, which we will discuss later. Ribbon can be thought of as a load balancer (Load Balancer, referred to as LB, that is, low ratio). Load balancing is to distribute users' requests equally to multiple services, so as to achieve the high availability of the system.

To put it simply, the main function of Ribbon is to provide software load balancing algorithms on the client side, connecting the middle-tier services of Netflix together. The Ribbon client component provides us with a complete set of configuration items, such as configuring connection timeout, retry, and so on.

To put it more popularly, you can list all the machines (that is, services) after LB in the configuration file. Ribbon will automatically connect these machines (that is, services) according to certain rules (such as polling, random, etc.), and we can also customize some load balancing algorithms.

To put it more simply, Ribbon is a class library integrated with the service consumer, who learns from the service registry what addresses (that is, services) are available, and then the consumer uses Ribbon to choose an appropriate server from these addresses to consume the service.

2. The use of Ribbon

In the previous article, we registered the order service with Eureka, and then the consumer can get the order information through the http request, but this is the original http call, there is no Ribbon in it, and then we are going to insert Ribbon in the consumer.

2.1 Import Ribbon dependencies

The version of Spring Cloud we use is Finchley, and the dependencies we need to import are as follows:

Org.springframework.cloud

Spring-cloud-starter-netflix-eureka-client

Org.springframework.cloud

Spring-cloud-starter-netflix-ribbon

As you can see, Eureka Client dependencies also need to be imported, because services registered with Eureka,Ribbon also need to be integrated with Eureka, so Eureka dependencies are also imported at the consumer side.

2.2 configure application.yml

Server:

Port: 9001

Eureka:

Client:

Register-with-eureka: false

Service-url:

DefaultZone: http://eureka01:7001/eureka/,http://eureka02:7002/eureka/,http://eureka03:7003/eureka/

As can be seen from the previous content (Spring Cloud: using Eureka cluster to build a high availability service registry), we have built an Eureka cluster, so we will use this cluster, and this consumer will not register with this cluster.

2.3Implant Ribbon into http

What does this mean? The previous consumer uses RestTemplate to send http requests and invoke the order service, but there is no load balancer, so now we want this http call to bring its own load balancer.

That is, modify the RestTemplate configuration:

/ * *

* configure RestTemplate

* @ author shengwu ni

, /

@ Configuration

Public class RestTemplateConfig {

/ * *

The *'@ LoadBalanced' annotation indicates that Ribbon is used to achieve client-side load balancing

* @ return RestTemplate

, /

@ Bean

@ LoadBalanced

Public RestTemplate getRestTemplate () {

Return new RestTemplate ()

}

}

Add a @ LoadBalanced annotation to the method to enable Ribbon load balancer. In this way, the corresponding service can be found and accessed from the Eureka by the name of the microservice.

Tip: don't forget to add @ EnableEurekaClient to the main startup class, because this consumer is also an EurekaClient, and we have just imported EurekaClient dependencies.

2.4 change ip to service name

As mentioned earlier, when Ribbon load balancer is enabled, the corresponding service can be found in the Eureka through the name of the microservice. Let's first take a look at how it was realized.

/ * *

* order consumption service

* @ author shengwu ni

, /

@ RestController

@ RequestMapping ("/ consumer/order")

Public class OrderConsumerController {

/ * *

* url prefix of order service provider module

, /

/ / private static final String ORDER_PROVIDER_URL_PREFIX = "http://localhost:8001";

Private static final String ORDER_PROVIDER_URL_PREFIX = "http://MICROSERVICE-ORDER";

@ Resource

Private RestTemplate restTemplate

@ GetMapping ("/ get/ {id}")

Public TOrder getOrder (@ PathVariable Long id) {

Return restTemplate.getForObject (ORDER_PROVIDER_URL_PREFIX + "/ provider/order/get/" + id, TOrder.class)

}

}

As you can see, the part commented out is the original access method, and the service provided by the order is port 8001. Now we change the access method of ip+ port number to micro-service name, which is the registered name shown in the Eureka management interface, that is, the service name configured in the provider's application.yml configuration file:

Spring:

Application:

Name: microservice-order # exposed service name

It has been said in the previous article, so I will not repeat it.

2.5 start the service, test

As mentioned in the previous cluster article, start eureka7001, eureka7002, eureka7003, and order service 8001, respectively, and you can see that the order service has been registered with the Eureka cluster.

Then start the consumer service, and then enter http://localhost:9001/consumer/order/get/1 in the browser to query the corresponding order service:

{"id": 1, "name": "study Spring Boot with Brother Wu", "price": 39.99, "dbSource": "microservice01"}

Note: we can get the order information through the service name of the order.

These are all the contents of the article "how to use Ribbon in Spring Cloud". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report