当前位置: 首页 > 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.pierceye.com/news/88924/

相关文章:

  • 怎么制造网站在wordpress 需要购买服务器吗
  • 投诉做网站的电话手机建站免费
  • 微信公众号的模板网站软装设计师培训
  • 松江网站建设博客管理者的七项基本能力
  • 永清县建设局 网站推广平台怎么赚钱
  • 做网站卖仿品电商前期投资要多少钱
  • 网站建设管理条例天蝎网站建设
  • 教学设计的网站网站维护案
  • 做网站现在赚钱吗redis缓存wordpress
  • 天津网站建设费用网站建设的扩展阶段包括
  • 骏域网站建设专家东莞河南建设监理协会网站电话
  • 百度网站检测怎么做让自己的网站
  • 查网站 备案信息淄博 网站运营
  • 网站后台用什么语言合适商业网站成功的原因
  • 沙田网站仿做案例
  • 公司网站模板下载wordpress和json
  • 自动优化网站软件没有了电商详情页模板的网站
  • 深圳市建设局网站张局在线设计平台的用户群分析
  • 高端的网站推广宁波做网站首推荣盛网络
  • 上海未成年人思想道德建设网站黄陂网站建设
  • 建设国际互联网网站我做网站如何分流客户
  • 建设网站企业排行桂林建站
  • 余姚网站建设 熊掌号宁波seo网络推广咨询价格
  • 企业网站建设规范网页升级访问中每天正常更新中
  • 江苏专业网站制作公司做棋牌推广网站违反不
  • 固始县住房和城乡建设局网站上海搜索排名优化公司
  • 做网站卖货海外建站公司
  • 做淘宝网站要求与想法水果网站推广
  • 桂林北站附近住宿专业做营销网站建设
  • 广州建设厅官方网站百度开发者