[Special topic on Fegin technology] "Original ecology" opened the beginning of Fegin's RPC technology. Would you use the original ecology of Fegin? (middle)

  • You can use Jersey and CXF to write a Java client for Rest or SOAP services.

  • You can also use Apache HttpClient directly to achieve. But the purpose of Feign is to minimize resources and codes to realize the connection with HTTP API .

  • * With custom codecs and error handling, you can write any text-based HTTP API.

  • Feign works by injecting a templated request via annotations. Just turn it off before sending, and the parameters can be applied directly to the template.

  • * However, this also limits Feign to only support APIs in text form, which greatly simplifies the system in terms of responding to requests.

interface UserService {
    
    

	 List getUserList( String userName,  int age);
}

static class User {
    
    
 String userName;
 int age;
}

public static void main(String... args) {
    
    
 User user = Feign.builder()
            .decoder(new GsonDecoder())
            .target(UserService.class, "https://api.github.com");

 List userList = github.getUserList("libo", 12);
 for (User user : userList) {
    
    
   System.out.println(user.userName + " (" + user.age + ")");
 }
}
复制代码

Feign has many aspects that can be customized. As a simple example, you can use Feign.builder() to construct an API interface with your own components, as follows:

interface Bank {
    
    

 Account getAccountInfo( String id);
}
复制代码

// AccountDecoder() is a Decoder implemented by itself

Bank bank = Feign.builder().decoder(new AccountDecoder()).target(Bank.class, https:
复制代码

For example, the following pattern allows to decorate every request to the authentication service with the current url and authentication token.

CloudDNS cloudDNS = Feign.builder().target(new CloudIdentityTarget(user, apiKey));

Feign includes sample implementations of GitHub and Wikipedia clients. Similar projects also use Feign in practice. Especially its sample daemon.

Feign can work integrated with other open source tools. You can integrate these open source tools into Feign. Some of the existing modules are as follows:

  • * Gson contains an encoder and a decoder, which can be used in the JSON format API.

Add GsonEncoder and GsonDecoder to your Feign.Builder as follows:

GsonCodec codec = new GsonCodec();
GitHub github = Feign.builder()
           .encoder(new GsonEncoder())
           .decoder(new GsonDecoder())
           .target(GitHub.class, https:
复制代码

<dependency>
  <groupId>com.netflix.feigngroupId>
  <artifactId>feign-gsonartifactId>
  <version>8.18.0version>
dependency>
复制代码
  • * Jackson includes an encoder and a decoder, which can be used in JSON-formatted APIs.

Add JacksonEncoder and JacksonDecoder to your Feign.Builder as follows:

UserService service = Feign.builder()
           .encoder(new JacksonEncoder())
           .decoder(new JacksonDecoder())
           .target(UserService.class, https:
复制代码

<dependency>
  <groupId>com.netflix.feigngroupId>
  <artifactId>feign-jacksonartifactId>
  <version>8.18.0version>
dependency>
复制代码
  • SaxDecoder is used to parse XML and is compatible with common JVM and Android. Here is an example of configuring SAX to parse the response:
api = Feign.builder().decoder(SAXDecoder.builder()
.registerContentHandler(UserIdHandler.class)
.build())
.target(Api.class, https:
复制代码
<dependency>
  <groupId>com.netflix.feigngroupId>
  <artifactId>feign-saxartifactId>
  <version>8.18.0version>
dependency>
复制代码
  • JAXB includes an encoder and a decoder, which can be used in XML-formatted APIs.

Add JAXBEncoder and JAXBDecoder to your Feign.Builder as follows:

api = Feign.builder()
      .encoder(new JAXBEncoder())
      .decoder(new JAXBDecoder())
      .target(Api.class, https:
复制代码

<dependency>
  <groupId>com.netflix.feigngroupId>
  <artifactId>feign-jaxbartifactId>
  <version>8.18.0version>
dependency>
复制代码

JAXRSContract overrides the default annotation processing using the JAX-RS specification.

Here is an example using JAX-RS:

interface GitHub {
    
    

 List contributors( String owner,  String repo);
}

GitHub github = Feign.builder()
           .contract(new JAXRSContract())
           .target(GitHub.class, https:
复制代码

<dependency>
  <groupId>com.netflix.feigngroupId>
  <artifactId>feign-jaxrsartifactId>
  <version>8.18.0version>
dependency>
复制代码

OkHttpClient uses OkHttp to send Feign requests. OkHttp supports SPDY (SPDY is a TCP-based transport layer protocol developed by Google to minimize network delays, increase network speed, and optimize users' network experience), and has better control http request.

To make Feign use OkHttp, you need to add OkHttp to your environment variable, and then configure Feign to use OkHttpClient, as follows:

GitHub github = Feign.builder()
           .client(new OkHttpClient())
           .target(GitHub.class, "https://api.github.com");
复制代码

share resources

Information sharing
To obtain the above resources, please visit the open source project and click to jump

Guess you like

Origin blog.csdn.net/star20100906/article/details/132159550