最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

FeignClient如何通過配置變量調用配置文件url

 更新時間:2022年06月28日 11:29:37   作者:西楚三少  
這篇文章主要介紹了FeignClient如何通過配置變量調用配置文件url,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

通過配置變量調用配置文件url

1.application.yml 配置文件配置參數(shù)

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();
}

這樣部署開發(fā)時就可以只需要修改一處yml配置url就可以了。

調用指定的動態(tài)URL

1 創(chuàng)建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;
/**
?* @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 創(chuàng)建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;
/**
 * @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】 

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

阿城市| 贡觉县| 图们市| 绥中县| 和政县| 鸡泽县| 万年县| 诏安县| 株洲县| 永新县| 侯马市| 新绛县| 文登市| 万年县| 乐业县| 邳州市| 西城区| 姚安县| 天镇县| 马尔康县| 仁寿县| 藁城市| 汉川市| 肇庆市| 陈巴尔虎旗| 郎溪县| 秦安县| 西城区| 邵东县| 深泽县| 梧州市| 霞浦县| 黑河市| 门头沟区| 航空| 诏安县| 富锦市| 贵溪市| 黑山县| 盘山县| 庆元县|