本文介紹了為什么Spring-cloud-starter-config會(huì)忽略bootstrap.properties?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我的目標(biāo)是從config-service
獲取world-service
的配置。
架構(gòu):
config-service
依賴spring-cloud-config-server
位于localhost:8888
world-service
依賴spring-web
和spring-cloud-starter-config
。
我所做的:
-
我已經(jīng)設(shè)置了配置服務(wù)器并向
http://localhost:8888/hello-service/master
發(fā)送了GET請(qǐng)求,配置服務(wù)器從the config-repo repository獲取hello-service.properties
。(如果您需要config-service
的源代碼,我會(huì)推送到this repository。)
我的預(yù)期結(jié)果:
world-service
使用端口8081。
我的實(shí)際結(jié)果:
world-service
使用端口8080。
bootstrap.properties
spring.application.name=world-service
spring.cloud.config.uri=http://localhost:8888
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.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>world-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>world-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.0-M5</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-config</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
推薦答案
在Spring Cloud 2020中,他們更改了引導(dǎo)的工作方式,您必須添加新的啟動(dòng)器:spring-cloud-starter-bootstrap
。
這篇關(guān)于為什么Spring-cloud-starter-config會(huì)忽略bootstrap.properties?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,