SpringCloud Chapter VIII, Upgrade, Load Balancing and Service Calls Ribbon and OpenFeign
1. Ribbon
1. Overview
SpringCloud Ribbon is a set of client load balancing tools that give NetFlex Ribbon implementations. Simply put, the main function is to provide load balancing algorithms and service calls for clients.The Ribbon client component provides a series of configuration items such as connection timeouts, retries, and so on.Simply put, by listing all the machines behind Load Balance in the configuration file, Ribbon automatically helps you connect them based on a certain rule (simple polling, random, etc.). Currently Ribbon's official website also shows maintenance status, SpringCloud may use LoadBalancer instead in the future
2. Load Balancing
1. What is load balancing? LoadBanlance simply means that user requests are evenly distributed across multiple servers.This achieves high availability of the system. Common examples are balanced Nginx and LVS. 2. What is the difference between Nginx and Ribbon? Nginx is server load balancing, where all requests from the client are handed over to Nginx, which then forwards the requests, that is, load balancing is done by the server. Ribbon is a local load balancing, and when invoking a micro-service interface, it caches the list of registered information services on the registry to the JVM locally, thereby implementing RPC remote service calls locally.
3. Demonstration
We have previously demonstrated that the cloud-consumer-order-80 project calls cloud-provider-payment-8001 and cloud-provider-payment-8002 through RestTemplate, respectively.
Although we don't reference Ribbon, Eureka-client has actually integrated Ribbon internally.
Ribbon usage and IRule custom load rules can be found in the SpringCloud Chapter III, Load Balancing Ribbon and Feign>
<!--Normal Reference--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
MyRule
@Configuration public class MyRule { @Bean public IRule getRule(){ return new RandomRule(); } }
Main Startup Class
package com.lee.springcloud; import com.lee.rules.MyRule; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; /** * consumer Main Startup Class */ //Indicate that you are a client of Eureka @EnableEurekaClient @RibbonClient(name="CLOUD-PAYMENT-SERVICE",configuration = MyRule.class) @SpringBootApplication public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class,args); } }
2. OpenFeign
1. Overview
Originally, when we used Dubbo to implement remote RPC calls, they were all interoperable between Service s.However, Ribbon is used in combination with the Controller layer and RestTemplate, which is not very consistent with our usage habits.This leads to OpenFeign. Feign is a declarative web service client.Using feign makes writing web service clients easier. It is used by defining a service interface and then annotating it.SpringCloud encapsulates Feign to support springmvc standard annotations and HttpMessageConverters.Feign can be used in combination with Eureka and Ribbon to support load balancing.
openFeign also integrates Ribbon internally.
2. Differences between Feign and OpenFeign
Feign Feign Built-in Ribbon,Used for client load balancing to invoke services from the service registry. feign Use this method: Feign Annotations define an interface that can be invoked to invoke a service in the Service Registry. OpenFeign OpenFeign yes springcloud stay feign Supported on springmvc Notes, such as@RequestMapping Wait. openFeign Of@FeignClient Annotations can be interpreted springmvc Of@RequestMapping Interfaces under annotations, and implementation classes are generated by dynamic proxies to load-balance the classes and invoke other services
3. Construction of cloud-consumer-order-80
New pom
<!--openFeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
Main startup class added:
@EnableFeignClients
PaymenService
package com.lee.springcloud.feign.service; import com.lee.springcloud.entities.CommonResult; import com.lee.springcloud.entities.Payment; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @Service @FeignClient(name="CLOUD-PAYMENT-SERVICE") public interface PaymentService { @PostMapping("/payment/create") CommonResult create(@RequestBody Payment payment); @GetMapping("/payment/get/{id}") CommonResult getPaymentById(@PathVariable("id") Long id); }
Controller
@Resource private PaymentService paymentService; @PostMapping("/openFeign/payment/create") public CommonResult<Payment> create2(@RequestBody Payment payment) { return paymentService.create(payment); } @GetMapping("/openFeign/payment/get/{id}") public CommonResult<Payment> getPaymentById2(@PathVariable("id") Long id) { return paymentService.getPaymentById(id); }
Test:
Start eureka7001 eureka7002 payment-8001 payment-8002 order-80 Call: http://localhost/consumer/openFeign/payment/get/1 Result: {"code":200,"message": "serverPort:8002 Payment (id=1, serial=001)", "data": null} for successful data query