java使用Feign实现声明式Restful风格调用
一、feign简介
feign是netflix开发的声明式、模板化的http客户端,在使用时就像调用本地(服务消费者自己)的方法一般,帮助我们更加优雅的调用服务提供者的api。feign自身支持springmvc,还整合了eureka、ribbon,极大的简化了feign的使用。就整合euraka而言,只需和普通的服务配置eureka server的信息即可。整合ribbon,就意味着不再需要通过标注@loadbalanced的实例化后的resttemplate去调用服务提供者方法了。feign只需通过简单的定义一个接口即可实现负载均衡。
二、在服务消费者中使用feign
1、添加feign依赖
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-feign</artifactid> </dependency>
2、创建一个feign接口,并在头部加上@feignclient注解
import com.simons.cn.util.commonresult; import org.springframework.cloud.netflix.feign.feignclient; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; @feignclient(name = "user-provider") public interface userfeignservice { @requestmapping(value = "/getuserinfo",method = requestmethod.get) commonresult getuserbyname(@requestparam(required = false,value = "name") string name); }
这里的name="user-provider" 会被解析为注册到eureka server上的其中一个客户端,换句话说就是注册到eureka中的其中一个服务,利用它可以实现负载均衡。也可以结合value来指定@feignclient(name="user-provider",value = "http://localhost:8000/")
3、修改controller,不再调用@loadbalanced标注的resttemplate,而是通过标注@feignclient的自定义接口
import com.simons.cn.userfeignservice; import com.simons.cn.util.commonresult; import lombok.extern.slf4j.slf4j; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; @slf4j @restcontroller public class ticketfeigncontroller { @autowired private userfeignservice userfeignservice; @getmapping("/ticketpurchase") public commonresult purchaseticket(@requestparam(required = false,value = "name") string name){ commonresult result = userfeignservice.getuserbyname(name); return result; } }
4、修改启动类,头部添加@enablefeignclients注解
import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; import org.springframework.cloud.netflix.feign.enablefeignclients; @enablefeignclients @enablediscoveryclient @springbootapplication public class ticketconsumerfeignapplication { public static void main(string[] args) { springapplication.run(ticketconsumerfeignapplication.class, args); } }
测试:
启动多个user-provider-eureka服务实例,其配置文件中的application.name=user-provider;
启动discovery-eureka服务实例;
启动ticket-consumer-feign服务实例
如上测试结果可以看到ticket-consumer-feign消费者顺利调用user-provider-eureka服务提供者的方法,并且实现了负载均衡。
三、使用feign构造多参数请求
1、get请求:多个参数就用多个@requestparam标注几个
@feignclient(name = "user-provider") public interface userfeignservice { @requestmapping(value = "/getuserinfo",method = requestmethod.get) commonresult getuserbyname(@requestparam(required = false,value = "name") string name); }
或者用map来封装参数
@feignclient(name="user-provider") public interface userservicefeign { @requestmapping(value = "/getuserinfo",method = requestmethod.get) public commonresult getuserbyname(@requestparam map<string,object> map); }
@restcontroller public class ticketcontroller { @autowired private userservicefeign userservicefeign; @getmapping("ticketpurchase") public commonresult (long id, string actid) { map map = new hashmap<string, object>(); map.put("id", id); map.put("actid", actid); return this.userservicefeign.getuserbyname(map); } }
2、post请求就相对简单的多
// 服务消费者方 @feignclient(name="user-provider") public interface userservicefeign { @requestmapping(value="/getuserbyname",method = requestmethod.post) public commonresult getuserbyname(@requestbody user user); }
//服务提供者 @slf4j @restcontroller public class usercontroller { @autowired private userserviceimpl userservice; @getmapping(value = "/getuserinfo") public commonresult getuserinfo(@ruquestbody user user){ list<user> userlist = userservice.getuserbyname(user.getname()); return commonresult.success(commonenum.sucess.getcode(), commonenum.sucess.getmessage(),userlist); } }
项目的github
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 简单的观察者模式示例分享
下一篇: shell 命令之wc