基于SpringMVC XML配置文件的Dubbo开发与使用
[模块一]
首先引入Dubbo的依赖资源,这里我们使用基于SpringMVC的项目于Dubbo进行整合
先进行依赖导入。
pom.xml
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<!--database-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--log-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
</dependencies>
首先是服务提供者端,在这里可以理解为Service和Dao层,这是一个单独的项目[SpringMVC_Provider]
第一步,创建Dao
/**
* 用户持久层接口
*/
public interface UserDao {
/**
* 基于id去查询数据
*
* @param id 用户id
* @return 用户数据
*/
User findById(@Param("id") Integer id);
}
第二步、使用mybatis映射该接口,以实现数据查询
UserDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aircoinst.dao.UserDao">
<!--基于id查询数据-->
<select id="findById" parameterType="integer" resultType="com.aircoinst.pojo.User">
select *
from t_user
where id = #{id}
</select>
</mapper>
第三步,创建接口与类
UserService
/**
* 用户的业务接口
*/
public interface UserService {
/**
* 根据用户id来查询用户数据
*
* @param id 用户id
* @return 用户对象
*/
public User findById(Integer id);
}
实现这个接口
/**
* 用户业务接口实现类
*/
public class UserServiceImpl implements UserService {
//依赖注入
@Autowired
private UserDao userDao;
/**
* 基于id查询用户
*
* @param id 用户id
* @return 用户数据
*/
@Override
public User findById(Integer id) {
return userDao.findById(id);
}
}
第四步,创建Spring配置文件
spring-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://dubbo.apache.org/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 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--配置dubbo_provider工程的dubbo应用名称-->
<dubbo:application name="dubboProvider"/>
<!--配置dubbo注册zookeeper地址-->
<dubbo:registry address="zookeeper://192.168.10.101:2181"/>
<!--配置需要暴露出来的接口与接口实现类和超时时间-->
<dubbo:service interface="com.aircoinst.service.UserService" ref="userService" timeout="1200000"/>
<!--接口实现类对象-->
<bean id="userService" class="com.aircoinst.service.impl.UserServiceImpl"/>
</beans>
spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--导入properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="typeAliasesPackage" value="com.aircoinst.pojo"/>
</bean>
<!--配置Mapper扫描器-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.aircoinst.dao"/>
</bean>
</beans>
spring-service.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:contex="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解扫描-->
<contex:component-scan base-package="com.aircoinst.service"/>
<!--配置事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource"/>
</bean>
<!--开启事务-->
<tx:annotation-driven/>
</beans>
这个服务提供者端就创建成功了,并配置Tomcat使该服务提供者端的端口号为8080
[模块二]
然后是服务消费者端,在这里可以理解为Controller和View层,这是一个单独的项目[SpringMVC_C]
先进行依赖导入
pom.xml
<dependencies>
<!--引入dubbo_interface子模块-->
<dependency>
<groupId>org.example</groupId>
<artifactId>dubbo_interface</artifactId>
<version>1.0</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-annotations.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-annotations.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-annotations.version}</version>
</dependency>
<!--log-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
第一步,创建Controller
UserController
package com.aircoinst.controller;
import com.aircoinst.pojo.User;
import com.aircoinst.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 用户操作控制器
*/
@RequestMapping("/user")
public class UserController {
//依赖注入Service对象
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
/**
* 基于id查询用户
*
* @param id 用户id
* @return 用户对象
*/
@RequestMapping("/findById")
@ResponseBody
public User findById(Integer id) {
return userService.findById(id);
}
}
第二步,创建Spring配置文件
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.aircoinst.controller"/>
<!--开启mvc注解-->
<mvc:annotation-driven/>
</beans>
spring-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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--配置dubbo_consumer工作在dubbo中应用名称-->
<dubbo:application name="dubboConsumer"/>
<!--配置dubbo注册中心zookeeper地址-->
<dubbo:registry address="zookeeper://192.168.10.101:2181"/>
<!--配置需要订阅的业务接口以及引用-->
<dubbo:reference id="userService" interface="com.aircoinst.service.UserService"/>
<!--初始化控制器,并把远程service注入到controller-->
<bean id="userController" class="com.aircoinst.controller.UserController">
<property name="userService" ref="userService"/>
</bean>
<!--启动时检查提供者是否存在-->
<dubbo:consumer check="true"/>
</beans>
这个服务提供者端创建成功了,并配置Tomcat使该服务提供者端的端口号为8081
使用浏览器输入http://localhost:8081/user/findById.do?id=1 发现数据返回正确就可以使用了。