Spring Cloud負載均衡:使用Feign做客戶端負載均衡

有了一篇服務端負載均衡後,再來一篇客戶端負載均衡,客戶端負載均衡很簡單,無需在zuul中作多餘配置(本示例不引入zuul),只須要在客戶端進行Feign引入和配置便可。spring

準備工做很簡單,實現客戶端負載均衡,首先須要Feign組件。app

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

在client啓動類中添加EnableFeignClients屬性,代碼以下:負載均衡

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}

而後編寫一個調用,示例代碼以下:spa

@FeignClient(value = "${service.request.name}", fallback = helloHystrix.class)
public interface hello extends BaseService{
    @RequestMapping(method = RequestMethod.GET, value = path + "/index/hello")
    String hello();
}

而後,在eureka中註冊相關服務提供者和調用者,以下圖所示:code

在本地輸入調用地址:http://localhost:8004/index/hello,結果以下:blog

最終在Feign的加持下,實現客戶端負載均衡(原理略)
over.get

相關文章
相關標籤/搜索