Spring cloud 使用spring云名称空间和两个数据源
我有一个SpringIntegrationWar组件,我正在更新它以在私有PCF中运行。我在应用程序中定义了两个数据源和一个RabbitMQ连接工厂 我看到了Thomas Risberg关于使用云名称空间并同时处理多个服务的文章-。这是通过使用@Autowired和@Qualifier注释来处理的 我想知道,如果我们没有@Autowired和@Qualifier注释,例如将数据源连接到JdbcTemplate中,如何实现这一点。在这里,我们无法指定@Qualifier注释 我的应用程序是基于SpringXML配置的。我确实能够在其中一个数据源上使用@Autowired和@Qualifier注释,但另一个是JPA实体管理器。请参阅代码片段 非常感谢您的帮助Spring cloud 使用spring云名称空间和两个数据源,spring-cloud,spring-cloud-connectors,Spring Cloud,Spring Cloud Connectors,我有一个SpringIntegrationWar组件,我正在更新它以在私有PCF中运行。我在应用程序中定义了两个数据源和一个RabbitMQ连接工厂 我看到了Thomas Risberg关于使用云名称空间并同时处理多个服务的文章-。这是通过使用@Autowired和@Qualifier注释来处理的 我想知道,如果我们没有@Autowired和@Qualifier注释,例如将数据源连接到JdbcTemplate中,如何实现这一点。在这里,我们无法指定@Qualifier注释 我的应用程序是基于Sp
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="activity-monitor" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaProperties">
<value>
hibernate.format_sql=true
</value>
</property>
</bean>
<beans profile="cloud">
<cloud:data-source id="dataSource" service-name="actmon-db-service" />
</beans>
hibernate.format_sql=true
Java构建包:Java_buildpack_offline Java-buildpack-offline-v2.4.zip
Spring自动重新配置版本1.4.0
更新:这是两个数据源的完整配置,包括使用DAO从数据源加载属性的PropertySourcesPlaceholderConfigurer
<bean id="cic.application.ppc" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="properties" ref="cic.application.properties"/>
<property name="locations" ref="cic.application.propertyLocations"/>
</bean>
<bean id="cic.application.properties" class="java.util.Properties">
<constructor-arg value="#{cicPropertiesService.properties}"></constructor-arg>
</bean>
<bean id="cic.properties.propertiesService" name="cicPropertiesService"
class="com.emc.it.eis.properties.service.DefaultPropertiesService">
<constructor-arg index="0"
ref="cic.properties.propertiesDao" />
</bean>
<bean id="cic.properties.propertiesDao" class="com.emc.it.eis.properties.dao.JdbcPropertiesDao">
<constructor-arg ref="cic.properties.dataSource" />
</bean>
<beans profile="default">
<jee:jndi-lookup id="cic.properties.dataSource"
jndi-name="jdbc/intdb" />
</beans>
<beans profile="cloud">
<cloud:data-source id="cic.properties.dataSource" service-name="oracle-cicadm-db-service" />
</beans>
<beans>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="actmonDataSource" />
<property name="persistenceUnitName" value="activity-monitor" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaProperties">
<value>
hibernate.format_sql=true
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
<beans profile="default">
<jee:jndi-lookup id="dataSource"
jndi-name="jdbc/actmon" />
</beans>
<beans profile="cloud">
<cloud:data-source id="actmonDataSource" service-name="postgres-actmon-db-service" />
</beans>
<beans profile="default,cloud">
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="POSTGRESQL" />
</bean>
</beans>
hibernate.format_sql=true
部署时来自CF的输出。注意,它正在跳过javax.sql.DataSources的自动重新配置首先,来自Thomas的文章非常旧,并且引用了一个不推荐使用的支持库。您应该使用,而不是
org.cloudfoundry:cloudfoundry运行时:0.8.1
依赖项
然后,您可以遵循使用SpringCloudConnectors的XML配置的步骤。对于相同类型的多个服务,您需要为每个bean指定服务的名称。按照您的示例,假设您创建了两个名为inventory db
和customer db
的CF数据库服务,它们可能如下所示:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="inventory-dataSource" />
<property name="persistenceUnitName" value="activity-monitor" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaProperties">
<value>
hibernate.format_sql=true
</value>
</property>
</bean>
<beans profile="cloud">
<cloud:data-source id="inventory-dataSource" service-name="inventory-db">
<cloud:data-source id="customer-dataSource" service-name="customer-db">
</beans>
hibernate.format_sql=true
我已经通过使用SpringCloud:data source使用的工厂bean解决了这个问题,CloudDataSourceFactory
。创建此实例并连接配置,包括CF服务的服务名称
。这避免了PropertySourcesPlaceholderConfigurer在定义bean之前试图使用数据源的问题
<!--
configure cloud data source for using CloudDataSourceFactory; this is what spring cloud:data-source is using;
required to manually wire this data source bean as cloud:data-source bean gets defined in a phase after our
PropertySourcesPlaceholderConfigurer bean.
-->
<bean id="cic.properties.dataSource" class="org.springframework.cloud.service.relational.CloudDataSourceFactory">
<constructor-arg value="oracle-cicadm-db-service" />
<constructor-arg>
<!-- configuring minimal data source as it is used only to bootstrap properties on app start-up -->
<bean class="org.springframework.cloud.service.relational.DataSourceConfig">
<constructor-arg>
<bean class="org.springframework.cloud.service.PooledServiceConnectorConfig.PoolConfig">
<constructor-arg value="0" />
<constructor-arg value="2" />
<constructor-arg value="180" />
</bean>
</constructor-arg>
<!-- ConnectionConfig not required for cic.properties.dataSource so setting to null -->
<constructor-arg value="#{ null }" />
</bean>
</constructor-arg>
</bean>
你好,斯科特。用附加信息编辑了我的问题。很抱歉,我找到了对那篇文章的引用。我实际上没有使用那些库。我正在使用最新的GA版本的spring cloud connectors。没问题,我不确定您是否在使用较旧的LIB,只是想确定一下。只有当您的应用程序绑定了一个给定类型的服务时,才会启用spring应用程序中绑定服务的自动重新配置。看见在这种情况下,您正在手动配置数据源,所以不需要自动重新配置。如果您绑定了多个数据库,auto reconfig无法知道哪个绑定数据库应用于哪个目的。您确定推送应用程序时“云”配置文件已激活吗?Java buildpack的某些版本在所有情况下都不会自动激活此配置文件。请尝试运行cf set env appname SPRING\u PROFILES\u ACTIVE cloud
以确保安全。谢谢。这些文件使它更清楚了一点。我原以为自动重新配置和spring云连接器是捆绑在一起的。手动配置意味着spring云。因此,对于多个数据源,文档中的建议是使用@Qualifier注释来区分。日志确实表明云配置文件正在被激活。我想我已经在这里发现了问题。我们正在使用propertysourcesplaceplaceconfigurer
,并通过配置为指向cloud:data source
cic.properties.dataSource
的DAO将属性加载到其中propertysourcesplaceplaceconfigurer
是实现bean工厂后处理器的PriorityOrdered
。这里的问题似乎是,创建服务bean的CloudServiceIntroductor
类只是一个普通的bean工厂后处理器,因此在加载属性源时不存在。有没有办法解决这个问题,可以实施PriorityOrdered
tks