Spring Cloud - Load Balancing Ribbon and Remote Call Feign principles and examples

Keywords: Spring OkHttp github Nginx

Previous: Principle and Example of Spring Cloud-Eureka Registration Center
Official website: https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.1.RELEASE/reference/html/#spring-cloud-ribbon
In the front-end and back-end separation architecture, the service layer is divided into a number of micro-services, and there is inevitable interaction between services. This section studies the technologies used for remote invocation of micro-services.

1 Ribbon

1.1 Introduction to Ribbon

Ribbon is an open source load balancing project for Netflix (https://github.com/Netflix/ribbon), which is an HTTP, TCP based client load balancer.
1. What is load balancing?
Load balancing is a technology that must be used in the micro-service architecture to achieve high availability, cluster expansion and other functions of the system.Load balancing can be achieved by hardware devices and software, such as hardware such as F5, Array, software such as LVS, Nginx, etc.The following is a schematic diagram of load balancing:

User requests first arrive at the load balancer (which is also equivalent to a service), which forwards requests to the microservice based on the load balancing algorithm.Load balancing algorithms include: round-robin, random, weighted round-robin, weighted random, address hash, etc. Load balancer maintains a list of services and forwards requests to the corresponding micro-services according to the load balancing algorithm, so load balancing can share requests for the micro-service cluster and reduce system pressure.
2. What is client load balancing?
The above figure shows the service-side load balancing. The difference between client-side load balancing and service-side load balancing is that the client maintains a list of services. Ribbon gets the list of services from Eureka Server. Ribbon requests specific micro-services directly according to the load balancing algorithm, eliminating the load balancing service.The following is a flow chart of Ribbon load balancing:

In consumer microservices, Ribbon uses Ribbon to achieve load balancing. Ribbon first gets a list of services from Eureka Server; Ribbon calls microservices based on the load balancing algorithm.

1.2 Ribbon Test

Spring Cloud introduces Ribbon and restTemplate for client load balancing.There are many remote calling technologies in Java, such as web service, socket, rmi, Apache HttpClient, OkHttp, etc. Internet projects use more http-based clients, and this project uses OkHttp.

  1. Add OkHttp dependencies to the course service:
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
</dependency>

Dependent on spring-cloud-starter-eureka, the spring-cloud-starter-ribbon dependency is automatically added.

  1. Configure Ribbon parameters
    Configure ribbon parameters in application.yml of the course service as follows:
ribbon:
  MaxAutoRetries: 2 #Maximum number of retries, when a service can be found in Eureka, but the service cannot be connected it will be retried
  MaxAutoRetriesNextServer: 3 #Number of retries to switch instances
  OkToRetryOnAllOperations: false  #Retry all operation requests, if get, post, put, etc.
//It is dangerous not to be idempotent,So set to false
  ConnectTimeout: 5000  #Timeout for Request Connection
  ReadTimeout: 6000 #Timeout for request processing
  1. Load Balance Test
    1) Start two teacher services, and note that the modification ports are inconsistent.
    2) Startup class defines RestTemplate, using the @LoadBalanced annotation
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}

3) Test code
Create unit test code in the course service project to call the teacher information interface remotely.After adding the @LoadBalanced annotation, restTemplate walks through the LoadBalancerInterceptor interceptor, which queries the service address through the RibbonLoadBalancerClient, where you can observe the service address and port of each call.You can also simply print your own port on the teacher's microservice interface, so you can see from the console which microservice is currently being requested.

@GetMapping("test/{id}")
	public String getTeacherTest(@PathVariable("id") long teacherId) {
		System.out.println("=============Access Teacher Micro-Service, port number:" + port + " teacherId:" + teacherId);
		return "success";
	}
	
@Test
	public void testRibbon() {
		for (int i = 0; i < 10; i++) {
			// Call through service id
			ResponseEntity<String> forEntity = restTemplate
					.getForEntity("http://SERVICE-PROVIDER-TEACHER/teacher/test/" + i, String.class);
			String result = forEntity.getBody();
			System.out.println(result);
		}


2 Feign

Introduction to 2.1 Feign

Feign is Netflix's open source lightweight rest client, which makes it easy to implement an Http client.Spring Cloud introduced Feign and integrated Ribbon for client load balancing calls.

2.2 Feign Test
  1. Course Services Add Feign Dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>    
<artifactId>spring‐cloud‐starter‐openfeign</artifactId>    
</dependency>
<dependency>
    <groupId>com.netflix.feign</groupId>
    <artifactId>feign‐okhttp</artifactId>
</dependency>
  1. Define the FeignClient interface
    Refer to the Swagger documentation to define the FeignClient, and note that the interface Url, request parameter type, and return value type are consistent with the Swagger interface.
    Create a client package in the course service to define an interface for querying teacher information.
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "SERVICE-PROVIDER-TEACHER") // Specify the service name for the remote call
public interface TeacherClient {
	@GetMapping("/teacher/test/{id}") // Identify the http method type for remote calls with GetMapping
	public String getTeacherById(@PathVariable("id") long teacherId);
}

  1. Startup Class Add @EnableFeignClients Comment
  2. test
@Autowired
	TeacherClient teacherClient;

	@Test
	public void testFeign() {
		for (int i = 0; i < 5; i++) {
			String result = teacherClient.getTeacherById(i);
			System.out.println(result);
		}
	}



Source address: https://github.com/qqxhb/springcloud-demo
Next: Principle and Example of Zuul in Spring-Cloud Gateway

104 original articles were published. 7 were praised. 10,000 visits+
Private letter follow

Posted by cbcampbell on Mon, 24 Feb 2020 19:04:49 -0800