feign的应用实例(包括应用springCloud项目和不应用在springCloud)

1、在不是SpringCloud项目运用feign的实例(交换数据为不是对象)。

服务消费端:    

      需要导入的maven包(其中feign-core表示运用feign时需要导入的包,而feign-jackson的作用是,服务消费端与生产端之间交换的数据往往是一或多个对象,feign同样提供基于json的对象转换工具,方便我们直接以对象形式交互)

<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>  

    自定义接口:  

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

        测试类:

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);  
    }  
}  

  

服务生产端:

  自定义接口:

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

  

2、在不是SpringCloud项目运用feign的实例(交换数据是对象)。

     需要导入的包和1的包一样。

     服务消费者:

        自定义接口:            

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);  
}  

 

        测试类

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);  
    }  
}  

  

        实体类:

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

  

        服务生产者: 

@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. 在SpringCloud中运用feign实例

搭建好SpringCloud项目,包括服务注册中心、服务消费者、服务生产者。

服务消费者:

        添加maven:

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

  

        添加自定义接口:其中代码中注解feignclient的属性name表示服务消费者的项目名。注解RequestMapping的属性value表示被调用的服务消费者对应的方法上@RequestMapping的value的值。 

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 {  
    //这个接口要和远程调用的接口一只,参数,接口名,返回类型  
    @RequestMapping(value = "/hello/provider")  
    public String helloProvider();  
  
    @RequestMapping(value = "/producerHello")  
    public String sayHello(@RequestParam(value="name") String name);  
  
}  

  

        添加controller类:

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);  
    }  
}  

  

服务生产者:

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";  
    }  
}  

  

 运行项目,结果如下:

posted @ 2018-05-04 11:07  yaohuiqin  阅读(4682)  评论(0编辑  收藏  举报