一个综合的分布式项目之项目环境准备

    统一JDK为1.7
java

    tomcat端口上下游各为8080和8088,其余服务端口默认
mysql

    开发工具eclipse
git

    版本控制git(为方便公网查看,初期会托管到github)
github

    其余各项服务暂时不用启动,先建立项目。
redis

    建立maven项目相信看到这的oscer都会,这里就不赘述,主要贴出一些配置文件(限于篇幅随意贴了点,所有的请看github上)。spring

github地址:https://github.com/shang7053/base/tree/master/sustainable-parent sql

同时导入一份到osc-git,地址:http://git.oschina.net/loveliyiyi/basemongodb

    这篇博客改了好屡次,最终仍是决定不贴上配置信息,由于一方面占用篇幅太大,还有一方面他在博客里并无太多意义,只有用到的时候再贴就行了。
数据库

    基本环境如上所述,如今用文字说明一下各项目之间的关系。
apache

    首先是parent项目,这个项目集成了一些基本的依赖,好比spring相关、MQ、junit等等,是公共的依赖。

    而后是sustainable-interactive,这个项目集成了redis以及springmvc等依赖。

    最后是sustainable-service,这个就多了,有mybatis的、有mongo的等等。

    至于common的包基本上也就放点工具包,服务接口之类的,可能会用到好比gson的第三方工具,倒没什么可说的。

    还有一个就是各类配置文件,大部分集成到spring,小部分是基于之前本身写的工具包,好比redis,由于为了方便切换缓存,搞了一个公共的缓存接口,而后本身用oscache或者ehcache或者redis实现,因此这里就直接拿来用了。

    最后就是页面了,登录注册也没啥,最原生态的标签,因此页面看着……基本能用吧,就不献丑了。

    贴一下spring的配置文件吧,看这个估计就差很少了。

    sustainable-interactive

context:annotation-config />
	<context:component-scan base-package="sustainable.interactive" />
	<tx:annotation-driven/>
	<import resource="dubbo-customer.xml"/>
	<context:property-placeholder location="classpath:redis.properties" />  
  
    <import resource="mq.xml"/>

dubbo-customer.xml

<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方同样 -->
	<dubbo:application name="dubbo_consumer" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://zookeeper.sustainable.com:2181" />

	<!-- 生成远程服务代理,能够像使用本地bean同样使用demoService		xml配置时使用
	<dubbo:reference id="userDao" interface="scc.common.user.IUserDao" /> -->
	<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中全部的类 -->
	<dubbo:annotation package="sustainable.interactive" />

mq.xml

<!-- 基本配置开始 -->
    <!-- 链接工厂 -->
	<bean id="connectinFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<!-- 配置链接 -->
		<property name="brokerURL" value="tcp://activeMq.sustainable.com:61616" />
	</bean>
	<!-- spring管理链接的工厂 -->
	<bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
		<!-- 目标ConnectionFactory对应真实的能够产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="connectinFactory"></property>
		<!-- Session缓存数量 -->
		<property name="sessionCacheSize" value="10"></property>
	</bean>
	<!-- Spring JMS Template 配置JMS模版 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="cachingConnectionFactory" />
	</bean>
	<!-- 注册消息转换器 -->
	<bean id="messageConverter" class="sustainable.common.util.jms.SustainableMessageConverter"/>
	<!-- 基本配置结束 -->
	
	<!-- 注册队列开始 -->
	<!-- 注册Queue队列:仅有一个订阅者会收到消息,消息一旦被处理就不会存在队列中 -->
	<bean id="registQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<!-- 队列名称 -->
		<constructor-arg value="regist.notify"></constructor-arg>
	</bean>
	<!-- 使用Spring JmsTemplate 的消息生产者 -->
	<bean id="registQueueMessageProducer" class="sustainable.common.util.jms.QueueMessageProducer">
		<property name="jmsTemplate" ref="jmsTemplate"></property>
		<property name="queue" ref="registQueue"></property>
		<property name="messageConverter" ref="messageConverter"></property>
	</bean>
	<!-- 注册队列结束 -->

sustainable-service

<context:annotation-config />
	<context:component-scan base-package="sustainable.service" />
	<!-- 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://mysql.sustainable.com:3306/test" />
		<property name="user" value="root" />
		<property name="password" value="1234" />
		<!--链接池中保留的最小链接数。 -->
		<property name="minPoolSize" value="1" />
		<!--链接池中保留的最大链接数。Default: 15 -->
		<property name="maxPoolSize" value="5" />
		<!--初始化时获取的链接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="3" />
		<!--最大空闲时间,60秒内未使用则链接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="60" />
		<!--当链接池中的链接耗尽的时候c3p0一次同时获取的链接数。Default: 3 -->
		<property name="acquireIncrement" value="5" />
		<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但因为预缓存的statements 属于单个connection而不是整个链接池。因此设置这个参数须要考虑到多方面的因素。 若是maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 
			0 -->
		<property name="maxStatements" value="0" />
		<!--每60秒检查全部链接池中的空闲链接。Default: 0 -->
		<property name="idleConnectionTestPeriod" value="60" />
		<!--定义在从数据库获取新链接失败后重复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts" value="30" />
		<!--获取链接失败将会引发全部等待链接池来获取链接的线程抛出异常。可是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取链接。若是设为true,那么在尝试 获取链接失败后该数据源将申明已断开并永久关闭。Default: 
			false -->
		<property name="breakAfterAcquireFailure" value="true" />
		<!--因性能消耗大请只在须要的时候使用它。若是设为true那么在每一个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提高链接测试的性能。Default: 
			false -->
		<property name="testConnectionOnCheckout" value="false" />
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- mapper.xml文件对应的接口 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="sustainable.service.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<!-- 注解事物 -->
	<tx:annotation-driven />
	<aop:aspectj-autoproxy />
	<import resource="dubbo_provider.xml" />
	<import resource="mq.xml" />
	<import resource="mongo-config.xml"/>

dubbo_provider.xml

<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="dubbo_provider" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://zookeeper.sustainable.com:2181" />

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 声明须要暴露的服务接口 		xml式配置
	<dubbo:service interface="scc.common.user.IUserDao" ref="userDaoImpl" />-->
	<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中全部的类 -->
	<dubbo:annotation package="sustainable.service.service" />

mq.xml

<!-- 基本配置开始 -->
	<!-- ActiveMQ 链接工厂 -->
	<bean id="connectinFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://activeMq.sustainable.com:61616" />
	</bean>
	<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
	<bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
		<!-- 目标ConnectionFactory对应真实的能够产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="connectinFactory"></property>
		<!-- Session缓存数量 -->
		<property name="sessionCacheSize" value="10"></property>
	</bean>
	<!-- Spring JMS Template 配置JMS模版 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="cachingConnectionFactory" />
	</bean>
	<!-- 注册消息转换器 -->
	<bean id="messageConverter" class="sustainable.common.util.jms.SustainableMessageConverter"/>
	<!-- 基本配置结束 -->
	
	<!-- 注册队列开始 -->
	<!-- 注册Queue队列:仅有一个订阅者会收到消息,消息一旦被处理就不会存在队列中 -->
	<bean id="registQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<!-- 队列名称 -->
		<constructor-arg value="regist.notify"></constructor-arg>
	</bean>
	<!-- 消息处理 -->
	<bean id="registDealJmsMessage" class="sustainable.service.jms.RegistDeal"/>
	<!-- 异步接收消息处理类 -->
	<bean id="registQueueMessageListener" class="sustainable.common.util.jms.QueueMessageListener">
		<property name="dealJmsMessage" ref="registDealJmsMessage"></property>
	</bean>
	<!-- 消息监听容器 -->
	<bean id="registQueueContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectinFactory"></property>
		<property name="destination" ref="registQueue"></property>
		<property name="messageListener" ref="registQueueMessageListener"></property>
	</bean>
	<!-- 注册队列结束 -->
	
	<!-- 同步user队列开始 -->
	<!-- 注册Queue队列:仅有一个订阅者会收到消息,消息一旦被处理就不会存在队列中 -->
	<bean id="syncUserQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<!-- 队列名称 -->
		<constructor-arg value="syncUser.notify"></constructor-arg>
	</bean>
	<!-- 使用Spring JmsTemplate 的消息生产者 -->
	<bean id="syncUserQueueMessageProducer" class="sustainable.common.util.jms.QueueMessageProducer">
		<property name="jmsTemplate" ref="jmsTemplate"></property>
		<property name="queue" ref="syncUserQueue"></property>
		<property name="messageConverter" ref="messageConverter"></property>
	</bean>
	<!-- 同步user队列结束 -->
	
	<!-- 同步user队列开始 -->
	<!-- 消息处理 -->
	<bean id="syncUserDealJmsMessage" class="sustainable.service.jms.SyncUserDealJmsMessage"/>
	<!-- 异步接收消息处理类 -->
	<bean id="syncUserQueueMessageListener" class="sustainable.common.util.jms.QueueMessageListener">
		<property name="dealJmsMessage" ref="syncUserDealJmsMessage"></property>
	</bean>
	<!-- 消息监听容器 -->
	<bean id="syncUserQueueContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectinFactory"></property>
		<property name="destination" ref="syncUserQueue"></property>
		<property name="messageListener" ref="syncUserQueueMessageListener"></property>
	</bean>
	<!-- 同步user队列结束 -->

mongo-config.xml

<!-- Default bean name is 'mongo' -->
	<mongo:mongo host="mongo.sustainable.com" port="27017" />
	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg ref="mongo" />
		<constructor-arg name="databaseName" value="test" />
	</bean>

页面仍是上个图好了

相关文章
相关标签/搜索