docker-compose部署項目時,yml文件靈活設置配置項

0 背景

    SpringBoot項目爲基礎,docker打包項目,docker-compose用.yml部署項目,一些靈活項配置在app.yml(自定義名稱)文件中。例如經常變動的文件就需要外掛在服務器路徑下(與項目docker容器裏的路徑是一一對應的關係)。例如靈活配置的地址。本文以這兩種情況爲例,記錄配置過程。

1 首先,定義配置類

    配置類 ApplicationProperties

import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * Properties specific to Epidemic.
 * <p>
 * Properties are configured in the {@code application.yml} file.
 * See {@link io.github.jhipster.config.JHipsterProperties} for a good example.
 */
@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)  //這裏的"application"爲prod.yml裏配置項名稱     
public class ApplicationProperties {

    private String basePath;  //配置文件地址
    private String mapUrl;  //配置url地址

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    public String getMapUrl() {
        return mapUrl;
    }

    public void setMapUrl(String mapUrl) {
        this.mapUrl = mapUrl;
    }
}

    在啓動類上添加@EnableConfigurationProperties 註解,並配置ApplicationProperties類

@EnableConfigurationProperties({ApplicationProperties.class})

    在項目裏的prod.yml文件中,配置項

application:
   basePath: ${BASE_PATH}
   mapUrl: ${MAP_URL}

2 配置app.yml文件

  volumes爲外掛文件路徑配置項: /mnt/data/epidemic/file/爲文件在服務器的地址
  environment爲項目環境配置項:BASE_PATH和MAP_URL與 prod.yml裏對應
version: '2'
services:
  epidemic-app:
    image: epidemic
    volumes:
      - /mnt/data/epidemic/file/:/root/epidemic/     // /mnt/data/epidemic/file/爲文件外掛路徑
    environment:
      - _JAVA_OPTIONS=-Xmx512m -Xms256m
      - SPRING_PROFILES_ACTIVE=prod,swagger
      - MANAGEMENT_METRICS_EXPORT_PROMETHEUS_ENABLED=true
      - SPRING_DATASOURCE_URL=jdbc:mysql://172.26.4.13:3336/epidemic?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
      - JHIPSTER_SLEEP=30 # gives time for other services to boot before the application
      - BASE_PATH=/root/epidemic/
      - MAP_URL=http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer   //靈活配置url
    ports:
      - 9988:9966
  epidemic-mysql:
    extends:
      file: mysql.yml
      service: epidemic-mysql

3 代碼裏自定義路徑(擴展)

    一般來說,文件來自於不同的路徑。因此,外掛文件時,可能需要自定義路徑,一般以menu的方式定義。

public enum FileType {
    Epidemic("疫情數據"){
        @Override
        public String path() {
            return "epidemic";
        }
    },
    MapPoint("經緯度"){
        @Override
        public String path() {
            return "mapPoint";
        }
    };
    private String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    FileType(String description){
        this.description = description;
    }
    
    public abstract String path();
}

    調用時

        String path = Paths.get(properties.getBasePath(), FileType.Epidemic.path()).toString();

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章