俩字.Java 2024-01-24 17:46 采纳率: 50%
浏览 21
已结题

spring could 微服务 config配置

最近在学习SpringCloud微服务架构 统一配置中心-Config
我知道了Spring Cloud Config有两种角色ServerClient


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的文件内容

img

然后我继续跟着视频进行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进行配置的文件,在网上找了好久都没解决问题,希望指点迷津

仓库和文件结构如下:

img

img

  • 写回答

14条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-01-24 18:41
    关注

    引自免费微信小程序:皆我百晓生

    根据您提供的信息,config-client启动失败的原因是无法解析属性${address}。根据您的描述,config-server可以成功访问http://localhost:9277/config-dev.yml并返回正确的配置文件内容。

    这个问题可能是由于config-client未能正确获取到config-server的配置文件信息引起的。下面是我给出的解决方案:

    1. 检查config-client的配置文件:确保您的config-client的bootstrap.ymlapplication.yml文件中有正确的配置信息。特别是请确保name参数与config-server的配置文件名一致,如在这个例子中应为name: config-dev

    2. 检查config-client的启动类:检查您的config-client的启动类是否正确,特别是注解@EnableEurekaClient是否有添加。如果您正在使用Eureka注册中心,确保Eureka客户端的相应依赖已添加到pom.xml文件中。

    3. 检查config-server的访问地址:确认您的config-server的URI配置是否正确。确保bootstrap.ymlapplication.yml文件中的uri参数与config-server的地址一致。

    4. 检查config-server的配置文件名称:检查您的config-server的配置文件名称是否匹配config-client配置文件中的name参数。确保config-client通过name参数正确指定要获取的配置文件。

    如果上述解决方案都无法解决问题,请提供config-server和config-client的完整代码和项目结构。同时,提供您的操作系统、IDE和相关版本信息可能有助于更好地理解您的问题。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月31日
  • 赞助了问题酬金15元 1月24日
  • 请采纳用户回复 1月24日
  • 创建了问题 1月24日

悬赏问题

  • ¥40 微信小程序 使用vant组件ActionSheet 下拉面板,内容区域滚动会触发。scroll-view自定义下拉刷!即使设置停止下拉刷新也不行。
  • ¥15 专业问题提问,7月5号2点之前
  • ¥25 使用cube ai 导入onnx模型时报错
  • ¥15 关于#微信小程序#的问题:用一个网页显示所有关联的微信小程序数据,包括每个小程序的用户访问量
  • ¥15 root的安卓12系统上,如何使apk获得root或者高级别的系统权限?
  • ¥20 关于#matlab#的问题:如果用MATLAB函数delayseq可以对分数延时,但是延时后波形较原波形有幅度上的改变
  • ¥15 使用华为ENSP软件模拟实现该实验拓扑
  • ¥15 通过程序读取主板上报税口的数据
  • ¥15 matlab修改为并行
  • ¥15 尝试访问%1服务的windows注册表时遇到问题。必须先解决此问题,然后才能运行安装过程。(请确认您正在使用管理员权限运行)373