Use profiles to specify environment profiles in Spring+Dubbo services

Posted by John Canyon on Fri, 10 May 2019 23:02:27 +0200

Spring provides profile features to isolate our configuration files so that the corresponding configuration items can be activated in different environments; it is easy to achieve uniform deployment of different configuration items in different environments;

Recently, in spring+dubbo's service project, we want to load different configuration files in different environments, so we immediately think of spring's profile feature. Then, because the service uses Dubbo internal scripts to start, we need to debug bat (wins) and sh (linux) scripts separately.

The activation logic of profile during the start-up of Dubbo script is as follows:

Created with Rapha l 2.1.2 profile priority read command line command line system environment variable default value start command line is not specified, read system environment value [spring.profiles.active] system environment is not configured, set default value use default value [pro] start with system environment configuration value start command line input profile activation value [dev/test/pro], press Input startup

1. Create spring-profile configuration files and corresponding resource files

<?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>

    <!-- Development Environment Profile -->
    <beans profile="dev">
        <context:property-placeholder location="classpath*:config/develop/*.properties" />
    </beans>

    <!-- Test Environment Profile -->
    <beans profile="test">
        <context:property-placeholder location="classpath*:config/test/*.properties" />
    </beans>

    <!-- Production Environment Profile -->
    <beans profile="pro">
        <context:property-placeholder location="classpath*:config/production/*.properties" />
    </beans>
</beans>

2. Write dubbo startup script

Here the original content of start.bat, start.sh comes from dubbo The script is extended to support spring-profile environment activation

  • start.bat(wins)
:: statement profile, Read configuration values from environment variables
set profile=%spring.profiles.active%
if defined profile set profile="%profile%"

:: Setting default values
if not defined profile set profile="pro"

:: Read the parameters passed in from the command line
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
#--------------------------------

# Declare profile and read configuration values from environment variables.
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'"

3. Some simple operations of bat and sh scripts

  1. In the above dubbo startup script, we use the script to read and read system environment variables, declare variables, assign variables, determine whether variables are defined, whether variables are empty strings, and use variables.
  2. Provide a script to query replacement strings in the shell
#!/bin/bash

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

echo $result
  1. More bat scripting tutorials: CMD
  2. More shell Scripting tutorials: Shell

Topics: Spring Dubbo shell Linux