本篇文章小编给大家分享一下FeignClient通过配置变量调用配置文件url代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1.application.yml 配置文件配置参数
feign: sys: http://127.0.0.1:8777
2.ISysFeignClient.java 使用@FeignClient时配置
@FeignClient(value = "sys",url = "${feign.sys}") public interface ISysFeignClient { @GetMapping("/sys/queryPlatMenus") List
这样部署开发时就可以只需要修改一处yml配置url就可以了。
调用指定的动态URL
1 创建demo1服务
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.3 com.demo demo1 0.0.1-SNAPSHOT demo1 Demo project for Spring Boot 1.8 2020.0.3 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
application-dev1.yml
server: port: 8111 spring: application: name: ${APPLICATION_NAME:demo1} eureka: client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:18880/eureka instance: instance-id: ${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true
application-dev2.yml
server: port: 8112 spring: application: name: ${APPLICATION_NAME:demo1} eureka: client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:18880/eureka instance: instance-id: ${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true
IndexController.java
package com.demo.demo1.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @description: * @author: Evan * @time: 2021/8/2 17:17 */ @RestController public class IndexController { @Value("${server.port}") private String port; @GetMapping("/hello") public String hello(){ System.out.println("进入" + port + "服务器"); return "返回的端口为:" + port; } }
2 创建demo2服务
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.3 com.demo demo2 0.0.1-SNAPSHOT demo2 Demo project for Spring Boot 1.8 2020.0.3 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
application.yml
server: port: 8113 spring: application: name: ${APPLICATION_NAME:demo2} eureka: client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:18880/eureka instance: instance-id: ${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true
DemoFeign.java
package com.demo.demo2.Feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import java.net.URI; /** * @description: * @author: Evan * @time: 2021/8/2 17:34 */ //@FeignClient(name = "DEMO1", url="EMPTY") @FeignClient(name = "DEMO1", url="http://localhost:8111") public interface DemoFeign { /** * 调用demo1的接口 * @return */ @RequestMapping(value = "/hello", method = RequestMethod.GET) String hello(); /** * 调用demo1的接口 * @return */ @RequestMapping(value = "/hello", method = RequestMethod.GET) String hello1(URI uri); }
IndexController.java
package com.demo.demo2.controller; import com.demo.demo2.Feign.DemoFeign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.net.URI; /** * @description: * @author: Evan * @time: 2021/8/2 17:33 */ @RestController public class IndexController { @Autowired private DemoFeign demoFeign; @GetMapping("/testFeign") public String testFeign(){ System.out.println("在demo2服务中,调用demo1的服务"); String resultStr = demoFeign.hello(); System.out.println("在demo2服务中,调用demo1的服务,返回的结果:" + resultStr); return "在demo2服务中,调用demo1的服务,返回的结果:" + resultStr; } @GetMapping("/testFeign2") public String testFeign2(@RequestParam String server) throws Exception{ String url = ""; if("1".equals(server)){ url = "http://localhost:8111"; }else if("2".equals(server)){ url = "http://localhost:8112"; } System.out.println("在demo2服务中,调用demo1的服务" + url); String resultStr = demoFeign.hello1(new URI(url)); System.out.println("在demo2服务中,调用demo1的服务,返回的结果:" + resultStr); return "在demo2服务中,调用demo1的服务,返回的结果:" + resultStr; } }
测试
指定服务器ip
http://localhost:8113/testFeign2?server=1
返回【在demo2服务中,调用demo1的服务,返回的结果:返回的端口为:8111】
http://localhost:8113/testFeign2?server=2
返回【在demo2服务中,调用demo1的服务,返回的结果:返回的端口为:8112】
使用默认的地址
http://localhost:8113/testFeign
返回【在demo2服务中,调用demo1的服务,返回的结果:返回的端口为:8111】
忍者必须死34399账号登录版 最新版v1.0.138v2.0.72
下载勇者秘境oppo版 安卓版v1.0.5
下载忍者必须死3一加版 最新版v1.0.138v2.0.72
下载绝世仙王官方正版 最新安卓版v1.0.49
下载Goat Simulator 3手机版 安卓版v1.0.8.2
Goat Simulator 3手机版是一个非常有趣的模拟游
Goat Simulator 3国际服 安卓版v1.0.8.2
Goat Simulator 3国际版是一个非常有趣的山羊模
烟花燃放模拟器中文版 2025最新版v1.0
烟花燃放模拟器是款仿真的烟花绽放模拟器类型单机小游戏,全方位
我的世界动漫世界 手机版v友y整合
我的世界动漫世界模组整合包是一款加入了动漫元素的素材整合包,
我的世界贝爷生存整合包 最新版v隔壁老王
我的世界MITE贝爷生存整合包是一款根据原版MC制作的魔改整