<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
package com.crazymaker.springcloud.user.info.start;
...
//启动Feign
@EnableFeignClients(basePackages = { "com.crazymaker.springcloud.seckill.remote.client"}, defaultConfiguration = {TokenFeignConfiguration.class} )
public class UserCloudApplication {
public static void main(String[] args) {
SpringApplication.run(UserCloudApplication.class, args);
}
}
package com.crazymaker.springcloud.seckill.remote.client;
...
/**
*@description:远程服务的本地声明式接口
*/
@FeignClient(value = "seckill-provider", path = "/api/demo/")
public interface DemoClient {
/**
*测试远程调用
*@return hello
*/
@GetMapping("/hello/v1")
Result<JSONObject> hello();
/**
*非常简单的一个回显接口,主要用于远程调用
*@return echo回显消息
*/
@RequestMapping(value = "/echo/{word}/v1", method = RequestMethod.GET)
Result<JSONObject> echo( @PathVariable(value = "word") String word);
}
package com.crazymaker.springcloud.user.info.controller;
...
@Api(value = "基础学习DEMO", tags = {"基础学习DEMO"})
@RestController
@RequestMapping("/api/demo")
public class DemoController {
//注入 @FeignClient注解所配置的客户端实例
@Resource
DemoClient demoClient;
@GetMapping("/say/hello/v1")
@ApiOperation(value = "Feign远程调用")
public Result<JSONObject> hello() {
Result<JSONObject> result = demoClient.hello();
JSONObject data = new JSONObject();
data.put("remote", result);
return Result.success(data).setMsg("操作成功");
}
}