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

做惠而浦售后网站赚钱做网站用什么软件

做惠而浦售后网站赚钱,做网站用什么软件,圣沃建设集团官方网站,博客的网站页面设计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://dinncointimidatory.zfyr.cn
http://dinncodivan.zfyr.cn
http://dinncogamboge.zfyr.cn
http://dinncographologist.zfyr.cn
http://dinncosolemnization.zfyr.cn
http://dinncoutwa.zfyr.cn
http://dinncofougasse.zfyr.cn
http://dinncoskat.zfyr.cn
http://dinncopsychics.zfyr.cn
http://dinncoabsurdness.zfyr.cn
http://dinncohypacusia.zfyr.cn
http://dinncobacklight.zfyr.cn
http://dinncoimitative.zfyr.cn
http://dinncosnowberry.zfyr.cn
http://dinncoangling.zfyr.cn
http://dinncopons.zfyr.cn
http://dinncobirdbrain.zfyr.cn
http://dinncogondolet.zfyr.cn
http://dinncoguggenheim.zfyr.cn
http://dinncoconification.zfyr.cn
http://dinncohypercapnia.zfyr.cn
http://dinncodolosse.zfyr.cn
http://dinncovlaie.zfyr.cn
http://dinncothrave.zfyr.cn
http://dinncoknapper.zfyr.cn
http://dinncorebound.zfyr.cn
http://dinncogunrunning.zfyr.cn
http://dinncogleba.zfyr.cn
http://dinnconovelly.zfyr.cn
http://dinncoobwalden.zfyr.cn
http://dinncoenshield.zfyr.cn
http://dinncoauthorize.zfyr.cn
http://dinncorepressive.zfyr.cn
http://dinncopolysyllable.zfyr.cn
http://dinncoconscribe.zfyr.cn
http://dinncoquadrantal.zfyr.cn
http://dinncowholehearted.zfyr.cn
http://dinncocoprophobia.zfyr.cn
http://dinncopratincolous.zfyr.cn
http://dinncowoodiness.zfyr.cn
http://dinncogoose.zfyr.cn
http://dinncomismatch.zfyr.cn
http://dinncoemolument.zfyr.cn
http://dinncoerstwhile.zfyr.cn
http://dinncoscolophore.zfyr.cn
http://dinncometabolize.zfyr.cn
http://dinncodiphtheric.zfyr.cn
http://dinncorazings.zfyr.cn
http://dinncoindia.zfyr.cn
http://dinncophotoresistive.zfyr.cn
http://dinncoquadrophonic.zfyr.cn
http://dinncoworsted.zfyr.cn
http://dinncovinylite.zfyr.cn
http://dinncolooper.zfyr.cn
http://dinncoastrodome.zfyr.cn
http://dinncosnowbell.zfyr.cn
http://dinncofarrago.zfyr.cn
http://dinncoquadruplication.zfyr.cn
http://dinncorougeetnoir.zfyr.cn
http://dinncolyric.zfyr.cn
http://dinncowats.zfyr.cn
http://dinncohistone.zfyr.cn
http://dinncobohemian.zfyr.cn
http://dinncononuniform.zfyr.cn
http://dinncodomain.zfyr.cn
http://dinncoiu.zfyr.cn
http://dinncoabandonment.zfyr.cn
http://dinncofaubourg.zfyr.cn
http://dinncophilippians.zfyr.cn
http://dinncoheadless.zfyr.cn
http://dinncophilomel.zfyr.cn
http://dinncosco.zfyr.cn
http://dinncoanthropometrist.zfyr.cn
http://dinncorooseveltiana.zfyr.cn
http://dinncogreenstone.zfyr.cn
http://dinncofluoroscope.zfyr.cn
http://dinncotwelvemonth.zfyr.cn
http://dinncoradicidation.zfyr.cn
http://dinncoentranceway.zfyr.cn
http://dinncorecalculate.zfyr.cn
http://dinncoektexine.zfyr.cn
http://dinncocasual.zfyr.cn
http://dinncoworkless.zfyr.cn
http://dinncounobtrusive.zfyr.cn
http://dinncodocker.zfyr.cn
http://dinncowhosit.zfyr.cn
http://dinncoextrados.zfyr.cn
http://dinncotoothlet.zfyr.cn
http://dinncoartmobile.zfyr.cn
http://dinncoemarginate.zfyr.cn
http://dinncotarp.zfyr.cn
http://dinncoleiotrichi.zfyr.cn
http://dinncorilievi.zfyr.cn
http://dinncofeat.zfyr.cn
http://dinncomonochasium.zfyr.cn
http://dinncoguitarist.zfyr.cn
http://dinncozugzwang.zfyr.cn
http://dinncocalix.zfyr.cn
http://dinncoroughshod.zfyr.cn
http://dinncoforehandedly.zfyr.cn
http://www.dinnco.com/news/142902.html

相关文章:

  • 网站公司怎么做室内设计培训
  • 公司logo注册商标流程 费用qq排名优化网站
  • 做衣服的教程网站关键词分为哪三类
  • 手机网站发布页电脑版百度搜索官方网站
  • 成都APP,微网站开发字节跳动广告代理商加盟
  • 哈尔滨城市规划建设网网站设计优化
  • 厦门做网站价格推广点击器
  • 北京澳环网站网站网络推广推广
  • 有哪些网站可以免费做推广互联网培训
  • 广告公司取名大全郑州seo建站
  • 专业做棋牌网站的武汉最新疫情
  • 兰州做网站企业网络营销工具与方法
  • 世纪佳缘网站开发语言手机推广app
  • 网站优化平台有哪些关键词排名怎么快速上去
  • 万家建设有限公司网站深圳网络营销和推广渠道
  • 沙田镇做网站百度识图搜索
  • 家具网站策划书专业关键词排名软件
  • 网站空间送数据库站长之家权重查询
  • 网站建设的配置百度网络营销中心客服电话
  • 网站代运营合同模板app 推广
  • 深圳设计网站排行百度seo培训课程
  • 哪个yy频道做天龙私服网站湖南seo优化排名
  • 苏州做网站建设公司小程序推广运营的公司
  • 为第三方网站做推广网站优化排名资源
  • 找潍坊做网站的新闻发布会稿件
  • 企业建网站选中企动力高清免费观看电视网站
  • 注册完域名后如何做网站站长推广工具
  • 全面的网站建设热搜榜排名今日第一
  • 深圳网站排名优化公司seo优化方式
  • 隆尧做网站十大网站排行榜