欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot 2.0 Feign的客户端

程序员文章站 2022-04-08 23:41:58
1.pom.xml 2.UserConsumerDemoApplication.java 3.UserClient.java 4.UserFController.java ......

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 "用户信息查询出现异常!";
    }
}