java
主页 > 软件编程 > java >

Springboot如何使用外部yml启动

2024-05-07 | 佚名 | 点击:

Springboot使用外部yml启动

有时候我们想更灵活的使用配置文件,例如同一套代码去部署多个客户,此时差异大的地方其实只是配置文件,这是我们希望每次启动项目从外部读取配置文件来加载项目,你可以使用一些配置中心来实现,当然也可以自己定义外部文件来实现。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

import com.ctrip.framework.apollo.Config;

import com.ctrip.framework.apollo.ConfigChangeListener;

import com.ctrip.framework.apollo.ConfigService;

import com.ctrip.framework.apollo.model.ConfigChange;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.core.io.FileSystemResource;

  

import java.util.Properties;

import java.util.Set;

  

@SpringBootApplication

@Slf4j

public class LitchiDaqApplication {

    private static Properties PROPERTIES = new Properties();

  

    public static void main(String[] args) {

//      SpringApplication.run(LitchiDaqApplication.class, args);

        try {

            String filePath = System.getProperty("user.dir") + "/config" + "/application-daq.yml";

            //此处获取启动参数中的config.id来判断从哪里读取config文件

            String configId = System.getProperty("config.id");

            if (configId != null) {

                //从Apollo配置中心获取配置

                Config config = ConfigService.getAppConfig();

                //监听apollo配置修改

                config.addChangeListener(new ConfigChangeListener() {

                    @Override

                    public void onChange(ConfigChangeEvent changeEvent) {

                        log.info("发生改变的工作区: {}", changeEvent.getNamespace());

                        for (String key : changeEvent.changedKeys()) {

                            ConfigChange change = changeEvent.getChange(key);

                            log.info(String.format("检测到改变 - key: %s, oldValue: %s, newValue: %s, changeType: %s",

                                    change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));

                        }

                    }

                });

                Set<String> keys = config.getPropertyNames();

                for (String key : keys) {

                    PROPERTIES.setProperty(key, config.getProperty(key, null));

                }

            } else {

                YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();

                factory.setResources(new FileSystemResource(filePath));

                PROPERTIES = factory.getObject();

            }

        } catch (Exception e) {

            log.error("项目初始化配置错误:", e);

        }

        new SpringApplicationBuilder(LitchiDaqApplication.class).properties(PROPERTIES).run(args);

    }

}

项目模拟外部文件读取

java -jar启Spring boot项目使用外部yml

配置方式

1

java -jar xx.jar --spring.config.location=application.yml路径

配置单一变量

1

java -jar xxx.jar --xxx=test

取值

1

spring的@value("${xxx}")

1

java -jar .\ydbanew-cases-2.4.16.1.jar -TZ=Asia/Shanghai -filterParam=2400 -appconfig

原文链接:
相关文章
最新更新