applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd
">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 定时器开关 开始 -->
<!-- <task:annotation-driven /> -->
<!-- 定时器开关 结束 -->
<!-- <bean id="V3Timer" class="com.test.soa.timer.V3Timer"></bean>
<task:scheduled-tasks> 每天早上六点,中午十二点,下午八点清除一次缓存内脏数据 0 0 0,6,12,18 * * ? <task:scheduled
ref="V3Timer" method="clearOverdueCache" cron="0/30 * * * * ?" /> </task:scheduled-tasks> -->
<!-- 配置要扫描的包 -->
<context:component-scan base-package="scc.provider"></context:component-scan>
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:config\jdbc.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username">
<value>${jdbc_user}</value>
</property>
<property name="password" value="${jdbc_password}" />
<!-- 若密碼加密需指定程序解密 -->
<!-- <property name="connectionProperties" value="config.decrypt=true" /> -->
<!-- 根据url自动识别 这一项可配可不配,如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName -->
<!-- <property name="driverClassName" value="${driverClass}" /> -->
<!-- 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall -->
<property name="filters">
<value>${filters}</value>
</property>
<!-- 最大连接池数量 -->
<property name="maxActive">
<value>${maxActive}</value>
</property>
<!-- 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 -->
<property name="initialSize">
<value>${initialSize}</value>
</property>
<!-- 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。 -->
<!-- <property name="maxWait"><value>60000</value></property> -->
<!-- 最小连接池数量 -->
<property name="minIdle">
<value>${minIdle}</value>
</property>
<!-- 有两个含义:1) Destroy线程会检测连接的间隔时间 2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明 -->
<property name="timeBetweenEvictionRunsMillis">
<value>${timeBetweenEvictionRunsMillis}</value>
</property>
<property name="minEvictableIdleTimeMillis">
<value>${minEvictableIdleTimeMillis}</value>
</property>
<!-- 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。 -->
<!-- <property name="validationQuery"><value></value></property> -->
<!-- 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 -->
<property name="testWhileIdle">
<value>${testWhileIdle}</value>
</property>
<!-- 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 -->
<property name="testOnBorrow">
<value>${testOnBorrow}</value>
</property>
<!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->
<property name="testOnReturn">
<value>${testOnReturn}</value>
</property>
<!-- 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。 -->
<!-- <property name="poolPreparedStatements"><value>false</value></property> -->
<!-- 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100 -->
<!-- <property name="maxOpenPreparedStatements"><value>-1</value></property> -->
<!-- 配置removeAbandoned对性能会有一些影响,建议怀疑存在泄漏之后再打开。在上面的配置中,如果连接超过30分钟未关闭,就会被强行回收,并且日志记录连接申请时的调用堆栈。 -->
<!-- 打开removeAbandoned功能 -->
<!-- <property name="removeAbandoned" value="true" /> -->
<!-- 1800秒,也就是30分钟 -->
<!-- <property name="removeAbandonedTimeout" value="1800" /> -->
<!-- 关闭abanded连接时输出错误日志 -->
<!-- <property name="logAbandoned" value="true" /> -->
</bean>
<!-- 配置mybitasSqlSessionFactoryBean -->
<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="scc.provider.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 事务配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<import resource="dubbo.xml"/>
</beans>
dubbo.xml
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 具体的实现bean -->
<!-- <bean id="IUserService" class="com.dubbo.test.service.impl.UserServiceImpl" /> -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo_provider" />
<!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234"
/> -->
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.0.56:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.common.inter.IUserService" ref="userServiceImpl" />
</beans>
接口实现类
package scc.provider.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scc.provider.dao.IUserDao;
import com.alibaba.dubbo.rpc.RpcContext;
import com.common.inter.IUserService;
import com.common.vo.User;
@Service
public class UserServiceImpl implements IUserService{
@Autowired
private IUserDao userDao;
public void insert(User user) {
System.out.println(RpcContext.getContext().getAttachment("index"));
userDao.insert(user);
}
public User getUserById(Integer id) {
return userDao.getUserById(id);
}
public List<User> getUsers() {
return userDao.getUsers();
}
}
上面至贴出关键部分代码,文章末尾会提供demo下载。
然后是服务消费者项目:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd
">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 定时器开关 开始 -->
<!-- <task:annotation-driven /> -->
<!-- 定时器开关 结束 -->
<!-- <bean id="V3Timer" class="com.test.soa.timer.V3Timer"></bean>
<task:scheduled-tasks> 每天早上六点,中午十二点,下午八点清除一次缓存内脏数据 0 0 0,6,12,18 * * ? <task:scheduled
ref="V3Timer" method="clearOverdueCache" cron="0/30 * * * * ?" /> </task:scheduled-tasks> -->
<!-- 配置要扫描的包 -->
<context:component-scan base-package="scc.cunsumer.controller"></context:component-scan>
<import resource="dubbo.xml"/>
</beans>
dubbo.xml
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo_consumer" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://192.168.0.56:2181" />
<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<dubbo:reference id="userService" interface="com.common.inter.IUserService" />
</beans>