最近在学习SpringCloud微服务架构 统一配置中心-Config
我知道了Spring Cloud Config有两种角色Server和Client。
Server作为配置中心的服务端作用:当配置客户端获取配置时,服务端及时从Git仓库中获取配置副本,从而保证配置数据为最新。支持从yml、json、properties等文件加载配置
Client只需要在引导配置文件中声明所使用的配置服务器地址即可
根据视频我给我的Gitee仓库设置成可以公开访问并对server完成了以下关键代码:
pom文件
<!--config的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
配置文件application.yml
server:
port: 9277
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/k-mofo-QWER/study-work2023.git
label: master
启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer//开启config server 服务
public class ConfigApp {
public static void main(String[] args) {
SpringApplication.run(ConfigApp.class,args);
}
}
点击启动类启动成功后在谷歌浏览器上输入访问文件的地址:http://localhost:9277/config-dev.yml,
成功访问到我仓库里名为config-dev.yml的文件内容
然后我继续跟着视频进行config-Client的编写,完成了以下关键代码:
pom文件
<dependencies>
<!--config client 的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-web</artifactId>
</dependency>
</dependencies>
配置文件application.yml
server:
port: 8021
spring:
application:
name: config-consumer
eureka:
client:
service-url:
dafaultzone: http://localhost:8761/eureka
配置文件bootstrap.yml
spring:
cloud:
config:
profile: dev
uri: http://localhost:9277/
name: config
label: master
启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigDemo1App {
public static void main(String[] args) {
SpringApplication.run(ConfigDemo1App.class,args);
}
}
访问类
package com.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class MyConfigController {
//https://gitee.com/k-mofo-QWER/study-work2023.git
@Value("${address}")
private String address;
@RequestMapping("/test")
public String test01(){
return address;
}
}
然后进行配置中心客户端访问外部文件内容的测试
首先启动config-server
然后点击config-client启动类进行启动
结果是config-client启动失败,控制台显示报错如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-01-24 16:57:12.474 ERROR 9708 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myConfigController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'address' in value "${address}"
访问http://localhost:9277/config-dev.yml
可以访问到我Gitee仓库里面的内容
由于config-client启动失败,所以并不能访问http://localhost:8021/hello/test
但是如果在bootstrap.yml或者application.yml添加属性address可以运行成功,并能通过浏览器访问并返回本地配置文件的内容
说明是没有读取到通过config-server进行配置的文件,在网上找了好久都没解决问题,希望指点迷津
仓库和文件结构如下: