声明式服务调用客户端FeginClient应用总结
eureka-feign-api 接口层。只负责提供接口package com.didispace.api;import org.springframework.web.bind.annotation.*;public interface HelloService {@RequestMapping(value = "/hello",method = RequestM...
·
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);
}
推荐使用第二种。更加灵活。
更多推荐
已为社区贡献1条内容
所有评论(0)