声明式服务调用客户端FeginClient应用总结

eureka-feign-api   接口层。只负责提供接口

package com.didispace.api;

import org.springframework.web.bind.annotation.*;


public interface HelloService {

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

}


//注意这里入参注解采用的@RequestParam("name")

eureka-feign-client  接口实现。负责具体业务逻辑层

package com.didispace.api.impl.controller;

import com.didispace.api.HelloService;
import org.springframework.web.bind.annotation.*;

/**
 * @author liyy
 * @description:
 * @date 2019-04-02 11:23
 * @program spring-cloud
 */

@RestController
public class HelloController implements HelloService {

    @Override
    public String hello(@RequestParam("name") String name) {
        return "hello " + name;
    }

}

//注意入参注解:@RequestParam("name")

eureka-feign-consumer:调用者通过feignclient发起服务调用

package com.didispace.api.consumer.feignclient;

import com.didispace.api.HelloService;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;


@FeignClient(value = "eureka-feign-client")
public interface HelloServiceClient extends HelloService{

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

//声明feignclient客户端。注意:@RequestParam("name"),请求方式只能是POST
package com.didispace.api.consumer.controller;

import com.didispace.api.consumer.Application;
import com.didispace.api.consumer.feignclient.HelloServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liyy
 * @description:
 * @date 2019-04-02 11:17
 * @program spring-cloud
 */
@RestController
public class TestController {

    @Autowired
    private HelloServiceClient helloServiceClient;

    @GetMapping("/test")
    public String test(String name) {
        System.out.println("通过feigin客户端调用"+name);
        return helloServiceClient.hello(name);
    }

}

//注入客户端。发起调用服务。

---------------------------------------------------------------------------------------------------------------------------------------------------------

添加一个注解。稍微变化一下也可以实现调用,而且代码看上去更加的整洁如下:

package com.didispace.api.consumer.feignclient;

import com.didispace.api.HelloService;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * 接口描述信息:feigin客户端
 *
 * @author liyy
 * @date 2019-04-02 11:18
 */
@FeignClient(value = "eureka-feign-client",path = "/helloController")
public interface HelloServiceClient extends HelloService{


}

//添加一个path注解指定要调用的服务端的地址。并且一定要去掉上面接口中所有的方法实现
package com.didispace.api.impl.controller;

import com.didispace.api.HelloService;
import org.springframework.web.bind.annotation.*;

/**
 * @author liyy
 * @description:
 * @date 2019-04-02 11:23
 * @program spring-cloud
 */
@RestController
@RequestMapping(value = "/helloController")
public class HelloController implements HelloService {

    @Override
    public String hello(@RequestBody String name) {
        return "hello " + name;
    }

}


//添加了一个注解@RequestMapping(value = "/helloController")。对应上面feignClient里面path.
入参的注解改成了@RequestBody也是可以的
package com.didispace.api;

import org.springframework.web.bind.annotation.*;

/**
 * @author 翟永超
 * @create 2017/8/8.
 * @blog http://blog.didispace.com
 * descri
 */
public interface HelloService {

    @RequestMapping(value = "/hello",method = RequestMethod.POST)
    String hello(@RequestBody String name);

}

推荐使用第二种。更加灵活。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值