上一篇集成了zuulgateway和eureka并进行了测试。在实际场景中,我们肯定会有很多的微服务,而他们之间可能会存在相互调用的关系,那么,如何优雅的处理服务之间的调用问题呢?接下来就是我们要解决的。
feign 是一个声明式rest web服务客户端,可以处理微服务间的web服务调用。他是使用注解加接口的形式形成去调用服务的,相对来说不是很难,有兴趣可去官方地址了解下。这里不多介绍。
这里我们还是基于之前的spring cloud demo去改造,老规矩先附上源码地址
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-openfeign</artifactid> </dependency>
@enablefeignclients
package cn.kxtop.blog.consumer.controller; import cn.kxtop.blog.consumer.client.providerclient; 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.postmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @slf4j @restcontroller @requestmapping("/feign") public class testfeigncontroller { @autowired private providerclient providerclient; @getmapping public string get() { log.info("consumer feign get action"); return providerclient.get(); } @postmapping public string post() { log.info("consumer feign post action"); return providerclient.post(); } }
package cn.kxtop.blog.consumer.client; import org.springframework.cloud.openfeign.feignclient; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.postmapping; @feignclient(name = "kxtop-provider", path = "/api/test-feign") public interface providerclient { @getmapping("/") string get(); @postmapping("/") string post(); }
package cn.kxtop.blog.provider.controller; import lombok.extern.slf4j.slf4j; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.postmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @slf4j @restcontroller @requestmapping("/test-feign") public class testfeigncontroller { @getmapping public string get() { log.info("provider feign get action"); return "test feign get"; } @postmapping public string post() { log.info("provider feign post action"); return "test feign post"; } }
到这里,我们的基本框架已经搭建完成,我们用springcloud集成了网关(zuul),还加入了服务发现与注册(eureka),也演示了微服务间的调用并集成了feign。
那么基于以上,我们会发现还是会有些场景没有解决。比如,我的配置都在properties里面,参数都是写死的,到线上后怎样在不重启服务的情况下修改参数?怎样进行灰度发布或金丝雀测试?还有我们的微服务已经通过feign可以相互调用了,那我怎样监测他们的运行情况?如果出故障时,如何快速的知道并修复?数据量太大,一台扛不住又该如何?在springcloud中又如何处理分库分表读写分离?
别急,后面我们都会讲到...
持续学习,记录点滴。更多文章请访问
如对本文有疑问, 点击进行留言回复!!
网友评论