feign一个接口多个方法_spring cloud 建一个服务消费者client-feign(最好用这种方式)...
Feign是一個聲明式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要創建一個接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign默認集成了Ribbon,并和Eureka結合,默認實現了負載均衡的效果。
簡而言之:
Feign 采用的是基于接口的注解
Feign 整合了ribbon
新建一個spring-boot工程,取名為serice-feign,在它的pom文件引入Feign的起步依賴spring-cloud-starter-feign、Eureka的起步依賴spring-cloud-starter-eureka、Web的起步依賴spring-boot-starter-web
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
server.port=8765
spring.application.name=client-feign
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientfeignApplication {
public static void main(String[] args) {
SpringApplication.run(ClientfeignApplication.class, args);
}
}
定義一個feign接口,通過@ FeignClient(“服務名”),來指定調用哪個服務。比如在代碼中調用了service-hi服務的“/hi”接口,代碼如下:
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
定義controller
@RestController
public class HiController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
SchedualServiceHi schedualServiceHi;
@RequestMapping(value = "/hi")
public String sayHi(@RequestParam String name){
logger.info("feign ====> "+name);
return schedualServiceHi.sayHiFromClientOne(name);
}
}
總結
以上是生活随笔為你收集整理的feign一个接口多个方法_spring cloud 建一个服务消费者client-feign(最好用这种方式)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot2 虚拟路径设置_转
- 下一篇: 文件上怎么盖章_投标文件该怎么盖章呢?投