标签:var 字符串 class 服务 interface java对象 mes ace bin
OpenFeign是REST服务客户端,REST其实就是HTTP啦,所以OpenFeign其实就是HTTP客户端,那么他和HttpClient有什么不同呢
第一步引入OpenFeign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
第二步启动OpenFeign客户端功能
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
第三步编写REST服务接口
@FeignClient("stores")
public interface StoreClient {
@RequestMapping(method = RequestMethod.GET, value = "/stores")
List<Store> getStores();
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") Long storeId, Store store);
}
在@FeignClient中的字符串称为Feign客户端名字,它可以是任意的字符串,它的作用是用来配置Rabbin或Spring Cloud LoadBalancer负载均衡(后面会详细介绍如何做)。
在@FeignClient中还可以设置url参数,它表示提供REST服务的地址,如果你没有设置url参数,那么就要在配置文件中配置。
之后我们就可以把StoreClient注入到我们需要使用的地方啦。
Spring Cloud OpenFeign REST服务客户端
标签:var 字符串 class 服务 interface java对象 mes ace bin
原文地址:https://www.cnblogs.com/stronger-brother/p/13672138.html