Today, I learned that Feign based remote service invocation (key) is more convenient than yesterday, the coupling degree of code is reduced, the development efficiency is improved, and different methods are selected in different business situations
The following is a conceptual introduction to today's subject
Background:
When the service consumer requests the service of the service provider based on rest, a direct way is to splice the url, splice the parameters, and then implement the service call. However, each service call needs to be spliced in this way. The amount of coding is complex and difficult to maintain. At this time, Feign was born.
Concept introduction:
Feign is a declarative Web service client. The bottom layer encapsulates the application of Rest technology. Feign can simplify the service consumer's call to the remote service provider methodrealization:
After getting to know him, we began to use him to fight
The first step is to import the dependencies he needs In POM XML file
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
Then we: add the @ EnableFeignClients annotation on the startup class
package com.jt.controller; import com.jt.feign.RemoteProvoderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/feign/") public class FeignConsumerController { @Autowired private RemoteProvoderService remoteProvoderService; @GetMapping("echo/{msg}") //http://localhost:8090/feign/echo/adc public String doFeignEcho(@PathVariable String msg) throws InterruptedException { Thread.sleep(10000); return remoteProvoderService.Msg(msg); } }
Note: with this annotation, when we start the project, we will scan the interfaces in the same package or sub package for us and create implementation classes for automatic implementation interfaces
In the third part, we create interfaces and annotate them@ FeignClient , this annotation will create the object of this class. By default, the name of the object is based on our name attribute value, but this name has the meaning of the second layer, which enables us to call the service name remotely. However, for the conflict error, we use another attribute to specify the specific object name, and use contextid , for the fallbackFactory later
package com.jt.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; ///@FeignClient notes that the object created on the interface is managed by spring. The name is the default value of name, but another meaning is the call name of remote service /*Remote service invocation interface*/ /*The location requirements should be written in the same package or sub package of the same package where the startup class is located*/ /*The framework hides the creation of implementation classes, and the bottom layer uses reflection and annotation feature acquisition to realize the communication between services through the restemplate object */ /* Solve the name of the object of the interface implementation class given by contextid */ @FeignClient( name="sca-provder" ,contextId = "Shixianleiname", fallbackFactory = ProviderFallbackFactory.class ) public interface RemoteProvoderService { @GetMapping( "/provider/echo/{msg}") public String Msg(@PathVariable("msg") String msg); }
We declare an interface. The contents of the interface are the request mode of the service we call remotely, and an abstract method
Step 4:
We create a controller with the following code:
package com.jt.controller; import com.jt.feign.RemoteProvoderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/feign/") public class FeignConsumerController { @Autowired private RemoteProvoderService remoteProvoderService; @GetMapping("echo/{msg}") //http://localhost:8090/feign/echo/adc public String doFeignEcho(@PathVariable String msg) throws InterruptedException { Thread.sleep(10000); return remoteProvoderService.Msg(msg); } }
We use IOC dependency injection, dynamic agent of AOP, and create the object of our interface implementation class By implementing the object of the class, we use it to call the method body of the remote service (function module business)
Test:
//http://localhost:8090/feign/echo/adc
Visit this website to see if there will be a specific implementation of the function code of remote business
In addition, we also add the underlying business implementation And handwritten tomcat (network programming) we are writing a post Thank you for reading. Have a nice life