二十、声明式服务调用:使用Spring Cloud Feign 实现参数绑定

上一篇博客是实现的是一个不带参数的REST服务绑定。然而系统各种业务复杂的时候没我们会在HTTP的各个位置传入不同的参数,并且返回请求响应的时候也可能是一个复杂的结构。
这篇学习Fegin中几种不同形式参数的绑定方法。

1、介绍Spring Cloud Feign的参数绑定前,先扩展一下服务提供方hello-service,增加几个接口定义,其中包含带有Request参数的请求,带有Header信息的请求,带有Request Body的请求以及请求响应是一个对象实体。

先为idea换个皮肤增肌新鲜感!哈哈!更换方法 博客目录idea下:http://blog.csdn.net/qq_34288630/article/details/78895823

这里写图片描述

package com.example.demo.web;

import com.example.demo.model.User;
import com.netflix.discovery.EurekaClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;


@RestController
public class HelloController {
    private final Logger logger = LoggerFactory.getLogger(HelloController.class);

    /*//Eureka客户端
    @Autowired
    private EurekaClient eurekaClient;
*/
    //服务发现客户端
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 获得Eureka instance的信息,传入Application NAME
     *
     * */
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        logger.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
        return "hello world";
    }

    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
    public String hello(@RequestParam String name){
        return "Hello"+name;
    }
    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public User hello(@RequestHeader String name,@RequestHeader Integer age){
        return new User(name,age);
    }
    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    public String hello(@RequestBody User user){
        return "Hello"+user.getName()+","+user.getAge();
    }
}

User对象定义如下,值得注意的是这里必须要有User的默认构造函数,不然Spring Cloud Feign根据JSON字符串转换User对象时会抛出异常。

package com.example.demo.model;

/**
 * @Author:peishunwu
 * @Description:
 * @Date:Created in ${Time} 2018/1/23
 */
public class User {
    private String name;

    private Integer age;

    public User() {
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

2、完成对hello-service的改造后,下面开始feign-consumer应用中实现这些新增的请求绑定,
首先,在fegin-consumer中创建与上面一样的User类
然后,在HelloService接口中增加对上述三个新增接口的绑定声明,修改后,完成HelloService接口:如下
这里写图片描述

package com.example.feignconsumer.service;

import com.example.feignconsumer.User.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:peishunwu
 * @Description:
 * @Date:Created in ${Time} 2018/1/22
 */
@FeignClient("hello-service")
public interface HelloService
{
    @RequestMapping("/hello")
    String hello();

    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
    String hello(@RequestParam("name") String name);

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    User hello(@RequestHeader("name") String name,@RequestHeader("age") Integer age);

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    String hello(@RequestBody User user);
}

这里一定要注意,在定义各参数绑定时,@RequestParam、@RequestHeader等可以指定参数名称的注解,他们的value也不能少,在Spring MVC程序中,这些注解会根据参数名称来作为默认值,但是Feign中绑定参数必须通过value属性指明具体的参数名,不然抛出口 lllegalStateException 异常, value 属性不能为空。

3、最后在ConcumerController中新增一个/Feign-consumer2接口,对本篇新增的声明式接口进行调用,修改完成的代码:
这里写图片描述

package com.example.feignconsumer.web;

import com.example.feignconsumer.User.User;
import com.example.feignconsumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:peishunwu
 * @Description:
 * @Date:Created in ${Time} 2018/1/22
 */
@RestController
public class ConsumerController {
    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/feign-consumer",method = RequestMethod.GET)
    public String helloConsumer(){
        return helloService.hello();
    }

    @RequestMapping(value = "/feign-consumer2",method = RequestMethod.GET)
    public String helloConsumer2(){
        StringBuilder sb = new StringBuilder();
        sb.append(helloService.hello()).append("\n");
        sb.append(helloService.hello("psw")).append("\n");
        sb.append(helloService.hello("psw",20)).append("\n");
        sb.append(helloService.hello(new User("psw",20))).append("\n");
        return sb.toString();
    }
}

验证:
在完成上述改造之后, 启动服务注册中心、 两个 hello-service 服务以及我们改造
过的 feign-consumer 。 通 过发送 GET 请求到 http://localhost:
9001/feign-consumer2, 触发 HelloService 对新增接口的调用。 最终, 我们会获
得如下输出, 代表接口绑定和调用成功。

这里写图片描述
eureka(微服务之——服务注册发现、服务消费者,服务提供者简单实例)下载
http://download.csdn.net/download/qq_34288630/10220258

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值