Spring Cloud提供了Ribbon和Feign作为客户端的负载均衡。
前面使用了Ribbon做客户端负载均衡,使用Hystrix做容错保护,这两者被作为基础工具类框架被广泛地应用在各个微服务的实现中。SpringCloudFeign是将两者做了更高层次的封装以简化开发。它基于Netflix Feign实现,整合了SpringCloud Ribbon和SpringCloud Hystrix,除了提供这两者的强大功能外,还提供了一种声明是的Web服务客户端定义的方式。SpringCloud Feign在NetFixFeign的基础上扩展了对SpringMVC注解的支持,在其实现下,我们只需创建一个接口并用注解的方式来配置它,即可完成对服务提供方的接口绑定。简化了SpringCloud Ribbon自行封装服务调用客户端的开发量。
在Spring Cloud下,使用Ribbon直接注入一个RestTemplate对象即可,此RestTemplate已做好负载均衡的配置;而使用Feign只需定义个注解,有@FeignClient注解的接口,然后使用@RequestMapping注解在方法上映射远程的REST服务,此方法也是做好了负载均衡配置。
- 新建项目 feign-consumer https://start.spring.io/
- 更新配置 application.properties
1
2
3
| spring.application.name=compute-consume-feign
server.port=2231
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
|
- 启动类加注解 @EnableFeignClients
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| package com.baoguoding.feignconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
| package com.baoguoding.feignconsumer.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("compute-service")
public interface HelloService {
@RequestMapping(value="/add", method = RequestMethod.GET)
String hello(@RequestParam("a") Integer a, @RequestParam("b") Integer b, @RequestParam("sn") Integer sn);
}
|
- 添加服务类 AddConsumerController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| package com.baoguoding.feignconsumer.service;
import com.baoguoding.feignconsumer.remote.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicInteger;
@RestController
public class AddConsumerController {
@Autowired
HelloService helloService;
private AtomicInteger sn = new AtomicInteger(0);
@RequestMapping(value="/feign-consumer", method = RequestMethod.GET)
public String helloConsumer(@RequestParam Integer a, @RequestParam Integer b) {
return helloService.hello(a, b, sn.incrementAndGet());
}
}
|
- 访问 http://127.0.0.1:2231/feign-consumer?a=1&b=3
- 熔断处理,服务降级,新增类 HelloServiceFallback
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package com.baoguoding.feignconsumer.remote;
import org.springframework.stereotype.Service;
@Service
public class HelloServiceFallback implements HelloService {
@Override
public String hello(Integer a, Integer b, Integer sn) {
System.out.println("HelloServiceFallback");
return "fallback";
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package com.baoguoding.feignconsumer.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//@FeignClient("compute-service")
@FeignClient(name="compute-service", fallback=HelloServiceFallback.class)
public interface HelloService {
@RequestMapping(value="/add", method = RequestMethod.GET)
String hello(@RequestParam("a") Integer a, @RequestParam("b") Integer b, @RequestParam("sn") Integer sn);
}
|
- 打开compute 下面的随机函数
- 更新配置 application.properties
1
| feign.hystrix.enabled=true
|
- 访问 http://127.0.0.1:2231/feign-consumer?a=1&b=3
参考