声明性REST客户端:Feign
对于feign,springCloud官方如是说:
Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters
。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。
使用三个测试项目:
先启动服务器和提供接口的客户端:
在eureka可以看到发现了一个微服务,在这个客户端有一个接口:
好,准备工作做完,开始写feign:
首先加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
完整pom文件如下:
<?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">
<parent>
<artifactId>SpringCloudTest</artifactId>
<groupId>com.xc.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>EurekaService_feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
启动类加入注解
@EnableFeignClients
完整如下:
package com.xc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class App_feign {
public static void main(String args[]){
SpringApplication.run(App_feign.class);
}
}
配置文件 改动端口以及微服务名称:
完整如下:
spring:
application:
name: test_feign
server:
port: 7777
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://xc:123@localhost:8761/eureka
instance:
prefer-ip-address: true
下面写一个feign接口:
package com.xc.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("cloud-test")
public interface TestFeign {
@RequestMapping(value = "/say/{words}",method = RequestMethod.GET)
public String sayHello(@PathVariable("words") String words);
}
其中
@FeignClient("cloud-test") 中的参数为eureka中的名称 也可修改为@FeignClient(url="localhost:7070")
此处接口应与cloud-test当中 要访问的接口一致。
然后写一个rest接口:
package com.xc.controller;
import com.xc.feign.TestFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestFeign testFeign;
@RequestMapping(value="/say/{word}",method = RequestMethod.GET)
public String say(@PathVariable String word){
return testFeign.sayHello(word);
}
}
ok,开始测试:
访问http://192.9.8.3:7777/say/haha
成功获取到数据。