pom文件
导入包
<!-- spring-cloud-feign -->
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>${feign-httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
<version>${feign-httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-gson</artifactId>
<version>${feign-httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>1.1.6.RELEASE</version>
</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.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<!--调用其他微服务-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<!-- spring-cloud-feign end -->
<!-- 后来发现只加入这一个包就可以了 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-core</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
写一个接口 FeignInterface
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* Created by Administrator on 2017/2/5.
*/
@FeignClient(name="ribbonserver", url="http://localhost:8080")
public interface FeignInterface {
@RequestMapping(value = "/baseBackStage/index", method = RequestMethod.GET)
ResponseEntity<String> index();
}
新建FeignController
import feign.Feign;
import feign.Logger;
import feign.codec.Decoder;
import feign.gson.GsonDecoder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Base64;
import java.util.List;
@Api(value = "Feign测试使用",description = "Feign测试使用")
@RequestMapping("/feigntest")
@RestController
public class FeignController {
@Autowired
FeignInterface feign;
@ApiOperation(value = "Feign主测试使用")
@ApiImplicitParams({})
@RequestMapping(value = "/index" ,method = RequestMethod.GET)
public ResponseEntity<String > helloWord() {
System.out.println("test Return================1");
Decoder decoder = new GsonDecoder();
ResponseEntity<String> responseEntity = feign.index();
String contributors = responseEntity.getBody();
System.out.println("test Return=="+contributors);
return ResponseEntity.ok("hello word");
}
}
main入口要增加注解 @EnableFeignClients
注意: 如果启动时出现Feign注入错误,应该是Controller层用了返回对象的情况,目前我测试的只能返回简单类型,例如:map string ,只要使用了其他的类型就会出现错误,可能我还没有研究透彻,如有不同意见请提意见,谢谢
我在启动的时候出现过空指针的异常,找了好久才发现原来是Swagger2的版本包有问题,使用2.6以上版本就不会出现此类问题了