Application instances of feign (including applying springCloud projects and not in springCloud)

1. An instance of feign used in a project other than SpringCloud (data exchanged is not an object).

Consumer side of service:

maven packages that need to be imported (where feign-core represents the package that needs to be imported when feign is used, while feign-jackson is used to exchange data between service consumers and producers often with one or more objects, and feign also provides json-based object conversion tools that allow us to interact directly as objects)

<dependency>  
            <groupId>com.netflix.feign</groupId>  
            <artifactId>feign-core</artifactId>  
            <version>8.18.0</version>  
        </dependency>  
        <dependency>  
            <groupId>com.netflix.feign</groupId>  
            <artifactId>feign-jackson</artifactId>  
            <version>8.18.0</version>  
</dependency>  

Custom interface:

import feign.Param;  
import feign.RequestLine;  
  
public interface remoteService {  
    @RequestLine("GET /producerHello?name={name}")  
    String sayHello(@Param(value = "name") String name);  
} 

Testing Class:

import feign.Feign;  
import feign.Request;  
import feign.Retryer;  
  
public class test {  
    public static void main(String[] args) {  
        remoteService service = Feign.builder()  
                .options(new Request.Options(1000, 3500))  
                .retryer(new Retryer.Default(5000, 5000, 3))  
                .target(remoteService.class, "http://localhost:8402");  
        String result = service.sayHello("scott");  
       System.out.println(result);  
    }  
}  

  

Service production side:

Custom interface:

@RestController  
public class HelloController {  
    @RequestMapping("/producerHello")  
    public String Hello(@RequestParam("name") String name){  
        return "hello " + name + ",this is demo-client1 messge";  
    }  

  

2. An instance of feign used in a project other than SpringCloud (exchanging data is an object).

The package to be imported is the same as the package 1.

Services consumers:

Custom interface:

import feign.Headers;  
import feign.RequestLine;  
  
public interface remoteService2 {  
    @Headers({"Content-Type: application/json","Accept: application/json"})  
    @RequestLine("POST /users/list")  
    User getOwner(User user);  
}  

 

* Test Classes

import feign.Feign;  
import feign.Request;  
import feign.Retryer;  
import feign.jackson.JacksonDecoder;  
import feign.jackson.JacksonEncoder;  
  
public class test {  
    public static void main(String[] args) {  
        remoteService2 service = Feign.builder()  
                .encoder(new JacksonEncoder())  
                .decoder(new JacksonDecoder())  
                .options(new Request.Options(1000, 3500))  
                .retryer(new Retryer.Default(5000, 5000, 3))  
                .target(remoteService2.class, "http://localhost:8402");  
        User u =new User();  
        u.setId("1233");  
        u.setName("yaohuiqin");  
        User result = service.getOwner(u);  
       System.out.println(result);  
    }  
}  

  

Entity classes:

public class User {  
    String name;  
    String id;  
................  
}  

  

Service producers:

@RestController  
public class HelloController {  
    @RequestMapping(value="/users/list",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT})  
    @ResponseBody  
    public User list(@RequestBody User user) throws InterruptedException{  
        System.out.println(user.getName());  
        user.setId("3333");  
        user.setName(user.getName().toUpperCase());  
        return user;  
    }  
}  

  

 

3. Using feign instances in SpringCloud

Build SpringCloud projects, including service registries, service consumers, and service producers.

Service consumers:

Add maven:

<dependency>  
      <groupId>org.springframework.cloud</groupId>  
      <artifactId>spring-cloud-starter-feign</artifactId>  
      <version>1.3.1.RELEASE</version>  
    </dependency>  

  

Add a custom interface where the attribute name with the comment feignclient in the code represents the item name of the service consumer.The property value of the annotation RequestMapping represents the value of @RequestMapping on the method corresponding to the invoked service consumer.(

import org.springframework.cloud.netflix.feign.FeignClient;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
  
@FeignClient(name= "YAOHUIQIN8401")  
public interface HelloRemote {  
    //The interface to be called remotely is one, parameter, interface name, return type  
    @RequestMapping(value = "/hello/provider")  
    public String helloProvider();  
  
    @RequestMapping(value = "/producerHello")  
    public String sayHello(@RequestParam(value="name") String name);  
  
}  

  

Add controller s:

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.ResponseBody;  
import org.springframework.web.bind.annotation.RestController;  
  
@RestController  
public class RemoteController {  
    @Autowired  
    HelloRemote helloRemote;  
  
    @RequestMapping(value = "/hello")  
    public String hello(){  
        return helloRemote.helloProvider();  
    }  
    @RequestMapping("/consumerHello/{name}")  
    public String index(@PathVariable("name") String name){  
        return helloRemote.sayHello(name);  
    }  
}  

  

Service producers:

import org.springframework.web.bind.annotation.*;  
  
@RestController  
public class HelloController {  
    @RequestMapping(value = "/hello/provider")  
    public String helloProvider(){  
        return "I am is provider ,hello world";  
    }  
  
    @RequestMapping("/producerHello")  
    public String Hello(@RequestParam("name") String name){  
        return "hello " + name + ",this is demo-client1 messge";  
    }  
}  

  

Run the project as follows:

Tags: Java JSON Maven Spring Attribute

Posted on Sun, 22 Mar 2020 12:15:02 -0400 by gotDNS