配置中⼼⼊门案例以及出现
CouldnotresolveplaceholderXXXinva。。。编写配置中⼼的服务端
创建项⽬
修改 pom ⽂件添加 config-server 坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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.1.11.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>config-server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</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-config-server</artifactId>
</dependency>
<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>
修改配置⽂件添加 Git 地址(将配置⽂件上传到gitee上)
spring.application.name=yxf-config-server
server.port=9010
eureka.client.service-url.defaultZone=admin:1234@192.168.41.242:5050/eureka/,admin:1234@192.168.41.242:5051/eureka/ #Git配置
fig.server.git.uri=gitee/sakurayxf/config
#fig.server.git.username=
#fig.server.git.password=
修改启动类
package com.bjsxt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.fig.server.EnableConfigServer;
import org.springframework.cloudflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
创建四个临时配置⽂件
注意命令规则
通过配置中⼼访问配置⽂件
编写配置中⼼的客户端
修改 pom ⽂件添加配置中⼼客户端坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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.1.11.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>config-client</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</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.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>springcloud和springboot
<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>
修改启动类
package com.bjsxt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloudflix.eureka.EnableEurekaClient;
import t.annotation.Bean;
import t.support.PropertySourcesPlaceholderConfigurer; @SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
修改配置⽂件
修改配置⽂件的名称。客户端配置⽂件名称必须为bootstrap.properties spring.application.name=config-client
server.port=9011
eureka.client.service-url.defaultZone=admin:1234@192.168.41.242:5050/eureka/,admin:1234@192.168.41.242:5051/eureka/ #默认false,这⾥设置true,表⽰开启读取配置中⼼的配置
abled=true
#对应 eureka 中的配置中⼼ serviceId,默认是 configserver
fig.discovery.service-id=yxf-config-server
#指定环境
fig.profile=dev
#git 标签
fig.label=master
编写测试代码
package ller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ShowController {
@Value("${e-book}")
private String msg;
@RequestMapping("/msg")
public String Msg(){
return msg;
}
}
在写这个的过程中,实验了好⼏次都会出现这样的问题
度娘也说了⼏种解决⽅法:
1.就是配置⽂件必须是bootstrap.properties/yml
2.就是
去注册中⼼中的服务必须名称正确。
3.就是我遇到的这种,
客户端的名称必须是config-client,不会就读取不到值。
总之这个问题花费了我很长时间,最后还是⽼师过来帮我解决的,问题也是不容易发现。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。