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

网站建设找 三尾狐wordpress kleo

网站建设找 三尾狐,wordpress kleo,公众号免费素材网站,营销网站建设服务平台需求 一个应用通过接口#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/88924/

相关文章:

  • 电子商务网站建设题6做网站软件ps
  • c 网站开发框架网站图片装修的热切图怎么做
  • 伍佰亿网站系统上海自助建站企业
  • 银川市住房和城乡建设局网站公告城阳网站改版
  • 网站开发工具中三剑客包括孝感英文网站建设
  • 怎么做建设网站首页宁波全网营销型网站建设
  • wordpress 站库分离哈尔滨 做网站公司哪家好
  • 英语培训网站建设需求分析报告能免费建手机网站吗
  • ps如何做切片网站按钮无代码开发平台有哪些
  • 网站静态页面生成没有网站怎么快速做cps
  • 罗湖小学网站建设做视频网站视频放在哪里
  • 腾讯云网站建设教学视频wordpress 别名
  • 某企业网站建设方案2000字门户网站技术方案
  • 怎样健网站需要找做网站的
  • 百度搜索网站图片上海中学校服
  • 做360网站中保存的图片存在哪里广告设计公司制作
  • 廉政网站 建设需求wordpress 插件 教程视频教程
  • django成品网站源码如皋做网站
  • 自助建站软件下载宿迁做网站建设的公司
  • 网站建设服务费一年多少钱东莞定制网页设计
  • 潍坊市城乡建设局网站wordpress 评论回复插件
  • 浙江网站建设价格费用网站建设案例查询
  • 做互联网公司网站谈单模拟视频教学电子商务网站建设投资预算
  • 安卓网站开发前景表白视频制作软件app
  • 打开山东城市建设职业学院网站个人适合建什么网站
  • 海外域名提示风险网站吗赣州省住房和城乡建设厅网站
  • 中山网站建设文化咨询wordpress tag 输出
  • o2o平台排名标题优化怎样选关键词
  • 模版网站后期可以更换图片吗搜索词排行榜
  • 网站建设规划模板太原建网站