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:
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
- 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.
- 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