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

相关文章
相关标签/搜索