基于Dubbo的CRUD案例
基于maven,dubbo,zookeeper实现增删改查的功能
项目设计
070401-dubbo-parent
管理项目所需的所有jar包、插件的版本,pom工程
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 对依赖的jar包的版本统一进行定义 -->
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mysql.version>5.1.32</mysql.version>
<slf4j.version>1.6.4</slf4j.version>
<druid.version>1.0.9</druid.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<tomcat.version>2.2</tomcat.version>
<jsp-api.version>2.0</jsp-api.version>
<zkClient-version>0.10</zkClient-version>
<dubbo-version>2.5.4</dubbo-version>
</properties>
<!-- jar包的依赖注入 ,由于该工程是一个父工程,所以jar包在该pom文件中只是声明 -->
<dependencyManagement>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Mybatis -->
<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>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</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-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo-version}</version>
</dependency>
<!-- 注册中心客户端 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkClient-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<!-- tomcat插件,由于子项目不一定每个都是web项目,所以该插件只是声明,并未开启 -->
<pluginManagement>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
070401-dubbo-mapper
是父工程的子工程,依赖于pojo,并需要注入mybatis、MySQL、连接池
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
<!-- 资源拷贝插件 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
mapper层接口
import java.util.List;
import com.bjsxt.pojo.Users;
public interface UsersMapper {
void insertUsers(Users users);
List<Users> findAll();
void del(int userid);
Users selByUserId(int userid);
void updateUser(Users users);
}
mapper层的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.bjsxt.mapper.UsersMapper">
<insert id="insertUsers" parameterType="com.bjsxt.pojo.Users">
insert into users(username,userage) values(#{username},#{userage})
</insert>
<select id="findAll" resultType="com.bjsxt.pojo.Users">
select * from users
</select>
<delete id="del" parameterType="int">
delete from users where userid = #{userid}
</delete>
<select id="selByUserId" parameterType="int" resultType="com.bjsxt.pojo.Users">
select * from users where userid=#{userid}
</select>
<update id="updateUser" parameterType="com.bjsxt.pojo.Users" >
update users set username=#{username},userage=#{userage} where userid=#{userid}
</update>
</mapper>
070401-dubbo-pojo
父工程的子模块,jar工程,规定实体属性
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build/>
</project>
070402-dubbo-provider
父工程的子工程,pom类型的工程,规定服务端所提供的服务
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>070402-dubbo-provider</artifactId>
<packaging>pom</packaging>
<build/>
<modules>
<module>070402-dubbo-interface</module>
<module>070402-dubbo-user-service</module>
</modules>
</project>
070402-dubbo-interface
provider的子模块,依赖pojo,jar工程,规定服务的功能
pomx.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>070402-dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>070402-dubbo-interface</artifactId>
<dependencies>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build/>
</project>
070402-dubb-user-service
provier的子模块,依赖interface和mapper工程,并引入dubbo服务框架,注册中心框架,spring框架,打包插件assembly(用于将jar包达成tar格式,使其在虚拟机上运行),因此这个模块最重要
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>070402-dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>070402-dubbo-user-service</artifactId>
<dependencies>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070402-dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入dubbo服务框架 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- 引用注册中心应用 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- 引入框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 指定项目的打包插件信息 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- 指定打包描述文件的位置:相对项目根目录的路径 -->
<!-- assembly打包的描述文件 -->
<descriptor>assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bjsxt.dubbo.service.AddUserDubboService;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
/**
* 调用provider的usersMapper接口,但是需要注入
* 增删改查的实现类其他类似故省略
* @author chy
*
*/
@Service
public class AddUsersServiceImpl implements AddUserDubboService {
@Autowired
private UsersMapper uMapper;
@Override
public void addUser(Users users) {
System.out.println("AddUsersServiceImpl.adduser() 执行..."+users);
this.uMapper.insertUsers(users);
}
}
在myeclipse用于启动服务的类
import com.alibaba.dubbo.container.Main;
/**
* 启动类
*
* @author chy
*
*/
public class start {
public static void main(String[] args) {
Main.main(args);
}
}
application-spring-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<import resource="../../applicationContext-dao.xml"/>
<import resource="../../applicationContext-service.xml"/>
<import resource="../../applicationContext-trans.xml"/>
<dubbo:application name="user-provider"/>
<dubbo:registry address="192.168.179.128:2181,192.168.179.128:2182,192.168.179.128:2183" protocol="zookeeper"></dubbo:registry>
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!-- 注册provider服务/接口中的服务类 ,ref的是provider中的实现类,注意首字母小写-->
<dubbo:service interface="com.bjsxt.dubbo.service.AddUserDubboService" ref="addUsersServiceImpl"></dubbo:service>
<dubbo:service interface="com.bjsxt.dubbo.service.FindUserDubboService" ref="findUsersServiceImpl"></dubbo:service>
<dubbo:service interface="com.bjsxt.dubbo.service.DeleteUserDubboService" ref="deleteUsersSericeImpl"></dubbo:service>
<dubbo:service interface="com.bjsxt.dubbo.service.SelUserDubboService" ref="selUserDubboServiceImpl"></dubbo:service>
<dubbo:service interface="com.bjsxt.dubbo.service.UpdateUserDubboService" ref="updateUserServiceImpl"></dubbo:service>
</beans>
applicationContext-dao.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置解析properties文件的工具类 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- <context:property-placeholder location="classpath*:*.properties"/> -->
<!-- 配置数据源 dataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- 创建mybatis的上下文对象 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation">
<value>classpath:SqlMapperClient.xml</value>
</property>
</bean>
<!-- 扫描mybatis的接口与映射配置文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bjsxt.mapper"/>
</bean>
</beans>
applicationContext-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描bean对象 -->
<context:component-scan base-package="com.bjsxt.dubbo.service.impl"/>
</beans>
applicationContext-trans.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:p="http://www.springframework.org/schema/p"
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置事物管理器的切面 -->
<bean id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事物传播行为 :其实就是那些方法应该受什么样的事物控制-->
<tx:advice id="advice" transaction-manager="transactionMananger">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="dorp*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 那些类下的方法需要参与到当前的事物管理中 。配置切点 -->
<aop:config>
<aop:advisor advice-ref="advice" pointcut="execution(* com.bjsxt.dubbo.service.impl*.*(..))"/>
</aop:config>
</beans>
db,properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.179.128:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
SqlMapperClient.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置分页插件 -->
</configuration>
assembly打包插件及文件夹自行下载
070403-dobbo-consumer
父工程的子工程,pom工程,依赖interface工程,pojo工程,引入spring的jar包
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>070403-dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>070403-dubbo-user-partal-services</artifactId>
<dependencies>
<!-- 导入070402-dubbo-interface的坐标,方面业务层实现类调用服务类 -->
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070402-dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 令注解生效的spring的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
070403-dubbo-user-protal-service
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>070403-dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>070403-dubbo-user-partal-services</artifactId>
<dependencies>
<!-- 导入070402-dubbo-interface的坐标,方面业务层实现类调用服务类 -->
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070402-dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 令注解生效的spring的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070401-dubbo-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
import java.util.List;
import com.bjsxt.pojo.Users;
public interface UserService {
void addUsers(Users users);
List<Users> findAllUser();
void delByUserid(int userid);
Users selByUserid(int userid);
void updateUser(Users users);
}
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bjsxt.dubbo.service.AddUserDubboService;
import com.bjsxt.dubbo.service.DeleteUserDubboService;
import com.bjsxt.dubbo.service.FindUserDubboService;
import com.bjsxt.dubbo.service.SelUserDubboService;
import com.bjsxt.dubbo.service.UpdateUserDubboService;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserService;
@Service
public class UserServiceImpl implements UserService {
//注入provider中的接口类
@Autowired
private AddUserDubboService addUserDubboService;
@Autowired
private FindUserDubboService findUserDubboService;
@Autowired
private DeleteUserDubboService deleteUserDubboService;
@Autowired
private SelUserDubboService selUserDubboService;
@Autowired
private UpdateUserDubboService updateUserDubboService;
@Override
public void addUsers(Users users) {
this.addUserDubboService.addUser(users);
}
@Override
public List<Users> findAllUser() {
return this.findUserDubboService.findAllUser();
}
@Override
public void delByUserid(int userid) {
this.deleteUserDubboService.deleteByUserid(userid);
}
@Override
public Users selByUserid(int userid) {
return this.selUserDubboService.selByUserid(userid);
}
@Override
public void updateUser(Users users) {
this.updateUserDubboService.updateUser(users);
}
}
070403-dubbo-user-portal
consumer的子模块,唯一的war工程,依赖070403-dubbo-user-partal-services,并引入web应用相关,单元测试,日志处理,spring、jsp相关、dubbo服务框架,tomcat插件,相当于springmvc的控制器
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>070403-dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>070403-dubbo-user-portal</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>070403-dubbo-user-partal-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件
clean tomcat7:run 本地运行 tomcat ,不需要远程热部署 ROOT
tomcat7:redeploy 若在远程的 tomcat 中,若存在相同项目名的话,不属于第一次发布 war ,远程热部署
tomcat7:deploy 适用于第一次发布 war ,远程热部署
-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- path: 上传的war包解压后的路径命名 -->
<path>/</path>
<port>8080</port>
<!-- url : 上传war包到什么位置,除IP和端口可以修改外其他不变 -->
<!-- <url>http://192.168.179.129:8080/manager/text</url>
为tomcat配置的管理用户名和密码.
<username>admin</username>
<password>admin</password> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
@RequestMapping("/index")
public String showIndex(){
return "index";
}
@RequestMapping("/addUser")
public String addUser(){
return "addUser";
}
}
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
//注入consumer中的接口类
@Autowired
private UserService userService;
@RequestMapping("/addUser")
public String addUser(Users users) {
this.userService.addUsers(users);
return "ok";
}
@RequestMapping("/findAll")
public String findAll(Model model) {
List<Users>usersList= userService.findAllUser();
model.addAttribute("usersList", usersList);
return "usersList";
}
@RequestMapping("/deluser/{userid}")
public String deluser(@PathVariable int userid) {
this.userService.delByUserid(userid);
return "ok";
}
@RequestMapping("/selUser/{userid}")
public String selUser(@PathVariable int userid,Model model) {
Users users=this.userService.selByUserid(userid);
model.addAttribute("users", users);
return "upPage";
}
///user/updateUser/${users.username}&${users.userage }&${users.userid }
@RequestMapping("/updateUser")
private String updateUser(Model model,Users users) {
this.userService.updateUser(users);
return "ok";
}
}
applicationContext-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- <dubbo:consumer timeout="20000" check="false" /> -->
<dubbo:application name="myconsumer"/>
<dubbo:registry address="192.168.179.128:2181,192.168.179.128:2182,192.168.179.128:2183" protocol="zookeeper"></dubbo:registry>
<!-- 在注册中心注入要调用的服务,不注入无法消费服务,服务类名小写,服务权限定路径 -->
<dubbo:reference id="addUserDubboService" interface="com.bjsxt.dubbo.service.AddUserDubboService"></dubbo:reference>
<dubbo:reference id="findUserDubboService" interface="com.bjsxt.dubbo.service.FindUserDubboService"></dubbo:reference>
<dubbo:reference id="deleteUserDubboService" interface="com.bjsxt.dubbo.service.DeleteUserDubboService"></dubbo:reference>
<dubbo:reference id="selUserDubboService" interface="com.bjsxt.dubbo.service.SelUserDubboService"></dubbo:reference>
<dubbo:reference id="updateUserDubboService" interface="com.bjsxt.dubbo.service.UpdateUserDubboService"></dubbo:reference>
</beans>
applicationContext-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描bean对象 -->
<context:component-scan base-package="com.bjsxt.service"/>
</beans>
springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 包的扫描器主要是扫描@controller -->
<context:component-scan base-package="com.bjsxt.web.controller"/>
<!-- 注册两个新对象 主要是为了来处理springmvc中的其他anntation 如:@requestmapping -->
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" /><!-- jsp所在的前缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置静态资源映射 -->
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
</beans>
jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>欢迎访问时间静止的小窝</h1>
<a href="/addUser">添加用户</a><br>
<a href="/user/findAll">查询用户</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'usersList.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table border="1" align="center">
<tr>
<th>id</th>
<th>用户名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:forEach items="${usersList }" var="list">
<tr>
<td>${list.userid }</td>
<td>${list.username }</td>
<td>${list.userage }</td>
<td><a href="/user/deluser/${list.userid }">删除</a> <a href="/user/selUser/${list.userid }">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
碍于篇幅,其他jsp省略
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 上下文参数,告诉Spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置springmvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
后台逻辑功能添加顺序
总结
1、mapper,打包
2、provider中interface,service创建抽象类和抽象方法,@Service,@Autowired,
3、provider中dubbo配置服务,注册provider服务/接口中的服务类 ,ref的是provider中的实现类,注意首字母小写,打包provider,
4、start发布服务
5、实现删除用户的业务,业务层的接口与实现类,注入provider中的接口类
6、controller,传参的写法,consumer中dubbo配置服务,在注册中心注入要调用的服务(id为首字母小写),不注入无法消费服务,打包
源码已经分享至百度云
链接:https://pan.baidu.com/s/1mIVhyNgSgpk9CXC1fIAiHQ
提取码:bgh3
复制这段内容后打开百度网盘手机App,操作更方便哦