网站 建设 流行 数据库,百度热搜关键词,网站建设策划表,图片制作方法需求
一个应用通过接口#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);}
成功调用的接口示例