当前位置: 首页 > 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/827356/

相关文章:

  • 网络推广话术开场白seo长尾关键词
  • 网站优化什么意思网页设计接单
  • 学习网站建设有什么用中国建设人才招聘官网
  • 网站开发工程师 能做什么单页设计软件
  • 医院网站建设存在问题广州海珠区繁华吗
  • asp.net网站开发项...wordpress安装好之后怎么用
  • 网站的数据库怎么备份建一个网站容易吗
  • 手机上做整蛊网站项目经理
  • 网站建设的目标客户分析大连网龙建站优化推广
  • 个人外贸网站wordpress自己添加menu
  • wordpress 网站变慢定制网站建设托管
  • 深圳网站建设_企业网站设计定制苏州有几个区
  • 青岛网站优化公司wordpress无法上传文件
  • 西宁做网站好的公司美食网站 怎么做
  • 泰安网站建设538sw建设网站制作公司如何选择
  • 承德建站公司网站建设维护的职位
  • 建设一个购物网站需要多少钱互推商盟
  • 推广 网站建设营销渠道分为三种模式
  • 常用网站名称大全seoul是什么意思
  • 进入网站服务器怎么做一些常用的网站
  • 个人在湖北建设厅网站申请强制注销导航类网站模板
  • 常宁网站企业网站建设服务内容
  • 网站排名logo怎么做上海专业seo排名优化
  • 网站的seo方案怎么做快速搭建论坛
  • 网站建设需要哪些技术人员安徽省住房城乡建设厅网站官网
  • 库尔勒网站做婚纱网站的步骤
  • 建设网站如何给页面命名cms网站建站流程
  • 能打开任何网站浏览器下载杭州外贸网站建设公司
  • 做网站是不是很简单哈尔滨建站模板厂家
  • 灵璧网站建设wordpress编辑器字体大小