In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how springboot uses feign alone to simplify interface calls, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article. Let's take a look at it.
Using feign alone to simplify interface calls
Compared with HttpClient and RestTemplate, it is easier to use the feign of springcloud to call the remote interface, and the remote interface call can be realized by configuration. But sometimes we don't want to use springcloud, but just want to use feign in springboot. I have searched the Internet for a lot of articles about springboot integrating feign alone, but none of them work. Through learning, development, a simple arrangement of a tutorial. This paper describes in detail how to integrate feign separately in springboot to call the remote interface in a simple way.
1. Introduction of maven org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.example demo 0.0.1-SNAPSHOT jar 1.8 1.8 1.8 Hoxton.SR1 org.springframework.boot spring-boot-starter Org.projectlombok lombok true org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-dependencies ${spring-cloud .version} pom import org.springframework.boot spring-boot-maven-plugin 2. Add @ EnableFeignClients annotation to the startup class
Turn on feign
@ EnableFeignClients / / Open feign@SpringBootApplicationpublic class SpringbootApplication {public static void main (String [] args) {SpringApplication.run (SpringbootApplication.class, args);}} 3. Write a service interface as usual
Add some feign comments on this basis
@ FeignClient (name = "userService", url = "${ip}: ${port} / ${base_url}") public interface UserService {/ * query user * @ param user query parameters * @ return user list * / @ PostMapping ("searchUsers") List searchUsers (@ RequestBody User user);}
Add @ FeignClient annotation to the service interface. Name is the id of springbean, which is used when injecting service, and url is the address of ip.
The final calling interface address is url plus @ PostMapping's value.
The url of @ FeignClient can use the parameters in the configuration file. You can introduce it using ${parameter name}.
Corresponding APIs:
RestController ("/ user") public class UserController {@ PostMapping ("searchUsers") List searchUsers (@ RequestBody User user);}
Thus, when using feign, the calling mode of service method (get, post) and parameters, parameter format and return value format need to be consistent with the corresponding interface.
4. Call the API @ Autowiredprivate UserService userService;/*** to query the 18-year-old user and print it to the console * / public void printUsers () {User user = new User (); user.setAge (18); List userList = userService.searchUsers (user); System.out.println ("userList =" + userList);} springboot feign call method comparison 1. The cause of the incident
Both owners use SpringCloud, Barabara used Spring buckets, from eureka to ribbon, from ribbon to feign, from feign to hystrix, and then found different ways of using feign. Take a closer look at this call mode, alas, trouble, how can I define DTO, define Fallback, define methods? After using it, it is found that each has its own advantages, so record it.
two。 Mode 1 introduction
When we develop the service, we will separate the interface from the implementation, that is, there is an API module and a Service module. Consumers rely on the jar package of API and directly inject Service into the API, then the corresponding service can be called directly through Feign.
The corresponding project structure is as follows:
We define the method in the interface API and add Feign annotations, etc. (MICRO-PROVIDER2 is the name of the service, which is registered on the Eureka Server.
You can also implement fallback by yourself using Feign, setting the default playback value of the timeout. Do tests here and don't write too much code. )
The specific implementation is shown in the following figure.
The next step is how we consume the service in consumer. We will implement Provider2Service by relying on api's jar package in the service service.
The specific implementation is as follows
Implementation logic in the code:
3. Mode 2 introduction
This way is relatively simple to introduce. Define the new service interface directly in the consumer, annotate the Feign, define the method, and invoke the same url as the url of the called service. The implementation logic is as follows.
4. Call result test
Mode 2:
Method 1:
As you can see, services can be consumed in both ways (essentially the same). However, the two methods have their own advantages and disadvantages. We have to compare them to see which one we need.
5. Comparison of the two ways
Through the comparison of the two ways, we can see the main advantages and disadvantages are:
Method 1:
Advantages:
1: service consumers do not have to write their own interfaces.
2: can provide good Dto,Vo and so on directly to the service consumers.
Disadvantages:
1:service depends on the jar package, and if you rely on too many services, there will be too much jar.
2: too many interfaces are exposed to consumers. Some interfaces that have nothing to do with consumers are also exposed to each other.
Method 2:
Advantages:
1: there is no need to rely on too many jar packages.
2: consumers should not accept the methods provided by consumers too much.
Disadvantages:
1: consumers are required to implement the interface themselves.
2: heavily dependent on documentation. When implementing the interface, there should be a documentation definition for all information. Such as: request method, request parameters, return value and so on.
3: complete the writing of Dto,Vo by yourself.
Thank you for reading this article carefully. I hope the article "how to call springboot using feign simplified interface alone" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.