使用全局的配置中心设置项目jar包的运行环境

使用全局的配置中心设置项目jar包的运行环境

在开发过程当中,我们可能会有好几套运行环境,此时我们如何来区别项目或者jar包运行在某个具体的运行环境中?
我们可以通过在jar包启动命令中添加相应的属性来区别对应的运行环境。下面将具体的讲解如何配置相应的属性及代码中获取相应的属性以此来区别获取不停运行环境中的相关配置。

1.在jar包启动命令上添加属性: -Dispro=uat 此时运行环境是uat环境

nohup /opt/apps/jdk/bin/java -Dispro=uat -jar /opt/deploy/service/BM-B-Task/BM-B-Task_Resume/BM-B-Resume-Task.jar -Xms256m -Xmx1024m &

2.在项目的启动类上继承extends ApplicationParent父类
import com.github.pagehelper.PageHelper;
import com.ylkz.base.ApplicationParent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Properties;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableTransactionManagement
@Configuration
public class ChannelDemandProviderApplication extends ApplicationParent {

    public static void main(String[] args) {

        SpringApplication.run(ChannelDemandProviderApplication.class, args);

    }

    //配置mybatis的分页插件pageHelper
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        properties.setProperty("dialect","mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }

}
3.具体的ApplicationParent类,通过获取系统属性System.getProperty(“ispro”)判断相应的运行环境
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApplicationParent {
    static Logger logger = LoggerFactory.getLogger(ApplicationParent.class);

    public ApplicationParent() {
    }

    static {
        String isPro = System.getProperty("ispro");
        logger.info("-------------------------------------------------------------------------------------------------");
        logger.info("isPro:" + isPro);
        if (isPro != null && "true".equals(isPro)) {
            logger.info("生产环境启动");
            System.setProperty("configUri", "http://b.conf.sc.banmaio.com/");
            System.setProperty("configProfile", "pro");
            System.setProperty("logstash.server", "172.18.XX.227:5000");
            System.setProperty("logstash.level", "pro");
            System.setProperty("configLabel", "master");
        } else if ("uat".equals(isPro)) {
            logger.info("uat环境启动");
            System.setProperty("configUri", "http://b.conf.sc.banmaio.com/");
            System.setProperty("configProfile", "uat");
            System.setProperty("logstash.server", "192.168.XX.70:5000");
            System.setProperty("logstash.level", "uat");
            System.setProperty("configLabel", "0.0.2-uat-SNAPSHOT");
        } else {
            logger.info("测试环境启动");
            System.setProperty("configUri", "http://b.conf.sc.banmaio.com/");
            System.setProperty("configProfile", "dev");
            System.setProperty("logstash.server", "10.0.XX.32:5000");
            System.setProperty("logstash.level", "dev");
            System.setProperty("configLabel", "0.0.1-test-SNAPSHOT");
        }

        logger.info("-------------------------------------------------------------------------------------------------");
    }
}

4.获取到相应的系统属性后,在启动配置bootstrap.yml中会获取不同运行环境的配置springconfig。

具体的请求格式:uri/label/name-profile.properties
举例:http://b.conf.sc.xxx.com/0.0.1-test-SNAPSHOT/YL-B-ChannelDemand-Provider-dev.properties

spring:
  application:
    name: YL-B-Channel-Demand-Provider
  cloud:
    config:
      username: banma
      password: banma2019
      uri: ${configUri}
      profile: ${configProfile}
      label: ${configLabel}
      name: YL-B-ChannelDemand-Provider

eureka:
  client:
    service-url:
      defaultZone: ${eureka.url}
#    register-with-eureka: false
  instance:
#    hostname: localhost
    prefer-ip-address: true
#management:
#  endpoints:
#    web:
#      exposure:
#        include:
#        - refresh
#        - health
#    
请求结果:

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值