上一篇我们介绍了服务消费者restTemplate+ribbon ,本篇我们介绍下服务消费者Fegin。
Fegin是什么?
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
简而言之:Feign 采用的是基于接口的注解 ,Feign 整合了ribbon
我们在sim-consumer工程使用Feign客户端
pom.xml 新加入 spring-cloud-starter-feign 起步依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
springboot启动类上,新添加 @EnableFeignClients注解开启Fegin客户端
package com.tingcream.simConsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
//@EnableEurekaClient //已过时不用了
@EnableDiscoveryClient
@EnableFeignClients //启用Feign客户端
public class SimConsumerApp extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(SimConsumerApp.class);
}
public static void main(String[] args) {
SpringApplication.run(SimConsumerApp.class, args);
}
}
编写Feign的java Api接口 :
HelloFeignApi.java
package com.tingcream.simConsumer.clientApi;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("sim-provider")
public interface HelloFeignApi {
@GetMapping( "/hello")
public String hello();
@GetMapping( "/hello2")
public String hello2(@RequestParam("name") String name);
}
sim-provider 服务提供者,提供对应的http REST 接口
HelloProviderController.java
package com.tingcream.simProvider.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloProviderController {
@Value("${spring.application.name}")
private String applicationName;
@Value("${server.port}")
private String port ;
@GetMapping("/hello")
public String hello(){
return "你好,我是:"+applicationName+",服务端口:"+port;
}
@GetMapping("/hello2")
public String hello2( String name){
return "你好,"+name+",我是:"+applicationName+",服务端口:"+port;
}
}
sim-consumer 服务消费者 ,使用@AutoWired注入 HelloFeignApi bean后,直接调用其中定义的方法即可
package com.tingcream.simConsumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.tingcream.simConsumer.clientApi.HelloFeignApi;
@RestController
public class HelloConsumerController {
private String PRE_URL="http://SIM-PROVIDER/";
@Autowired
private RestTemplate restTemplate;
@Autowired
private HelloFeignApi helloFeignApi;
@GetMapping("/hello")
public String hello(){
return helloFeignApi.hello();
}
@GetMapping("/hello2")
public String hello2(String name){
return helloFeignApi.hello2(name);
}
}
分别启动Eureka Server 、sim-provider 7001 、sim-provider 7002 、 sim-consumer应用。
浏览器访问http://localhost/hello , http://localhost/hello2?name=xiaoming ,刷新页面时,发现Fegin默认自动帮我们配上了负载均衡 。
注意:Feign会基于用户使用的注解声明来进行解析,生成最终http的调用模板,所以 HelloFeignApi 接口中声明hello2方法时,方法形参name前面的@RequestParam("name") 不能省略,否则Feign不能正常工作。