Feign
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。
Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
简而言之:
- Feign 采用的是基于接口的注解
- Feign 整合了ribbon
准备工作
启动项目
- Ahut-Eureka-Server(注册中心)
- Ahut-Eureka-Client-One(AHUT-EUREKA-CLIENT服务一)
- Ahut-Eureka-Client-Two(AHUT-EUREKA-CLIENT服务二)
浏览器访问Eureka
此时的项目架构
创建服务消费者
新创建一个项目Ahut-Service-Feign,和Ahut-Eureka-Client-One类似
右键,New>>Module>>Spring Init>>填写项目maven信息>>选择依赖>>填写项目名称
pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ahut</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-feign</name>
<description>service feign</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka disvovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目启动类
加上@EnableFeignClients注解开启Feign的功能
package com.ahut;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
/**
* @author cheng
* @className: ServiceFeignApplication
* @description: service feign
* @dateTime 2018/4/26 16:36
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
项目配置文件application.properties
# 配置服务器端口
server.port=8930
# spring项目名称
spring.application.name=AHUT-SERVICE-FEIGN
# 配置注册中心
eureka.client.service-url.defaultZone=http://localhost:8900/eureka/
声明一个接口HelloService
通过@ FeignClient(“服务名”),来指定调用哪个服务
package com.ahut.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author cheng
* @interfaceName: HelloService
* @description:
* @dateTime 2018/4/26 16:39
*/
@FeignClient(value = "AHUT-EUREKA-CLIENT")
public interface HelloService {
@RequestMapping(value = "/v1/showPort")
String showPort();
/**
* @description: 演示带参数
* @author cheng
* @dateTime 2018/4/26 16:45
*/
//@GetMapping(value = "/demo")
//void demo(@RequestParam(value = "name") String name);
}
控制层HelloAction
package com.ahut.action;
import com.ahut.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author cheng
* @className: HelloAction
* @description:
* @dateTime 2018/4/26 14:40
*/
@RestController
@RequestMapping(value = "/v1")
public class HelloAction {
@Autowired
private HelloService helloService;
/**
* @description: 模拟调用
* @author cheng
* @dateTime 2018/4/26 14:42
*/
@RequestMapping(value = "/hello")
public String hello() {
return helloService.showPort();
}
}
启动项目
- Ahut-Eureka-Server(注册中心)
- Ahut-Eureka-Client-One(AHUT-EUREKA-CLIENT服务一)
- Ahut-Eureka-Client-Two(AHUT-EUREKA-CLIENT服务二)
- Ahut-Service-Feign(AHUT-SERVICE-FEIGN服务消费者)
访问eureka