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

网站 建设 流行 数据库百度热搜关键词

网站 建设 流行 数据库,百度热搜关键词,网站建设策划表,图片制作方法需求 一个应用通过接口#xff0c;调用另一个应用的接口。使用OpenFeign来实现接口调用。 说明 通过OpenFeign#xff08;本文接下来简称Feign#xff09;调用远程接口#xff0c;需要Eureka注册中心的支持。 OpenFeign调用接口的逻辑如下#xff1a; 提供接口的应用…需求 一个应用通过接口调用另一个应用的接口。使用OpenFeign来实现接口调用。 说明 通过OpenFeign本文接下来简称Feign调用远程接口需要Eureka注册中心的支持。 OpenFeign调用接口的逻辑如下 提供接口的应用A将自身注册到Eureka服务器注册中心应用A需要给自己起一个应用名称调用接口的应用B从Eureka读取所有已注册服务的信息B应用的Feign客户端通过服务的应用名称从已注册服务的信息中找到应用A对应的IP地址和端口号从而调用A的接口。 本文主要内容 本文主要讲述如何配置一个注册中心EurekaFeign的配置以及使用Feign来调用接口。 主要包含三个部分 配置Eureka注册中心单体非集群配置提供接口的应用注册到Eureka提供被调用的接口配置调用接口的应用从Eureka获取到被调用方地址调用接口。 Eureka服务器 1. 依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-server/artifactId /dependency2. 配置application.properties 此配置为单体服务器配置非集群配置。 server.port8761# 主机名不配置的时候将根据操作系统的主机名获取。 eureka.instance.hostnamelocalhost# 不将自身注册到注册中心。是否将自己注册到注册中心默认为true。单个Eureka服务器不需要注册自身配置为false如果是Eureka集群则需要注册自身即配置为true。 eureka.client.registerWithEurekafalse # 是否从注册中心获取服务注册信息默认为true。 eureka.client.fetchRegistryfalse # 注册中心对外暴露的注册地址 eureka.client.serviceUrl.defaultZonehttp://${eureka.instance.hostname}:${server.port}/eureka/ 3. 开启Eureka服务器 在 Application 启动类上添加注解 EnableEurekaServer. 示例代码 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;EnableEurekaServer SpringBootApplication public class EurekaServerDemoApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerDemoApplication.class, args);}}FeignServer 提供接口的应用可以通过Feign来调用接口。 1. 依赖 Eureka Discovery Client dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId /dependency2. 配置application.properties server.port8081# 应用名称 spring.application.namefeign-server # 使用 ip地址:端口号 注册 eureka.instance.prefer-ip-addresstrue eureka.instance.instance-id${spring.cloud.client.ip-address}:${server.port} # 注册中心地址 eureka.client.service-url.defaultZonehttp://localhost:8761/eureka/3. 提供接口 package com.example.feign.server.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;RestController RequestMapping(feign_server_path) public class FeignServerController {GetMapping(hello)public String hello() {return hello feign server!;}GetMapping(data)public String getData() {return 来自FeignServer的数据;}} Feign客户端 通过Feign调用FeignServer应用的接口。 1. 依赖 需要引入两个依赖 Eureka Discovery ClientOpenFeign dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId /dependency dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId /dependency注意需要通过 dependencyManagement 和 properties管理 spring cloud 版本。如果项目中已经添加则无需再额外修改。 dependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies /dependencyManagementpropertiesspring-cloud.version2021.0.8/spring-cloud.version /properties2. 配置application.properties server.port8082# 不将自身注册到Eureka注册中心。本配置为是否将自己注册到注册中心默认为true。 eureka.client.registerWithEurekafalse # 注册中心地址 eureka.client.service-url.defaultZonehttp://localhost:8761/eureka/3. 开启Feign客户端 在 Application 启动类上添加注解 EnableFeignClients. 示例代码 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients;EnableFeignClients SpringBootApplication public class FeignClientDemoApplication {public static void main(String[] args) {SpringApplication.run(FeignClientDemoApplication.class, args);}}4. 定义接口与FeignServer对应 注解 FeignClient表示Feign接口。 valueFeign所调用的应用的应用名称。 pathFeign所调用的应用的对应Controller的接口路径即 Controller 上 RequestMapping 中的接口路径。 import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping;FeignClient(value feign-server, path feign_server_path) public interface FeignInvocationService {GetMapping(data)String getFeignServerData();}5. 调用Feign接口 像调用本地方法一样调用Feign接口。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import com.example.feign.client.feign.FeignInvocationService;RestController RequestMapping(feign_client) public class FeignClientController {GetMapping(hello)public String hello() {return hello feign client!;}Autowiredprivate FeignInvocationService feignInvocationService;GetMapping(feign_server_data)public String getFeignServerData() {return 通过FeignClient调用 feignInvocationService.getFeignServerData();}}调用示例 Eureka FeignServer的接口直接调用 FeignClient通过Feign调用FeignServer的接口 Get方法报错 两种报错 Body parameter 0 was null Feign客户端调用Get方法时接口包含一个参数报错 java.lang.IllegalArgumentException: Body parameter 0 was null Method has too many Body parameters Feign客户端调用Get方法时接口包含多个参数报错 Method has too many Body parameters 报错接口的原始代码 Body parameter 0 was null Feign服务器端接口 GetMapping(result)public String getData(String account) {return 从FeignServer查询的数据入参为 account;}Feign客户端 GetMapping(result)String getData(String account);Method has too many Body parameters Feign服务器端接口 GetMapping(two_params)public String getDataByTwoParam(String account, String name) {return 从FeignServer查询的数据account account name name;}Feign客户端 GetMapping(two_params)public String getDataByTwoParam(String account, String name);解决方法RequestParam Feign接口参数添加RequestParam注解。 Feign客户端修改后的代码如下 import org.springframework.web.bind.annotation.RequestParam;GetMapping(result)String getData(RequestParam(account) String account);GetMapping(two_params)public String getDataByTwoParam(RequestParam(account) String account, RequestParam(name) String name);完整的Feign客户端代码示例 package com.example.feign.client.feign;import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam;FeignClient(value feign-server, path feign_server_path) public interface FeignInvocationService {GetMapping(data)String getFeignServerData();GetMapping(result)String getData(RequestParam(account) String account);GetMapping(two_params)public String getDataByTwoParam(RequestParam(account) String account, RequestParam(name) String name);} 成功调用的接口示例
http://www.zqtcl.cn/news/907680/

相关文章:

  • 资源网站哪个好淄博网站设计
  • 网站建设林晓东网站数据库一般多大
  • 织梦网站后台默认登陆路径网站建设简介淄博
  • 重庆住房建设部网站东莞网站制作多少钱
  • 做胎儿羊水鉴定网站网站管理主要包括哪些内容
  • 公司网站建设应注意网店推广有哪些方法
  • 新网$网站优化企业资源管理软件
  • 甘肃营销型网站制作网页设计流程的图片
  • 厦门成交型网站建设公司今科云平台网站建设
  • 网站推广效果怎样学电商赚钱
  • 企业网站的一般要素包括哪些公司网站建设是什么费用
  • 网站收录说明长沙知名的营销公司
  • 网站开发 业务流程图天津网站排名方案
  • 风雨同舟网站建设小说网站如何做书源
  • h5手机网站建设哪家好广州有什么好玩的地方和风景好
  • 北京哪个网站建设最好怀化网站建设公司
  • 做类似猪八戒网的网站注册一个商标多少钱
  • 怎么提高网站访问速度wordpress怎么备份按在
  • 淘宝网站是谁做的好处wordpress商业授权
  • 淘宝客网站怎么批量采集淘宝商品方维采集淘宝数据思路珠宝类网站建设
  • 重庆网站关键字优化雅布设计中国分公司在哪里
  • 山西做网站费用温州做网站制作
  • 购买域名后 可以做网站么苏州市建设厅网站
  • 网站域名如何查询win7优化配置的方法
  • 免费建网站的服务器佛山城市建设工程有限公司
  • 安溪人做的网站wordpress 单页面 主题
  • 品牌型网站设计创意 国外 网站
  • o2o网站建设包括哪些平面设计作品欣赏
  • 万齐网站建设成都旅游攻略自由行攻略地图
  • 新网做网站流程app下载汅api未满入内