客户端Feign
- 在笔记0x2的基础上,启动以下应用
- eurekaserver,端口:8767
- eurekaclient,端口:8662
- eurekaclient,端口:8664
- 新建工程 eurekafeign -> 增加Feign起步依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- 配置文件:端口8665,注册中心仍为8767
- 添加启动类注解@EnableFeignClient,开启Feign
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class EurekafeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekafeignApplication.class, args);
}
}
- 定义一个feigh接口,@ FeignClient(“服务名”),指定调用的服务
@FeignClient("eurekaclient")
public interface InvokeService {
@RequestMapping(value = "/nice", method = RequestMethod.GET)
String sayService(@RequestParam(value = "name") String name);
}
上面调用了eurekaclient服务的"/nice"接口
- 暴露一个"/feign"接口,通过上面定义的InvokeService来消费服务
@RestController
public class InvokeController {
@Autowired
InvokeService invokeService;
@GetMapping(value = "/feign")
public String invokeService(@RequestParam String name) {
return invokeService.sayService(name);
}
}
ribbon vs feign
- ribbon+restTemplate的方式使用url进行调用
feign使用@ FeignClient(“服务名”)进行服务调用
- Feign 整合了ribbon,具有负载均衡的能力
整合了Hystrix,具有熔断的能力
客户端Feign
上面调用了eurekaclient服务的"/nice"接口
hello hugeterry ,port:8664
hello hugeterry ,port:8662
ribbon vs feign
feign使用@ FeignClient(“服务名”)进行服务调用
整合了Hystrix,具有熔断的能力