Welcome to pay attention to the public number: good code farmers, attention can receive a gift package

preface

Following the previous article, this article documented several services using feIGN remote invocations.

What is Feign?

Feign is a declarative Web services client.

The core of Feign remote call is to convert the REMOTE call API interface defined in the way of JAVA annotations into HTTP request form through a series of encapsulation and processing, and then decode the response result of HTTP request into JAVA Bean and return it to the caller.

Second, code examples

1. Service providers

Creating a Test Interface

@RestController @RequestMapping("api") public class ProviderController { @GetMapping("/user/{id}") public String User (@pathvariable (value = "id") String id) {return "I am a service provider ==> user id:" + id; }}Copy the code

2. Serve consumers

Introduce feign dependencies

<! - feign dependence - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>Copy the code

Call the interface myfeign.java

package com.local.springboot.client.clientcustomer.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "client-provider-server", path = "/api", fallback = MyFeignFallback.class
        , fallbackFactory = MyFeignFallbackFactory.class)
public interface MyFeign {

    @GetMapping("/user/{id}")
    String getUser(@PathVariable("id") String id);
}
Copy the code

Client-provider-server is the name registered by the service provider in Eureka. You can also specify the url @feignClient (name = “client-provider-server”, path = “/ API “,url = “http://localhost:8081”). Path specifies the path, and the interface method specifies the interface

controller

@getMapping ("/query/{id}") public String getUser(@pathVariable (value = "id") String ID) {return myFeign.getUser(id); } /** * feign */ @getMapping ("/exception") public String exception() {return myfeign.exception (); }Copy the code

3. The test

Start the program and you can see that the service is registered \



accesshttp://localhost:8082/query/imid\

A successful call

4. The Fallback fusing

Fault-tolerant handling is required if you want to make the system usable in the event of an exception to a network request or service provider.

You need to configure fallback to handle exceptions. To obtain error messages, you need to configure fallbackFactory

@FeignClient(value = "client-provider-server", path = "/api", fallback = MyFeignFallback.class
        , fallbackFactory = MyFeignFallbackFactory.class)
Copy the code

Specify the Feign interface implementation class, and FallbackFactory factory interface class, as follows:

MyFeignFallback.java

package com.local.springboot.client.clientcustomer.feign; import org.springframework.stereotype.Component; @component public class MyFeignFallback implements MyFeign {@override public String getUser(String)  id) { return null; } @override public String exception() {return "Network request timed out, please try again later! ; }}Copy the code

MyFeignFallbackFactory.java

package com.local.springboot.client.clientcustomer.feign; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component; /** * FallbackFactory, / @Component public class MyFeignFallbackFactory implements FallbackFactory<MyFeign> {implements FallbackFactory<MyFeign> {implements FallbackFactory<MyFeign> private final MyFeignFallback myFeignFallback; public MyFeignFallbackFactory(MyFeignFallback myFeignFallback) { this.myFeignFallback = myFeignFallback; } @override public MyFeign create(Throwable cause) {printStackTrace(); return myFeignFallback; }}Copy the code

Simulating abnormal

Service provider

@getMapping ("/exception") public String exception() {throw new RuntimeException(" server exception"); }Copy the code

Service consumer

@GetMapping("/exception")
String exception();
Copy the code

Go to http://localhost:8082/exception\

Note: To use Fallback, enable Hystrix and configurefeign.hystrix.enabled=true, if springCloud version is 2020.0.1 or higher, configurefeign.circuitbreaker.enabled=true.

Because look at the source code, it has been modified tofeign.circuitbreaker.enabled

Question:

The following error occurs when packaging:

**

Failed to execute goal org, apache maven. Plugins: maven - resources - the plugin: 3.2.0: resources (default - resources) on the project client-customer: Input length = 1 -> [Help 1]Copy the code

Solution: The corresponding POM will

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Copy the code

To:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> < version > 2.7 < / version > < dependencies > < the dependency > < groupId > org). Apache maven. Shared < / groupId > <artifactId> Maven-filtering </artifactId> <version>1.3</version> </dependency> </dependencies> </plugin>Copy the code

conclusion

Feign and JAVA’s dynamic proxy mechanism enable JAVA developers to complete HTTP calls of remote services without using HTTP framework to encapsulate HTTP request messages.

« previous chapter: getting started with SpringCloud — Eureka service registration and discovery » next chapter: getting started with SpringCloud — Ribbon load balancing

Author: cat tree links: www.jianshu.com/p/b4492bcc4…