Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of introducing springboot2.x into feign

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to solve the springboot2.x feign introduction of the pit, I believe that most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

The introduction of feign by springboot2.x is a pitfall. Demand

Recently, the company's project needs to call the interface of third-party services, so I chose feign to implement it (here I only talk about the pom reference of springboot2.x, not much use, a big deal on the Internet).

What is feign

Feign is a declarative web service client that makes calls between microservices easier, similar to controller calling service. Spring Cloud integrates Ribbon and Eureka to provide a load-balanced http client when using Feign.

III. The use of feign in springboot1.x

The configuration and implementation of hystrix circuit breaker are not shown here.

3.1 add dependencies in pom.xml

Org.springframework.cloud spring-cloud-starter-feign 1.2.2.RELEASE org.springframework.cloud spring-cloud-starter-hystrix 1.2.3.RELEASE org.springframework.cloud spring-cloud-starter-hystrix-dashboard 1.2.3.RELEASE

3.2 add an annotation @ EnableFeignClients to the startup class

@ EnableCaching@EnableAutoConfiguration@SpringBootApplication@EnableConfigurationProperties@ServletComponentScan// configuration druid must be annotated. If not, the access page cannot be opened, and filter, servlet and listener need to be registered separately before they can be used. This annotation is provided in spring boot to register @ EnableTransactionManagement@EnableFeignClients@EnableHystrix@EnableCircuitBreaker@EnableHystrixDashboardpublic class App {public static void main (String [] args) {SpringApplication.run (App.class, args);}}

3.3.Boot classes that launch springboot

IV. The use of feign in springboot2.x

The configuration and implementation of hystrix circuit breaker are not shown here.

Spring2.x is used when referencing in another project, so there is a problem and the introduction of feign fails. The Internet says that springboot2.x needs to be introduced as follows:

Org.springframework.cloud spring-cloud-starter-openfeign

But I use openfeign to report an error at startup:

So I changed the dependency of pom.xml to this (be sure to comment it out):

Org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-hystrix 1.2.3.RELEASE org.springframework.cloud spring-cloud-starter-hystrix-dashboard 1.2.3.RELEASE spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false org.springframework .cloud spring-cloud-openfeign 2.0.0.RC1 pom import

The startup class annotations are the same as those in springboot1.x, just start the project, and that's fine.

Comparison of feign call methods 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, I actually found that each has its own advantages. Let's record them one by one today.

II. Introduction to method 1

When developing a 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 in 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 the test 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:

III. Introduction to method 2

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.

The above is all the contents of this article entitled "how to solve the problem of introducing springboot2.x into feign". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report