SpringCloud实战:服务消费者(Feign)
一、Feign概述
Feign是一款声明式的HTTP客户端工具,能够简化HTTP请求的编写过程。开发者只需定义接口并添加相应的注解即可实现HTTP调用。该框架支持多种注解方式,包括Feign自身的注解及JAX-RS标准注解。此外,Feign还具备灵活的编解码器扩展能力。默认情况下,Feign集成了Ribbon组件,并与Eureka服务发现机制集成,从而自动实现客户端负载均衡。
总结来说:
- Feign基于接口方式进行注解配置
- Feign集成了Ribbon组件
二、环境准备
沿用前文项目结构,启动Eureka注册中心,监听端口1111;同时启动两个service-hi实例,分别运行在2222和3333端口。
三、构建Feign服务
创建一个新的Spring Boot项目,命名为service-feign,在其pom.xml中引入以下依赖项:
- spring-cloud-starter-feign:用于Feign功能
- spring-cloud-starter-eureka:用于服务注册与发现
- spring-boot-starter-web:提供Web开发支持
<?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.example.springcloud</groupId>
<artifactId>feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>feign</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</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>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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-web</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>
在application.properties中配置应用名称、端口以及Eureka服务器地址:
server.port=5555
spring.application.name=service-feign
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
创建一个Feign接口,使用@FeignClient("服务名称")标注来指定要调用的目标服务。示例中调用了service-hi服务中的/hi路径,如下所示:
package com.example.springcloud.feign.service;
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(value = "service-hi")
public interface RemoteService {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String greet(@RequestParam(value = "name") String userName);
}
在控制器层暴露一个外部访问接口/hi,通过上述定义的Feign客户端调用远程服务:
package com.example.springcloud.feign.web;
import com.example.springcloud.feign.service.RemoteService;
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;
@RestController
public class GreetingController {
@Autowired
private RemoteService remoteService;
@RequestMapping(value = "/hi", method = RequestMethod.GET)
public String greeting(@RequestParam String name) {
return remoteService.greet(name);
}
}
在主程序入口类FeignApplication中启用Feign功能:
package com.example.springcloud.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
启动服务后,连续访问http://localhost:5555/hi?name=forezp,浏览器将轮流返回不同实例的结果。