请求提供基于Spring Profiles配置多数据库属性的Apache Camel博客示例
使用Spring Profiles配置Apache Camel数据库属性的示例
我来给你分享几个实用的配置示例,帮你用Spring Profiles搞定Apache Camel的数据库属性区分,适配*不同环境(开发、测试、生产)*的需求:
1. XML配置示例
如果你的项目还是用Spring XML配置,可以这样为不同Profile定义专属的数据库属性和数据源:
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 通用配置:允许忽略无法解析的属性,避免跨Profile的属性报错 --> <context:property-placeholder ignore-unresolvable="true"/> <!-- 开发环境Profile --> <beans:beans profile="dev"> <util:properties id="dbProperties" location="classpath:db-dev.properties"/> <beans:bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <beans:property name="driverClassName" value="${db.driver}"/> <beans:property name="url" value="${db.url}"/> <beans:property name="username" value="${db.username}"/> <beans:property name="password" value="${db.password}"/> </beans:bean> </beans:beans> <!-- 生产环境Profile --> <beans:beans profile="prod"> <util:properties id="dbProperties" location="classpath:db-prod.properties"/> <beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"> <beans:property name="driverClassName" value="${db.driver}"/> <beans:property name="jdbcUrl" value="${db.url}"/> <beans:property name="username" value="${db.username}"/> <beans:property name="password" value="${db.password}"/> <!-- 生产环境专属的连接池优化配置 --> <beans:property name="maximumPoolSize" value="${db.pool.size}"/> </beans:bean> </beans:beans> <!-- Camel SQL组件:自动引用当前激活Profile对应的数据源 --> <beans:bean id="sqlComponent" class="org.apache.camel.component.sql.SqlComponent"> <beans:property name="dataSource" ref="dataSource"/> </beans:bean> </beans:beans>
对应的属性文件示例:
db-dev.properties(开发环境):
db.driver=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://localhost:3306/dev_db db.username=dev_user db.password=dev_pass
db-prod.properties(生产环境):
db.driver=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://prod-db:3306/prod_db db.username=prod_user db.password=prod_pass db.pool.size=20
2. Java配置示例
如果你的项目用Spring Java Config,这种方式更灵活且易于维护:
@Configuration public class CamelDbConfig { // 开发环境专属配置 @Profile("dev") @Configuration @PropertySource("classpath:db-dev.properties") public static class DevDbConfig { @Value("${db.driver}") private String driver; @Value("${db.url}") private String url; @Value("${db.username}") private String username; @Value("${db.password}") private String password; @Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } } // 生产环境专属配置 @Profile("prod") @Configuration @PropertySource("classpath:db-prod.properties") public static class ProdDbConfig { @Value("${db.driver}") private String driver; @Value("${db.url}") private String url; @Value("${db.username}") private String username; @Value("${db.password}") private String password; @Value("${db.pool.size}") private int poolSize; @Bean public DataSource dataSource() { HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName(driver); dataSource.setJdbcUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setMaximumPoolSize(poolSize); return dataSource; } } // Camel SQL组件:自动注入当前激活Profile对应的数据源 @Bean public SqlComponent sqlComponent(DataSource dataSource) { SqlComponent component = new SqlComponent(); component.setDataSource(dataSource); return component; } }
3. 激活Spring Profile的常用方式
你可以通过以下几种方式指定当前使用的Profile:
- JVM启动参数:添加
-Dspring.profiles.active=dev或-Dspring.profiles.active=prod - 环境变量:设置
SPRING_PROFILES_ACTIVE=prod - 全局配置文件:在
application.properties中添加spring.profiles.active=dev
4. 在Camel路由中使用配置好的数据源
不管用XML还是Java配置,你都可以在Camel路由里直接使用sql:组件操作数据库:
XML路由示例
<camelContext xmlns="http://camel.apache.org/schema/spring"> <route id="pendingOrdersQuery"> <from uri="timer:checkOrders?period=60000"/> <to uri="sql:SELECT * FROM orders WHERE status='PENDING'"/> <log message="Found ${body.size()} pending orders to process"/> </route> </camelContext>
Java DSL路由示例
@Component public class OrderProcessingRoute extends RouteBuilder { @Override public void configure() throws Exception { from("timer:checkOrders?period=60000") .to("sql:SELECT * FROM orders WHERE status='PENDING'") .log("Found ${body.size()} pending orders to process"); } }
内容的提问来源于stack exchange,提问作者mgunjara
相关产品推荐
相关产品推荐

