欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

FeignClient如何通過配置變量調(diào)用配置文件url

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

通過配置變量調(diào)用配置文件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就可以了。

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

1 創(chuàng)建demo1服務(wù)

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("進(jìn)入" + port + "服務(wù)器");
? ? ? ? return "返回的端口為:" + port;
? ? }
}

2 創(chuàng)建demo2服務(wù)

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 {
    /**
     * 調(diào)用demo1的接口
     * @return
     */
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    String hello();
    /**
     * 調(diào)用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服務(wù)中,調(diào)用demo1的服務(wù)");
        String resultStr = demoFeign.hello();
        System.out.println("在demo2服務(wù)中,調(diào)用demo1的服務(wù),返回的結(jié)果:" + resultStr);
        return "在demo2服務(wù)中,調(diào)用demo1的服務(wù),返回的結(jié)果:" + 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服務(wù)中,調(diào)用demo1的服務(wù)" + url);
        String resultStr = demoFeign.hello1(new URI(url));
        System.out.println("在demo2服務(wù)中,調(diào)用demo1的服務(wù),返回的結(jié)果:" + resultStr);
        return "在demo2服務(wù)中,調(diào)用demo1的服務(wù),返回的結(jié)果:" + resultStr;
    }
}

測試

指定服務(wù)器ip

  • http://localhost:8113/testFeign2?server=1
  • 返回【在demo2服務(wù)中,調(diào)用demo1的服務(wù),返回的結(jié)果:返回的端口為:8111】
  • http://localhost:8113/testFeign2?server=2
  • 返回【在demo2服務(wù)中,調(diào)用demo1的服務(wù),返回的結(jié)果:返回的端口為:8112】

使用默認(rèn)的地址

  • http://localhost:8113/testFeign
  • 返回【在demo2服務(wù)中,調(diào)用demo1的服務(wù),返回的結(jié)果:返回的端口為:8111】 

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

相關(guān)文章

  • 圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實(shí)踐與指導(dǎo)詳解

    圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實(shí)踐與指導(dǎo)詳解

    這篇文章主要介紹了圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實(shí)踐與指導(dǎo)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • java版簡單的猜數(shù)字游戲?qū)嵗a

    java版簡單的猜數(shù)字游戲?qū)嵗a

    猜數(shù)字游戲是一款經(jīng)典的游戲,該游戲說簡單也很簡單,說不簡單確實(shí)也很難,那么下面這篇文章主要給大家介紹了java版簡單的猜數(shù)字游戲的相關(guān)資料,文中給出了詳細(xì)的實(shí)現(xiàn)分析和示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。
    2017-05-05
  • 聊聊如何在springboot中添加模版

    聊聊如何在springboot中添加模版

    本文,我們談?wù)勅绾卧?nbsp;spring boot 中添加模版,因?yàn)橛袝r候我們也是需要后端渲染的嘛,比如公司官網(wǎng),文中有詳細(xì)的代碼示例供我們參考,需要的朋友可以參考下
    2023-08-08
  • SpringBoot AOP導(dǎo)致service注入后是null的問題

    SpringBoot AOP導(dǎo)致service注入后是null的問題

    本文主要講述了如何利用SpringAOP實(shí)現(xiàn)用戶操作日志的記錄,首先,引入SpringBoot的AOP依賴,然后,選擇基于注解的形式來實(shí)現(xiàn)日志操作,以避免污染原有代碼和邏輯,在理解了SpringBootAOP的一些注解后,需要記錄用戶的正常請求以及異常請求的信息
    2024-10-10
  • Java線程同步問題--哲學(xué)家就餐

    Java線程同步問題--哲學(xué)家就餐

    這篇文章主要介紹了Java線程同步問題,線程的同步是保證多線程安全訪問競爭資源的一種手段。線程的同步是Java多線程編程的難點(diǎn),下面文章舉例的方式講解Java線程同步,具有一定的參考價值,需要的朋友可以參考下
    2022-02-02
  • TKmybatis的框架介紹和原理解析

    TKmybatis的框架介紹和原理解析

    tkmybatis是在mybatis框架的基礎(chǔ)上提供了很多工具,讓開發(fā)更加高效,下面來看看這個框架的基本使用,后面會對相關(guān)源碼進(jìn)行分析,感興趣的同學(xué)可以看一下,挺不錯的一個工具
    2020-12-12
  • Java中equals和==的區(qū)別詳解

    Java中equals和==的區(qū)別詳解

    這篇文章主要介紹了詳解 Java 中 equals 和 == 的區(qū)別的相關(guān)資料,equals 和 == 都是用來檢測兩個字符串是否相等,返回值也都是布爾型,但是兩者在內(nèi)部比較的處理中卻不盡相同需要的朋友可以參考下
    2021-09-09
  • Java中transient關(guān)鍵字的詳細(xì)總結(jié)

    Java中transient關(guān)鍵字的詳細(xì)總結(jié)

    本文要介紹的是Java中的transient關(guān)鍵字,transient是短暫的意思。對于transient 修飾的成員變量,在類的實(shí)例對象的序列化處理過程中會被忽略,感興趣的朋友可以參考閱讀
    2023-04-04
  • 詳解Spring依賴注入:@Autowired,@Resource和@Inject區(qū)別與實(shí)現(xiàn)原理

    詳解Spring依賴注入:@Autowired,@Resource和@Inject區(qū)別與實(shí)現(xiàn)原理

    這篇文章主要介紹了詳解Spring依賴注入:@Autowired,@Resource和@Inject區(qū)別與實(shí)現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • RocketMQ集群消費(fèi)與廣播消費(fèi)模式

    RocketMQ集群消費(fèi)與廣播消費(fèi)模式

    這篇文章主要介紹了RocketMQ集群消費(fèi)與廣播消費(fèi)模式,消息隊(duì)列RocketMQ版支持集群消費(fèi)和廣播消費(fèi),本文介紹集群消費(fèi)和廣播消費(fèi)的基本概念、適用場景、功能差異、注意事項(xiàng)以及設(shè)置方式
    2023-02-02

最新評論