springcloud-config服务器,同样的配置在linux环境下不生效

原本在windows下能争取的获取远程配置但是部署到linux上死活都没有内容,然后开始了远程调试,这里顺带讲解下获取配置文件如果使用的是Git源,config service是如何响应接口并返回配置信息的。先说问题,我的服务名原本是abc-abc-abc这种,因为configService 在获取配置的时候允许使用 -来指定profiles,导致这种服务名会被认为 abc 是appname abc-abc是profiles。而正确的应该是abc-abc 是appname,abc是profiles。所有后面我把服务名改成了驼峰命名即 abcAbc 这样在获取的时候就可以识别。并且在git仓库中把配置文件的名字写成了abcAbc.properties,而远程仓库名在新建的时候仓库地址会默认给全部转换为小写
在这里插入图片描述
一开始我还没太在意这个事情,我是用的策略是一个微服务一个仓库的方式进行管理

spring.cloud.config.server.git.uri=http://your_git_space/your_org_name/{application}-config.git

但是由于git Url是小写,使用 http://your_git_space/your_org_name/abcAbc-config.git 无法访问到仓库,所以我在config client中配置了name

spring:
  cloud:
    config:
      enabled: true
      discovery:
        enabled: true #是否启动config server服务发现
        service-id: CONFIG-SERVICE #配置服务名称
      profile: ${spring.profiles.active}
      name: abcabc#获取配置是使用的application name,默认是spring.application.name
      label: master

config client 在拉取config的时候会以如下的方式进行访问
http://CONFIG-SERVICE/abcabc/dev/master

tip 对应的格式为 http://CONFIG-SERVICE/{appname}/{profile}/{branch|commit id}

在configService中,对应的JGitEnvironmentRepository会按照 spring.cloud.config.server.git.uri的配置将{xxx}提醒替换即,最终生成的git url为 http://your_git_space/your_org_name/abcabc-config.git 这样可以确保git url是一个正确的git仓库地址。然后神奇的事情来了 同样的配置,和git仓库地址。在linux上不行,在windows上可以。

这里有个需要注意的东西,spring.cloud.config.server.git.basedir=file:///E:/config-repo 在windows下需要是 file:/// 在linux下是file:/

其次在linux环境中如果没有使用file:开头,则spring会把本地配置文件的地址修改为 runpath+spring.cloud.config.server.git.basedir 配置的值,比如linux环境下配置
spring.cloud.config.server.git.basedir=~/config-service/config-repo,那么给到spring 配置文件加载的File地址就会变成 /home/user/config-server/~/config-service/config-repo

具体的官方文档描述在这里 file_system_backend

排除上面的注意点之后依然不可以,先来看看具体是怎么获取到配置文件的,访问http://CONFIG-SERVICE/fxdanmugw/dev/master,暴露该端点的是org.springframework.cloud.config.server.environment.EnvironmentController#labelled

	@RequestMapping(path = "/{name}/{profiles}/{label:.*}",
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Environment labelled(@PathVariable String name, @PathVariable String profiles,
			@PathVariable String label) {
		return getEnvironment(name, profiles, label, false);
	}

然后通过EnvironmentEncryptorEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)
···>CompositeEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)
···>CompositeEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)
···>MultipleJGitEnvironmentRepository#findOne
···>AbstractScmEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)

该超类的方法是通过 JGitEnvironmentRepository的实例调用的

好的核心逻辑到了我们看下源码

	public synchronized Environment findOne(String application, String profile,
			String label, boolean includeOrigin) {
		NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(
				getEnvironment(), new NativeEnvironmentProperties());
		Locations locations = getLocations(application, profile, label);
		delegate.setSearchLocations(locations.getLocations());
		Environment result = delegate.findOne(application, profile, "", includeOrigin);
		result.setVersion(locations.getVersion());
		result.setLabel(label);
		return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(),
				getUri());
	}

大致流程新建了一个NativeEnvironmentRepository 去读取本地文件,getLocations里面会根据application,profile,label 刷新对应的git仓库,保证仓库的version版本和远程git仓库一致,然后启动一个SpringApplication容器 使用配置–spring.config.path=file:/xxxx 的形式让这个容器去加载配置文件,在从上下文中取出所有和当前 application,profile,label 符合的配置返回出去,application=abcabc,profile=dev,label=master。有点不可思议完全没想到会使用SpringApplicationBuilder 构建一个容器进行配置读取,这个懒投的可以,当然可能是有其他必要原因导致不得不这么做,下面来看详细部分代码
git 仓库刷新部分之前博客有讲过,简单过一下
JGitEnvironmentRepository#getLocations ···> JGitEnvironmentRepository#refresh

delegate.setSearchLocations(locations.getLocations());locations就是过滤出来的本地仓库地址它的实际值可能是这样的
在这里插入图片描述
application被我马赛克了,理解成abcabc吧,然后依靠NativeEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)来获取Environment,具体代码如下

@Override
	public Environment findOne(String config, String profile, String label,
			boolean includeOrigin) {
		SpringApplicationBuilder builder = new SpringApplicationBuilder(
				PropertyPlaceholderAutoConfiguration.class);
		ConfigurableEnvironment environment = getEnvironment(profile);
		builder.environment(environment);
		builder.web(WebApplicationType.NONE).bannerMode(Mode.OFF);
		if (!logger.isDebugEnabled()) {
			// Make the mini-application startup less verbose
			builder.logStartupInfo(false);
		}
		String[] args = getArgs(config, profile, label);
		// Explicitly set the listeners (to exclude logging listener which would change
		// log levels in the caller)
		builder.application()
				.setListeners(Arrays.asList(new ConfigFileApplicationListener()));

		try (ConfigurableApplicationContext context = builder.run(args)) {
			environment.getPropertySources().remove("profiles");
			return clean(new PassthruEnvironmentRepository(environment).findOne(config,
					profile, label, includeOrigin));
		}
		catch (Exception e) {
			String msg = String.format(
					"Could not construct context for config=%s profile=%s label=%s includeOrigin=%b",
					config, profile, label, includeOrigin);
			String completeMessage = NestedExceptionUtils.buildMessage(msg,
					NestedExceptionUtils.getMostSpecificCause(e));
			throw new FailedToConstructEnvironmentException(completeMessage, e);
		}
	}

主要看args的值,如下

args[]数值
0 = "--spring.config.name=application,abcabc"
1 = "--spring.cloud.bootstrap.enabled=false"
2 = "--encrypt.failOnError=false"
3 = "--spring.config.location=file:/E:/config-repo/config-repo-6098804444752826688/"

注意看这里的–spring.config.name用的是 接口请求传进来的appname。在windows和linux中文件系统对文件名的大小写敏感程度不同,举个例子
在这里插入图片描述
在linux是允许大小写不同的文件存在的
在这里插入图片描述
罪魁祸首出现了,问题就是出现在这里,windows的文件系统与linux的差异导致了,他们表现上的差异,我的处理方式是,将git url的地址改成和服务一样的驼峰大小写,再删除掉config client中配置的name,或者直接不适用 特定的名称来编写配置文件,全部写道application.yml 里面

### 安装和配置Kafka #### 下载并解压Kafka 为了在Linux上安装Kafka,首先需要下载官方发布的压缩包。可以从Apache Kafka官方网站获取最新版本的二进制分发版。 ```bash wget https://downloads.apache.org/kafka/3.0.0/kafka_2.13-3.0.0.tgz tar -xzf kafka_2.13-3.0.0.tgz cd kafka_2.13-3.0.0/ ``` #### 配置ZooKeeper 因为Kafka依赖于ZooKeeper来管理集群状态和其他元数据信息,在启动Kafka之前应该先确保ZooKeeper正在运行。如果尚未安装ZooKeeper,则需按照官方文档完成其安装过程[^2]。 #### 修改Kafka配置文件 进入`config/server.properties`编辑必要的参数: ```properties broker.id=0 port=9092 host.name=localhost log.dirs=/tmp/kafka-logs zookeeper.connect=localhost:2181 listeners=PLAINTEXT://:9092 advertised.listeners=PLAINTEXT://localhost:9092 ``` 以上设置指定了Broker ID、监听端口、主机名、日志存储位置以及连接至本地ZooKeeper实例的方式。对于多节点集群来说,应当调整这些值以匹配各个机器的具体情况[^3]。 #### 启动服务 通过执行脚本来分别启动ZooKeeper和Kafka Broker: ```bash # Start ZooKeeper first bin/zookeeper-server-start.sh config/zookeeper.properties & # Then start the Kafka broker bin/kafka-server-start.sh config/server.properties & ``` 此时可以通过命令查看当前可用的主题列表验证是否成功启动了Kafka消息队列系统: ```bash sh bin/kafka-topics.sh --list --bootstrap-server localhost:9092 ``` ### 将Kafka与Spring Cloud集成 当完成了上述基础搭建之后,就可以着手准备让Spring Cloud应用程序能够利用Kafka作为消息总线的一部分了。这通常涉及到两个方面的工作——一个是配置中心(Config Server),另一个是客户端应用(Config Client)。 #### 设置Spring Cloud Config Server支持Kafka Bus 为了让配置服务器能接收到来自Git仓库变更的通知并通过Kafka广播出去,需要引入额外的支持库并在application.yml中指定相应属性: ```yaml spring: cloud: bus: enabled: true trace: enabled: false refresh: enabled: true kafka: id: ${vcap.application.instance_id:${spring.application.name}:${random.uuid}} destinations: output trust-certificate: classpath:/certificates/truststore.jks client-id: configServer brokers: localhost:9092 ``` 这段YAML片段启用了Spring Cloud Bus功能,并设置了用于发送事件的目标主题名称以及其他一些安全性和性能优化选项[^1]。 #### 更新Spring Cloud Config Clients 同样地,在每一个想要订阅配置更新通知的应用程序里也需要做类似的改动。主要是在pom.xml或build.gradle里面加入对`spring-cloud-starter-bus-kafka`模块的依赖声明,同时也要适当修改各自的application.yml文件以便正确接入到已有的Kafka集群当中去[^4]。 最后一步便是重启所有的微服务组件使得新的配置生效,这样每当有新的提交推送到关联的GitHub/GitLab项目时,就会触发一次全局范围内的动态刷新流程,从而实现了自动化运维的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值