在上一篇中说到了SpringCloud各服务间接口调用有两种调用方式,在这一篇中将介绍另外一种调用方式——Feign。
Spring Cloud Feign整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现;与Ribbon不同的是,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。
(一)创建一个feign-consumer服务消费子工程
新建工程后,在pom.xml文件中引入以下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
创建一个application启动类:
package com.ningmeng.consumer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@EnableDiscoveryClient
@EnableFeignClients // 开启Feign功能
@SpringBootApplication
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
@Bean //可在这配置负载均衡策略 不配置默认为轮询策略,这里配置为随机策略,策略详细内容可查看README.md
public IRule iRule() {
return new RandomRule();
}
}
在service层写一个调用service-provider服务的接口:
package com.ningmeng.consumer.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "service-provider")//绑定该接口对应service-provider服务
public interface FeignConsumerService {
/**
* @RequestMapping属性要跟service-provider服务暴露初开的接口的属性一样 比如post请求方法要对应post请求方法,请求路径名要相同等等
* 方法中的参数也要与暴露的接口的参数一致(参数数量一致,名称一致(若名称不一致,要通过设置value同名))
* @return
*/
@GetMapping("/getApplicationName")
String feignConsumer(@RequestParam(value = "time") String time);
}
在controller层实现service层的接口达到调用的效果:
package com.ningmeng.consumer.controller;
import com.ningmeng.consumer.service.FeignConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignConsumerController {
@Autowired
FeignConsumerService feignConsumerService;
@GetMapping("/feignConsumer")
public String feignConsumer(@RequestParam(value = "time") String time){
return feignConsumerService.feignConsumer(time);
}
}
application.yml文件:
#工程端口号
server:
port: 4001
spring:
application:
name: feign-consumer #工程名,后面各个服务间调用接口要用到
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#eureka注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #eureka服务器所在的地址
(二)启动项目
依次启动eureka-server、service-provider(启动两个及以上实例,参考第二篇的内容)、feign-consumer工程
在eureka注册中心控制台可看到注册的服务:
然后多次访问feign-consumer的接口http://localhost:4001/feignConsumer?time=2017,可看到调用service-provider服务的/getApplicationName接口成功,并实现了负载均衡。
本篇完结!
github代码地址:https://github.com/huijunzeng/springCloudDemo.git