Spring Cloud feign

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以上版本就不会出现此类问题了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值