广告
返回顶部
首页 > 资讯 > 后端开发 > Python >FeignClient如何通过配置变量调用配置文件url
  • 964
分享到

FeignClient如何通过配置变量调用配置文件url

2024-04-02 19:04:59 964人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录通过配置变量调用配置文件url调用指定的动态URL1 创建demo1服务2 创建demo2服务测试通过配置变量调用配置文件url 1.application.yml 配置文件配置

通过配置变量调用配置文件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<Map<String, Object>> queryPlatMenus();
}

这样部署开发时就可以只需要修改一处yml配置url就可以了。

调用指定的动态URL

1 创建demo1服务

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://Maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.demo</groupId>
	<artifactId>demo1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo1</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>2020.0.3</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-WEB</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<!-- eureka -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

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;

@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 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.demo</groupId>
	<artifactId>demo2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo2</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>2020.0.3</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<!-- eureka -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

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;

//@FeignClient(name = "DEMO1", url="EMPTY")
@FeignClient(name = "DEMO1", url="http://localhost:8111")
public interface DemoFeign {
    
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    String hello();
    
    @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;

@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】 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: FeignClient如何通过配置变量调用配置文件url

本文链接: https://www.lsjlt.com/news/152856.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作