“This is the fifth day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
The company’s projects are all called by Dubbo, and now it wants to add Feign call, so it introduces Feign to call based on the project
concept
The internal invocation of microservice cluster must depend on a good RPC framework, such as Feign based on HTTP protocol and Dubbo based on Tcp protocol.
What is Feign
Feign is another open source framework developed by Netflix to realize load balancing. It encapsulates Ribbon and RestTemplate and realizes interface oriented programming of WebService, further reducing the coupling degree of the project. Because it encapsulates Riboon and RestTemplate, it has the capabilities of both frameworks to implement load balancing and Rest calls.
Why Feign
Microservices are isolated from each other, so how do microservices and microservices call each other? Obviously, the two microservices can communicate with each other using HTTP. The restTemplate is also used to communicate with each other in code, but this way is not very convenient to pass and use parameters. We need to configure the request head and body before we can initiate the request. After obtaining the response body, it also needs to parse and other operations, which is very tedious. Using Feign to make calls between services can simplify the call process and really feel the joy of calling methods of another class in the same project.
Feign advantages
- Feign aims to make it easier to program Java Http clients
- The Ribbon is integrated with service invocation, so it also supports load balancing
The code field
Service Provider
@RestController
public class TestController {
@GetMapping("test")
public String test() {
return "Local Feign call successful";}}Copy the code
Service caller
Project Version:
- spring boot:
2.4.2
; - spring cloud:
2020.0.1
; - spring cloud alibaba:
2121.1
- Gradle introduces dependencies
api 'org.springframework.cloud:spring-cloud-starter-openfeign'
api 'org.springframework.cloud:spring-cloud-loadbalancer'
Copy the code
-
Add annotations to the start class
@ EnableFeignClients feign
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class RunLearning {
public static void main(String[] args) {
SpringApplication.run(RunLearning.class.args);}}Copy the code
- Calling code
// controller
@GetMapping("/note/test")
public Result<String> test() {
String test = testService.test();
return ResultUtil.success(test);
}
//value is the name of the service application you want to call
@FeignClient(value = "note")
public interface TestService {
@GetMapping("/test")
String test();
}
Copy the code
- demo
Here two note instances are started
Go to http://localhost:20000/note/test
return
For the first time,
{
"code": 200."msg": "Operation successful"."data": "Feign call successful"
}
The second time
{
"code": 200."msg": "Operation successful"."data": "Local Feign call successful"
}
Copy the code
It is called by polling policy
conclusion
From the above, we saw that using feign calls is quite simple. It doesn’t need to be as dependent as Dubbo. Note that if you use NACOS for service registration, you need to ensure that both services are in the same group. Otherwise, the call fails.