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 "用户信息查询出现异常!"; } }
上一篇: 运算符