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

本文介绍Feign在非SpringCloud及SpringCloud项目中的使用方法,包括不同数据类型的交换方式,详细展示了如何配置Feign客户端和服务端,以及如何进行远程调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.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);
}
            ceshilei
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";
    }
}

           运行项目,结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值