一.Feign
Feign使得
Java HTTP
客户端编写更方便。Feign
灵感来源于Retrofit、JAXRS-2.0和WebSocket。Feign
最初是为了降低统一绑定Denominator 到HTTP API
的复杂度,不区分是否支持Restful。
SpringCloud对Feign进行了整合,并且使用起来非常简单方便,接下来使用上一篇文章中的工程作为基础进行讲解。
二.Feign的使用
创建新的SpringBoot项目作为FeignClient。
1.添加依赖
<groupId>com.ywp</groupId> <artifactId>FeignClient</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>FeignClient</name> <description>Demo for FeignClient</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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>Finchley.M7</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- Feign依赖 --> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
2.修改application.properties
#服务端口
server.port=6002
#eureka主机名,会在控制页面中显示
eureka.instance.hostname=localhost
#eureka服务器地址
eureka.client.service-url.defaultZone=http://localhost:7001/eureka/
#服务名
spring.application.name=feign-01
3.添加注解
在启动类FeignClientApplication上添加@EnableFeignClients注解
@SpringBootApplication
@EnableFeignClients
@EnableConfigurationProperties
public class FeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
}
}
4.添加Service发起调用
创建FileService接口,添加@FeignClient注解,并且添加接口方法getInfo
@FeignClient(value = "fileServer")
public interface FileService {
@RequestMapping(value = "/static/info", method = RequestMethod.POST)
public String getInfo();
}
@FeignClient:
value:需要访问的服务名,即服务的spring.application.name
@RequestMapping: 与被调服务对应的接口相同,此处表示请求路径为/static/info,请求方法为POST
添加Controller调用getInfo()方法用于测试。
@Controller
public class MainController {
@Autowired
FileService fileService;
@RequestMapping(value = "fileServerInfo", method = RequestMethod.GET)
@ResponseBody
public String fileServerInfo(HttpServletResponse response) {
return fileService.getInfo();
}
}
5.测试
1) 依次启动EurekaServer、FileServer、FeignClient。
2) 访问Eureka地址http://localhost:7001/ ,查看是否注册成功
3) 访问http://localhost:6002/fileServerInfo
可以发现FeignClient收到/fileServerInfo请求后调用了FileServer的/static/info接口,并且将返回数据响应到浏览器端。