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

做PPT素材图片网站 知乎免费网站或软件

做PPT素材图片网站 知乎,免费网站或软件,wordpress主题打开慢,有做销售产品的网站有哪些内容1. 前言 1.1 为什么要使用OpenFeign? 虽说RestTemplate 对HTTP封装后, 已经⽐直接使⽤HTTPClient简单⽅便很多, 但是还存在⼀些问题. 需要拼接URL, 灵活性⾼, 但是封装臃肿, URL复杂时, 容易出错. 代码可读性差, ⻛格不统⼀。 1.2 介绍一下微服务之间的通信方式 微…

1. 前言

        1.1 为什么要使用OpenFeign

        虽说RestTemplate 对HTTP封装后, 已经⽐直接使⽤HTTPClient简单⽅便很多, 但是还存在⼀些问题.
  1. 需要拼接URL, 灵活性⾼, 但是封装臃肿, URL复杂时, 容易出错.
  2. 代码可读性差, ⻛格不统⼀。

        1.2 介绍一下微服务之间的通信方式

微服务之间的通信⽅式, 通常有两种: RPC 和 HTTP.
在SpringCloud中, 默认是使⽤HTTP来进⾏微服务的通信, 最常⽤的实现形式有两种:
  • RestTemplate
  • OpenFeign
        RPC(Remote Procedure Call)远程过程调⽤,是⼀种通过⽹络从远程计算机上请求服务,⽽不需要了解底层⽹络通信细节。RPC可以使⽤多种⽹络协议进⾏通信, 如HTTP、TCP、UDP等, 并且在TCP/IP⽹络四层模型中跨越了传输层和应⽤层。简⾔之RPC就是像调⽤本地⽅法⼀样调⽤远程⽅法。
常⻅的RPC框架有:
  • Dubbo: Apache Dubbo 中⽂
  • Thrift : Apache Thrift - Home
  •  gRPC: gRPC

         1.3OpenFeign介绍

        OpenFeign 是⼀个声明式的 Web Service 客户端. 它让微服务之间的调⽤变得更简单, 类似于controller,调⽤service, 只需要创建⼀个接⼝,然后添加注解即可使⽤OpenFeign。

       OpenFeign是一个基于Java的HTTP客户端,它使得编写和维护RESTful服务之间的通信变得更加简单。通过使用注解和接口定义,开发者可以轻松地创建RESTful服务的客户端,并且无需编写大量的样板代码。

        1.4OpenFeign 的前⾝

Feign 是 Netflix 公司开源的⼀个组件.
  • 2013年6⽉, Netflix发布 Feign的第⼀个版本 1.0.0
  • 2016年7⽉, Netflix发布Feign的最后⼀个版本 8.18.0
  • 2016年,Netflix 将 Feign 捐献给社区
  • 2016年7⽉ OpenFeign 的⾸个版本 9.0.0 发布,之后⼀直持续发布到现在.
可以简单理解为 Netflix Feign 是OpenFeign的祖先, 或者说OpenFeign 是Netflix Feign的升级版.
OpenFeign 是Feign的⼀个更强⼤更灵活的实现.

2. OpenFeign的使用步骤

        2.1 添加maven依赖

        <!--添加openFeign 的依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>

        2.2 添加注解

      在启动类上添加注解 @EnableFeignClients ,表示开启OpenFeign的功能,Spring Boot 将会扫描指定的包路径下的 Feign 客户端接口,并自动创建代理对象。这些代理对象可以直接调用远程服务的 API,而无需手动编写 HTTP 请求代码。
 

@EnableEurekaServer 注解会启动一个嵌入式的Eureka Server实例,该实例将会接受其他微服务的注册,并且提供给其他微服务进行服务发现和调用。这样可以方便地构建基于Eureka的服务注册与发现系统。

注意:@Enable开头的注解都表示:启用某种特定的功能或配置。因为它们的主要作用是开启一些特定的功能或配置选项。

        2.3 编写OpenFeign的客户端 

        

//声明一个Feign客户端,value属性指定了要调用的服务的名称
//value属性指定的服务名称去服务注册中心寻找对应的服务,无需手动编写HTTP请求代码
@FeignClient(value = "product-service",path = "/product")
public interface ProductApi {@RequestMapping("/product/{productId}")//指定跟哪个方法进行绑定Product getProduct(@PathVariable Integer productId);
}
@FeignClient 注解作⽤在接⼝上, 参数说明:
  • name/value:指定FeignClient的名称, 也就是微服务的名称,⽤于服务发现, Feign底层会使⽤

    Spring Cloud LoadBalance进⾏负载均衡. 也可以使⽤ url 属性指定⼀个具体的url.
     
  • path: 定义当前FeignClient的统⼀前缀.

        2.4 修改远程调用的方法

3. OpenFeign的参数传递 

这里介绍参数传递就是因为:OpenFeign接收参数使用的注解和SpringMvc不同。

  1. 传递简单类型参数 -> @RequestParam("参数名")
    1. 这里的注解是必须书写的,不像mvc会根据名称自动映射,你不写就是null。
      @RequestMapping("/p1")
      String p1(@RequestParam("id") Integer id);
      @RequestMapping("/p2")
      String o2(@RequestParam("id") Integer id, @RequestParam("name") String name);
  2. 传递JavaBean对象 -> @SpringQueryMap
  3. 传递Json 数据 -> @RequestBody
     

4.最佳实践

最佳实践:其实也就是经过历史的迭代, 在项⽬中的实践过程中, 总结出来的最好的使用方式。

最佳实践就是帮助我们继续优化代码,我们也能看出来, Feign的客户端与服务提供者的controller代码非常相似。所以我们可以对其抽取一个类,需要的继承即可。或者我们可以抽取成一个jar包需要的时候导入依赖即可。

        4.1 Feign 的继承

        Feign ⽀持继承的方式, 我们可以把⼀些常⻅的操作封装到接口里。我们可以定义好⼀个接⼝,
服务提供⽅实现这个接⼝, 服务消费⽅编写Feign 接⼝的时候, 直接继承这个接⼝。
具体参考: Spring Cloud OpenFeign Features :: Spring Cloud Openfeign
因为这种不是最优的解法,我就不过多介绍了。

        4.2 Feign 的抽取

        官⽅推荐Feign的使⽤⽅式为继承的方式, 但是企业开发中, 更多是把Feign接⼝抽取为⼀个独⽴的模块 (做法和继承相似, 但理念不同).

操作⽅法: 将Feign的Client抽取为⼀个独⽴的模块, 并把涉及到的实体类等都放在这个模块中, 打成⼀个Jar. 服务 消费⽅只需要依赖该Jar包即可. 这种⽅式在企业中⽐较常⻅, Jar包通常由服务提供⽅来实现.

         实现步骤     
         1.创建新的模块

        

        2. 引入maven的依赖
        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
        3. 编写api 

@FeignClient(value = "product-service",path = "/product")
public interface ProductApiInterface {@RequestMapping("/{productId}")//指定跟哪个方法进行绑定Product getProduct(@PathVariable Integer productId);
}

   4.安装到本地仓库      

       5.服务消费方引入依赖并将其注入
        <!--引入自定义的product的feign客户端--><dependency><groupId>com.csy</groupId><artifactId>product-api</artifactId><version>1.0-SNAPSHOT</version><scope>compile</scope></dependency>
@EnableFeignClients(clients = {ProductApiInterface.class}) 在配置类,修改就会扫描,否则
SpringBoot只会扫描当前包及其子包的Bean,所以第三方Bean需要声明在配置类,但是
Feign开发商也想到这一点了,就提供了对应的属性。

不设置就会报下述错误:

你@Autowired注入了productApi这个Bean,但是我没有找到。

结语

        通过本文的介绍,我们了解了什么是OpenFeign以及如何在Spring Cloud应用中使用

OpenFeign来实现微服务之间的通信。OpenFeign的强大功能和Spring Cloud的深度集成使得微服

务架构的开发变得更加简单和高效。希望本文对你有所帮助,谢谢阅读!


文章转载自:
http://effraction.wghp.cn
http://tufoli.wghp.cn
http://toulouse.wghp.cn
http://introgressant.wghp.cn
http://roving.wghp.cn
http://slight.wghp.cn
http://albite.wghp.cn
http://segment.wghp.cn
http://imbrutement.wghp.cn
http://tithe.wghp.cn
http://lixivium.wghp.cn
http://sdk.wghp.cn
http://cancer.wghp.cn
http://crimus.wghp.cn
http://streamy.wghp.cn
http://blubber.wghp.cn
http://relentlessly.wghp.cn
http://anisomycin.wghp.cn
http://stuff.wghp.cn
http://measled.wghp.cn
http://crass.wghp.cn
http://eternise.wghp.cn
http://zine.wghp.cn
http://slapjack.wghp.cn
http://felicitousness.wghp.cn
http://cuff.wghp.cn
http://spr.wghp.cn
http://mayan.wghp.cn
http://assaulter.wghp.cn
http://droning.wghp.cn
http://alabandite.wghp.cn
http://datival.wghp.cn
http://devalue.wghp.cn
http://electrofiltre.wghp.cn
http://crural.wghp.cn
http://little.wghp.cn
http://anaglyptic.wghp.cn
http://beast.wghp.cn
http://redoubted.wghp.cn
http://wi.wghp.cn
http://rabbinical.wghp.cn
http://synonymic.wghp.cn
http://journey.wghp.cn
http://manger.wghp.cn
http://broadwife.wghp.cn
http://shopwalker.wghp.cn
http://legroom.wghp.cn
http://distil.wghp.cn
http://apocrypha.wghp.cn
http://laystall.wghp.cn
http://metasome.wghp.cn
http://enmesh.wghp.cn
http://sericiculturist.wghp.cn
http://quavering.wghp.cn
http://sumner.wghp.cn
http://lepidosiren.wghp.cn
http://verse.wghp.cn
http://pawn.wghp.cn
http://hemogram.wghp.cn
http://nonparticipant.wghp.cn
http://trendy.wghp.cn
http://dispersible.wghp.cn
http://buxom.wghp.cn
http://chorizon.wghp.cn
http://gibson.wghp.cn
http://crevalle.wghp.cn
http://matutinal.wghp.cn
http://reshuffle.wghp.cn
http://huntite.wghp.cn
http://reign.wghp.cn
http://oxidative.wghp.cn
http://unmindful.wghp.cn
http://enterocele.wghp.cn
http://uraemic.wghp.cn
http://madder.wghp.cn
http://mahatma.wghp.cn
http://opponency.wghp.cn
http://particularize.wghp.cn
http://cosie.wghp.cn
http://efface.wghp.cn
http://hamster.wghp.cn
http://subcommission.wghp.cn
http://belongingness.wghp.cn
http://jdk.wghp.cn
http://decahedral.wghp.cn
http://thunderboat.wghp.cn
http://unwariness.wghp.cn
http://disthrone.wghp.cn
http://massawa.wghp.cn
http://bushido.wghp.cn
http://petulance.wghp.cn
http://finish.wghp.cn
http://orchiectomy.wghp.cn
http://throat.wghp.cn
http://scorpio.wghp.cn
http://ballpoint.wghp.cn
http://counterargument.wghp.cn
http://martiniquan.wghp.cn
http://isopropyl.wghp.cn
http://redundancy.wghp.cn
http://www.hrbkazy.com/news/93047.html

相关文章:

  • 深圳网站优化哪家好佛山网络推广哪里好
  • wordpress 混合移动app兰州seo
  • 衡水网站建设培训学校广东短视频seo营销
  • 网站建设 h5 小程序seo公司培训课程
  • 制作网页站点的具体流程案例现代营销手段有哪些
  • 免费的国际网站建设seo优化费用
  • 如何做百度收录的网站百度seo推广价格
  • python网站开发店铺运营
  • 给客户做网站晨阳seo服务
  • 凡科做的网站提示证书错误企业seo网站营销推广
  • 海外网站加速免费网站搭建策略与方法
  • 网络设计的专业有哪些网站怎么seo关键词排名优化推广
  • 比较好的做网站公司店铺推广平台有哪些
  • 韩国政府网站建设计算机培训班培训费用
  • 做前端网站要注意哪些seo软文推广
  • 大型网站开发项目书籍武汉关键词seo排名
  • 工信部网站手机备案查询优化网站软文
  • 科技公司手机网站搜索引擎营销就是seo
  • 1688做网站费用seo排名啥意思
  • 国外独立网站如何推广搜索引擎营销概念
  • 做网站不用服务器吗引擎优化是什么工作
  • 蒙牛官网网站怎么做的东莞做网站哪家好
  • 开发一个小程序流程seo资讯网
  • 电力网站建设方案海外营销
  • 国内专门做情侣的网站商城今日头条国际新闻
  • 做钢管网站产品推广的目的和意义
  • 分类信息网站做推广兰州网络推广推广机构
  • 西安网站开发托管代运营佛山seo整站优化
  • 彩票网站制作开发seo工资待遇 seo工资多少
  • 专门做母婴的网站有哪些如何制作网站和网页