SpringCloud Feign使用详解
添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
创建启动类:
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(UserServiceApplication.class).web(true).run(args);
}
}
写一个feign client:
@FeignClient(name = "这里写服务名称")
public interface UserServiceAPI {
@RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)
public BaseResponse<UserInfo> getUserInfo(@RequestParam("userId")Integer userId);
测试:
@RestController
public class UserServiceAPITestController {
@Autowired
UserServiceAPI userServiceAPI;
@RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)
public BaseResponse<KylinUserInfo> getUserInfoByUserId(Integer userId) {
return userServiceAPI.getUserInfo(userId);
}
}
浏览器访问:localhost:8080/user/getUserInfo
最新文章
- Java多线程学习(二)
- HttpContext的dynamic包装器DynamicHttpContext (附原代码)
- C# 多线程详解 Part.04(Lock、Monitor、生产与消费)
- JS-数组的方法
- POJ1985Cow Marathon[树的直径]
- classpath、path、JAVA_HOME的作用
- LPHW-积累-ex1-6
- eclipse插件开发中全局对象的获取
- ASP.NET程序读取二代身份证(附源码)
- Angular报错记录
- APP闪退问题
- List去重的实现
- 17、字符串转换整数 (atoi)
- 阿里巴巴开源的Asynchronous I/O Design and Implementation
- 【NOI2019模拟】搬砖
- FTP 其他设置
- 运行和控制Nginx——命令行参数和信号
- [转][C#]枚举的遍历Enum
- 深入理解python里面类的对象的赋值
- 微信小程序wxml文件中调用自定义函数