Spring+Dubbo服务中使用profile指定环境配置文件

Spring提供的profile特性可以对我们的配置文件进行隔离, 以便在不同的环境激活对应的配置项; 轻松实现不同环境不同配置项统一部署的形式;

最近在spring+dubbo的服务项目中, 想实现在不同的环境加载不同的配置文件, 因此就立即想到了spring的profile特性, 然后由于服务采用dubbo内部脚本的启动方式, 所以这里就需要分别调试bat(wins), sh(linux)两种脚本内容;

Dubbo脚本启动过程中对profile的激活逻辑为:

Created with Raphaël 2.1.2 profile优先级读取 命令行 命令行 系统环境变量 系统环境变量 默认值 默认值 启动 启动 命令行未指定,读取系统环境值[spring.profiles.active] 系统环境未配置, 设置默认值 使用默认值[pro]启动 用系统环境配置值启动 命令行传入了profile激活值[dev/test/pro], 就按传入的启动

一. 创建spring-profile配置文件, 并创建对应资源文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <description>spring-profile configure</description>

    <!-- 开发环境配置文件 -->
    <beans profile="dev">
        <context:property-placeholder location="classpath*:config/develop/*.properties" />
    </beans>

    <!-- 测试环境配置文件 -->
    <beans profile="test">
        <context:property-placeholder location="classpath*:config/test/*.properties" />
    </beans>

    <!-- 生产环境配置文件 -->
    <beans profile="pro">
        <context:property-placeholder location="classpath*:config/production/*.properties" />
    </beans>
</beans>

二. 编写dubbo的启动脚本

这里的start.bat, start.sh原始内容来自dubbo, 下面对脚本进行扩展以支持spring-profile的环境激活

  • start.bat(wins)

:: 声明profile, 并从环境变量中读取配置值
set profile=%spring.profiles.active%
if defined profile set profile="%profile%"

:: 设置默认值
if not defined profile set profile="pro"

:: 读取命令行传入的参数
if "%1"=="dev" set profile="dev"
if "%1"=="test" set profile="test"
if "%1"=="pro" set profile="pro"

if ""%2"" == ""debug"" goto debug
if ""%2"" == ""jmx"" goto jmx

:start
java -Xms64m -Xmx1024m -XX:MaxPermSize=64M -Ddubbo.application.logger="slf4j" -Dspring.profiles.active=%profile% -classpath ..\conf;%LIB_JARS% com.alibaba.dubbo.container.Main
goto end
  • start.sh(linux)

#--------------------------------
# spring+dubbo profile process
#--------------------------------

# 声明profile, 并从环境变量中读取配置值,
profile=${spring.profiles.active}

if test -z "$profile"
then
  profile="pro"
else
  profile="$profile"
fi

if [ "$1" == "dev" ]
then
   profile="dev"
elif [ "$1" == "test" ]
then
   profile="test"
elif [ "$1" == "pro" ]
then
   profile="pro"
fi

JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Ddubbo.application.logger="slf4j" -Dspring.profiles.active='$profile'"

三. bat和sh脚本的一些简单操作

  1. 上面的dubbo启动脚本中, 分别用到了脚本中读取系统环境变量, 声明变量, 变量赋值, 判断变量是否定义, 变量是否为空字符串, 使用变量;
  2. 再提供一个shell中查询替换字符串的脚本
#!/bin/bash

str="1,2,3,4,5"
find=","
replace=""
result=${str//$find/$replace}

echo $result
  1. 更多bat脚本教程: CMD
  2. 更多shell脚本教程: Shell
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值