产品营销类网站,电子版证件照免费制作微信小程序,wordpress给文章分类,销售网络平台推广上一篇集成了ZuulGateway和Eureka并进行了测试。在实际场景中#xff0c;我们肯定会有很多的微服务#xff0c;而他们之间可能会存在相互调用的关系#xff0c;那么#xff0c;如何优雅的处理服务之间的调用问题呢#xff1f;接下来就是我们要解决的。简单的说下FeignFeig…上一篇集成了ZuulGateway和Eureka并进行了测试。在实际场景中我们肯定会有很多的微服务而他们之间可能会存在相互调用的关系那么如何优雅的处理服务之间的调用问题呢接下来就是我们要解决的。简单的说下FeignFeign 是一个声明式REST Web服务客户端可以处理微服务间的Web服务调用。他是使用注解加接口的形式形成去调用服务的相对来说不是很难有兴趣可去官方地址了解下。这里不多介绍。如何用这里我们还是基于之前的Spring cloud demo去改造老规矩先附上源码地址spring cloud demo步骤这里Consumer与Provider分别代表两个微服务测试时使用Controller通过Feign调用Provider。调用流程如下 网关zuul - consumer - provider1,引入依赖org.springframework.cloud spring-cloud-starter-openfeign2.在Consumer的启动类上增加注解开启Feign的支持EnableFeignClients3.在Consumer新增Controller以供测试时调用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(); } }4.在Consumer定义Feingn接口 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(); }5.在Provider中新增REST接口这里主要用于测试供Consumer调用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; } }6.使用Postman请求Consumer测试观察得知Postman请求到网关之后分发到consumer微服务微服务通过Feign接口调用Provider微服务并接收到返回值之后原路返回到Consumer。当然这里只是简单的演示下如何使用Feign实际生产环境中使用远不止这么简单这就需要我们慢慢去摸索了...最后到这里我们的基本框架已经搭建完成我们用SpringCloud集成了网关(Zuul)还加入了服务发现与注册(Eureka)也演示了微服务间的调用并集成了Feign。那么基于以上我们会发现还是会有些场景没有解决。比如我的配置都在properties里面参数都是写死的到线上后怎样在不重启服务的情况下修改参数怎样进行灰度发布或金丝雀测试还有我们的微服务已经通过Feign可以相互调用了那我怎样监测他们的运行情况如果出故障时如何快速的知道并修复数据量太大一台扛不住又该如何在SpringCloud中又如何处理分库分表读写分离