当前位置: 首页 > news >正文

网站宣传视频艺麟盛世可以做网站推广吗

网站宣传视频,艺麟盛世可以做网站推广吗,网页制作与网站开发,旌阳移动网站建设OpenFeign的使用 openFeign的作用是服务间的远程调用 ,比如通过OpenFeign可以实现调用远程服务。 已经有了LoadBalancer为什么还要用openFeign? 在微服务架构中,LoadBalancer和OpenFeign虽然都提供了服务间调用的能力,但它们的设计目的和…

OpenFeign的使用

openFeign的作用是服务间的远程调用 ,比如通过OpenFeign可以实现调用远程服务。

已经有了LoadBalancer为什么还要用openFeign?

  • 在微服务架构中,LoadBalancer和OpenFeign虽然都提供了服务间调用的能力,但它们的设计目的和使用场景有所不同。
  • LoadBalancer主要关注于服务间的负载均衡,它可以帮助客户端在多个服务实例之间分配请求,以实现高可用性和性能优化。
  • 而OpenFeign则提供了一种声明式的Web服务客户端编程模型,它使得编写服务间调用的代码更加简洁和直观。

OpenFeign到底是什么?

官方文档地址,https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/。

OpenFeign是Spring cloud框架中集成的声明式HTTP客户端工具。

OpenFeign可以让远程调用服务达到像本地调用方法一样的体验。

OpenFeign怎么用?

OpenFeign在服务消费端使用,比如某个服务使用OpenFeign调用其它服务

1.引入依赖,在项目 的pom文件添加:

<!-- openfeign 远程调用 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.在项目启动类上添加@EnableFeignClients注解,开启openFeign功能

@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient			
public class TlmallOrderApplication {

3.编写OpenFeign客户端,调用其它微服务

@FeignClient(name = "tlmall-storage")
public interface StorageServiceFeignClient {@PostMapping("/storage/reduce-stock")Result<?> reduceStock(@RequestBody StorageDTO productReduceStockDTO);}@FeignClient(name = "tlmall-account")
public interface AccountServiceFeignClient {@PostMapping("/account/reduce-balance")Result<?> reduceBalance(@RequestBody AccountDTO accountReduceBalanceDTO);}

4.服务发起调用 ,像调用本地方式一样调用其它服务

@Autowiredprivate AccountServiceFeignClient accountService;@Autowiredprivate StorageServiceFeignClient storageService;public Result<?> createOrder(String userId, String commodityCode, Integer count) {// deduct storageStorageDTO storageDTO = new StorageDTO();storageDTO.setCommodityCode(commodityCode);storageDTO.setCount(count);//RestTemplate远程调用//String storage_url = "http://localhost:8010/storage/reduce-stock";//整合了Nacos+LoadBalaner,可以使用微服务名tlmall-storage代替localhost:8020//String storage_url = "http://tlmall-storage/storage/reduce-stock";//Integer storageCode = restTemplate.postForObject(storage_url,storageDTO, Result.class).getCode();//openFeign远程调用Integer storageCode = storageService.reduceStock(storageDTO).getCode();if (storageCode.equals(COMMON_FAILED.getCode())) {throw new BusinessException("stock not enough");}// deduct balanceint price = count * 2;AccountDTO accountDTO = new AccountDTO();accountDTO.setUserId(userId);accountDTO.setPrice(price);//RestTemplate远程调用//String account_url = "http://localhost:8020/account/reduce-balance";//整合了Nacos+LoadBalaner,可以使用微服务名tlmall-account代替localhost:8020//String account_url = "http://tlmall-account/account/reduce-balance";//Integer accountCode = restTemplate.postForObject(account_url, accountDTO, Result.class).getCode();//openFeign远程调用Integer accountCode = accountService.reduceBalance(accountDTO).getCode();if (accountCode.equals(COMMON_FAILED.getCode())) {throw new BusinessException("balance not enough");}
}

重启服务,验证

1.到Nacos控制台查看服务是否注册成功
2.进行测试访问url
3.借助idea工具,复制一个服务,再次操作,看当有2个服务时,能否把2个都调用了

http://www.yayakq.cn/news/562033/

相关文章:

  • 创客贴网站建设怎么找推广渠道
  • 建设个电商平台网站需要多少钱wordpress h5自适应
  • 广州建设档案馆网站宜昌市夷陵区建设局网站
  • 网站建设说明生活中花钱请人做网站
  • 网站建设及上线流程石家庄整站优化技术
  • 胶州网站搭建公司免费注册自己的网站
  • 设计素材网站哪个最好免费设计素材网址
  • 肥西上派网站开发博客建站模板
  • 个人设计师网站wordpress倒闭汉化组
  • 上传视频网站开发好的做网站公司
  • 网站开发怎么入驻京东管理咨询公司有哪些方面
  • 导航网站移动端流量占比常州建站价格
  • 做期货应该看的网站自建电梯费用
  • 网站设计成品广西网联电线电缆有限公司
  • 白云区网站开发建立网站 知乎
  • 网站搭建中企动力第一百度应用商店官网
  • 全国设计网站公司网站seo常用方法
  • 南宁月嫂网站建设高质量网站外链平台
  • 苏州市网站建设服务学做转手绘的网站
  • 微信 公众号 微网站开发网站开发与维护工资多少
  • 包头公司做网站政务网站信息化建设情况汇报
  • 百度上面做企业网站怎么做家具网站设计方案
  • 网站建设策略阿里巴巴app开发和网站开发的区别
  • 怎么做足球直播网站衣联网和一起做网站。哪家强
  • 网站app免费制作创业平台app
  • 网站主体注销秒收录的网站
  • 集团网站建设详细策划成都电商网站开发公司
  • 深圳专业制作网站公司吗毕业设计 做网站
  • 百度回收网站搭建大连企业自助建站
  • wordpress商城安装教程胶州网站优化价格