当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot 2.0 Feign的客户端

spring boot 2.0 Feign的客户端

2019年04月29日  | 移动技术网IT编程  | 我要评论

1.pom.xml

<dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-starter-openfeign</artifactid>
            <version>2.0.2.release</version>
        </dependency>

2.userconsumerdemoapplication.java

@enablefeignclients

 

3.userclient.java

package cn.itcast.user.client;

import cn.itcast.user.pojo.user;
import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;

@feignclient("user-service")
public interface userclient {
    @getmapping("{id}")
    user getuserqueryinfo(@pathvariable("id") long id);
}

 

4.userfcontroller.java

package cn.itcast.user.controller;

import cn.itcast.user.client.userclient;
import cn.itcast.user.pojo.user;
import com.netflix.hystrix.contrib.javanica.annotation.defaultproperties;
import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand;
import com.netflix.hystrix.contrib.javanica.annotation.hystrixproperty;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import org.springframework.web.client.resttemplate;

@restcontroller
@requestmapping("consumerf")
@defaultproperties(defaultfallback = "queryuserbyidfallback")
public class userfcontroller {
    @autowired
    private userclient userclient;

    @getmapping("{id}")
    public user queryuserbyid(@pathvariable("id") long id){
        return userclient.getuserqueryinfo(id);
    }

    public string queryuserbyidfallback(){
        return "用户信息查询出现异常!";
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网