SpringCloud 中Feign的简单使用
-
导入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
-
启动类添加注解@EnableFeignClients
@SpringBootApplication @EnableFeignClients public class ClientdemoApplication { public static void main(String[] args) { SpringApplication.run(ClientdemoApplication.class, args); } }
-
创建API接口
//方式一:配置url //@FeignClient(name = "eurekaserver",url = "http://eureka1.com:8081") //方式二:配置name,此name要与要与服务名的纯小写相同 @FeignClient(name = "eurekaserver") public interface FeignTest { @GetMapping(path = "/shello") String shello(); }
-
注入调用
@Autowired private FeignTest feignTest; public Object service4() { String hello = feignTest.shello(); System.out.println(hello); return "service3: " + hello; }