Spring Cloud - Ribbon Spring load balancing client Cloud introductory tutorial (V): Ribbon Spring load balancing client Cloud introductory tutorial (a): Registration Service

Spring Cloud Getting Started tutorial (V): Ribbon load balancing client

 

Connect section, if the amount of access to our services surge Hello world, with a service has been unable to carry, we can make a Hello World Services cluster. 

Very simple, we only need to copy Hello world service, while the original port 8762 changed to 8763. Both Spring Boot and then start the application, you can get two Hello World service. Both Hello world are registered to a eureka service center. This time to visit http: // localhost: 8761, you can see two hello world service has been registered. (See the registration service and Spring Cloud Start Tutorial (a): service registration ).

1. Client load balancing

Load balancing can be divided into server and client load balancing load balancing, server load balancing is handled entirely by the server, the client does not need to do anything. The client load balancing technology, the client needs to maintain a set of server reference, every time the client sent a request to the server, will automatically select a service node according to an algorithm. Common load balancing algorithms:  Round Robbin, Random, Hash, StaticWeighted and so on .

Spring provides two kinds of service scheduling: Ribbon + restful and Feign. Ribbon is a client-based load balancer, Ribbon provides a lot of control over HTTP and TCP clients. 

Internal Feign also been used Ribbon, so long as the use @FeignClient notes, the contents of this chapter are also applicable.

Here's a look at how Spring Cloud Ribbon How to achieve load balancing two Hello World Service. The following is a ribbon Spring cloud client load balancing architecture diagram.

hello world service and ribbon are registered to the service center

service-hi project ran for two copies, respectively, 8762,8763 ports were registered with the service registry, when the service-Hellowworld interface sercvice-ribbon invoked by restTemplate, the use of load balancing with ribbon, will take turns to call in Hello world serving two different ports

 2. Create a Ribbon Service

1) create a maven project, named the Ribbon-Service , pom.xml file as follows:

  pom.xml

 

2) Create Main Class ServiceRibbonApplication

Copy the code
 1 package springcloud.helloworld.ribbon.service;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 6 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.web.client.RestTemplate;
 9 
10 @SpringBootApplication
11 @EnableDiscoveryClient
12 public class ServiceRibbonApplication {
13 
14     public static void main(String[] args) {
15         SpringApplication.run(ServiceRibbonApplication.class, args);
16}
17 
18     @Bean
19 @LoadBalanced 
20 Template Residual rest template () { 
21 return new Template Rest (); 
22} 
23}
Copy the code

@EnableDiscoveryClient registration to the service center, and registered a bean called restTemplate of.

@ LoadBalanced registration shows that this restRemplate need to do is load balancing.

 3) Create a Gets Hello acquired content service class

Copy the code
 1 package springcloud.helloworld.ribbon.client;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 import org.springframework.web.client.RestTemplate;
 6 
 7 @Service
 8 public class HelloService {
 9     @Autowired RestTemplate restTemplate;
10 
11     public String getHelloContent() {
12         return restTemplate.getForObject("http://SERVICE-HELLOWORLD/",String.class);
13     }
14 }
Copy the code

Here is the key code, restTemplate.getForObject method will pass ribbon load balancing mechanism automatically select a Hello word service,

Here is the URL "http: // SERVICE-HELLOWORLD /", which is the name of SERVICE-HELLOWORLD Hello world services, registered to the service center has two SERVICE-HELLOWORLD. Therefore, this call is essentially ribbon-service as a client to choose a server as a SERVICE-HELLOWORLD service based on the load balancing algorithm. Then visit the selected SERVICE-HELLOWORLD Hello world to perform true calling.

3. Start the ribbon-service applications, we can visit http: // localhost: 8901 /, then each refresh can see the following two results appear alternately show the actual call is different SERVICE-HELLOWORLD in different ports.

            

 

Connect section, if the amount of access to our services surge Hello world, with a service has been unable to carry, we can make a Hello World Services cluster. 

Very simple, we only need to copy Hello world service, while the original port 8762 changed to 8763. Both Spring Boot and then start the application, you can get two Hello World service. Both Hello world are registered to a eureka service center. This time to visit http: // localhost: 8761, you can see two hello world service has been registered. (See the registration service and Spring Cloud Start Tutorial (a): service registration ).

1. Client load balancing

负载均衡可分为服务端负载均衡和客户端负载均衡,服务端负载均衡完全由服务器处理,客户端不需要做任何事情。而客户端负载均衡技术,客户端需要维护一组服务器引用,每次客户端向服务端发请求的时候,会根据算法主动选中一个服务节点。常用的负载均衡算法有: Round Robbin,  Random,Hash,StaticWeighted等

Spring 提供两辆种服务调度方式:Ribbon+restful和Feign。Ribbon就是一个基于客户端的负载均衡器, Ribbon提供了很多在HTTP和TCP客户端之上的控制. 

Feign内部也已经使用了Ribbon, 所以只要使用了@FeignClient注解,那么这一章的内容也都是适用的。

下面就看看如何Spring Cloud如何用Ribbon来实现两个Hello World服务的负载均衡。以下是Spring cloud的ribbon客户端负载均衡架构图。

hello world服务和ribbon均注册到服务中心

service-hi工程跑了两个副本,端口分别为8762,8763,分别向服务注册中心注册, 当sercvice-ribbon通过restTemplate调用service-Hellowworld的接口时,利用用ribbon进行负载均衡,会轮流的调用处于两个不同端口的Hello world服务

 2. 创建一个Ribbon服务

1) 创建一个maven工程,取名叫service-ribbon, pom.xml文件如下:

  pom.xml

 

2). 创建主类ServiceRibbonApplication

Copy the code
 1 package springcloud.helloworld.ribbon.service;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 6 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.web.client.RestTemplate;
 9 
10 @SpringBootApplication
11 @EnableDiscoveryClient
12 public class ServiceRibbonApplication {
13 
14     public static void main(String[] args) {
15         SpringApplication.run(ServiceRibbonApplication.class, args);
16     }
17 
18     @Bean
19     @LoadBalanced
20     RestTemplate restTemplate() {
21         return new RestTemplate();
22     }
23 }
Copy the code

@EnableDiscoveryClient向服务中心注册,并且注册了一个叫restTemplate的bean。

@ LoadBalanced注册表明,这个restRemplate是需要做负载均衡的。

 3). 创建获取一个获取Hello内容的service类

Copy the code
 1 package springcloud.helloworld.ribbon.client;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 import org.springframework.web.client.RestTemplate;
 6 
 7 @Service
 8 public class HelloService {
 9     @Autowired RestTemplate restTemplate;
10 
11     public String getHelloContent() {
12         return restTemplate.getForObject("http://SERVICE-HELLOWORLD/",String.class);
13     }
14 }
Copy the code

这里关键代码就是, restTemplate.getForObject方法会通过ribbon负载均衡机制, 自动选择一个Hello word服务,

Here is the URL "http: // SERVICE-HELLOWORLD /", which is the name of SERVICE-HELLOWORLD Hello world services, registered to the service center has two SERVICE-HELLOWORLD. Therefore, this call is essentially ribbon-service as a client to choose a server as a SERVICE-HELLOWORLD service based on the load balancing algorithm. Then visit the selected SERVICE-HELLOWORLD Hello world to perform true calling.

3. Start the ribbon-service applications, we can visit http: // localhost: 8901 /, then each refresh can see the following two results appear alternately show the actual call is different SERVICE-HELLOWORLD in different ports.

            

Guess you like

Origin www.cnblogs.com/wzb0228/p/10973113.html