使用Maven完成SSM实现RabbitMQ+Dubbo多对多案例,本例设计两个程序,一个程序为Web程序,另一个是Dubbo服务,Dubbo服务程序实现数据库存取操作,Web程序只包括Controller层和发送消息和接收消息业务处理,Controller需要远程注入Dubbo服务实现操作数据库业务。
案例要求:
1、Zookeeper服务
2、RabbitMQ服务
3、Dubbo控制台,是Web程序
系统结构图
Dubbo服务程序
是Maven项目,负责操作数据库存储
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Proj_ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Proj_ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.5.RELEASE</spring.version>
<mybatis.version>3.2.7</mybatis.version>
<mybatis.spring.version>1.2.4</mybatis.spring.version>
<mysql.version>5.1.8</mysql.version>
<slf4j.version>1.7.12</slf4j.version>
<druid.version>1.0.29</druid.version>
<commons-lang3.version>3.4</commons-lang3.version>
<commons-io.version>2.5</commons-io.version>
<commons-codec.version>1.6</commons-codec.version>
<jackson.version>2.5.1</jackson.version>
<fastjson.version>1.2.32</fastjson.version>
<spring.redis.version>1.7.2.RELEASE</spring.redis.version>
<mapper.version>3.4.5</mapper.version>
<pagehelper.version>4.1.6</pagehelper.version>
<amqp-client.version>3.6.5</amqp-client.version>
<cglib-full.version>2.0.2</cglib-full.version>
<dubbo.version>2.5.8</dubbo.version>
<freemarker.version>2.3.23</freemarker.version>
<spring.rabbit.version>1.6.6.RELEASE</spring.rabbit.version>
<zookeeper.version>3.3.1</zookeeper.version>
<zkclient.version>0.4</zkclient.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</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>
<!-- mybatis插件 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>${amqp-client.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-full</artifactId>
<version>${cglib-full.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${spring.rabbit.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>hr</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<extdirs>lib</extdirs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>6060</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Dubbo服务的Spring配置文件
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Proj_ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Proj_ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.5.RELEASE</spring.version>
<mybatis.version>3.2.7</mybatis.version>
<mybatis.spring.version>1.2.4</mybatis.spring.version>
<mysql.version>5.1.8</mysql.version>
<slf4j.version>1.7.12</slf4j.version>
<druid.version>1.0.29</druid.version>
<commons-lang3.version>3.4</commons-lang3.version>
<commons-io.version>2.5</commons-io.version>
<commons-codec.version>1.6</commons-codec.version>
<jackson.version>2.5.1</jackson.version>
<fastjson.version>1.2.32</fastjson.version>
<spring.redis.version>1.7.2.RELEASE</spring.redis.version>
<mapper.version>3.4.5</mapper.version>
<pagehelper.version>4.1.6</pagehelper.version>
<amqp-client.version>3.6.5</amqp-client.version>
<cglib-full.version>2.0.2</cglib-full.version>
<dubbo.version>2.5.8</dubbo.version>
<freemarker.version>2.3.23</freemarker.version>
<spring.rabbit.version>1.6.6.RELEASE</spring.rabbit.version>
<zookeeper.version>3.3.1</zookeeper.version>
<zkclient.version>0.4</zkclient.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</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>
<!-- mybatis插件 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>${amqp-client.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-full</artifactId>
<version>${cglib-full.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${spring.rabbit.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>hr</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<extdirs>lib</extdirs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>6060</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Mybatis配置文件
<?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>
<settings>
<!-- 打印查询语句-->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<mappers>
<mapper resource="com/test/mapper/student.xml"/>
<!--
<package name="com.test.hr"/>
<package name="com.test.o2m"/>
<mapper resource="user.xml"/>
<mapper resource="com/test/hr/UserDao.xml"/>
<mapper class="com.test.hr.UserDao"/>
-->
</mappers>
</configuration>
JDBC配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/w1?useUnicode=true&characterEncoding=utf8
jdbc.uname=root
jdbc.pwd=root
Mybatis Mapping.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.test.mapper.StuMapper">
<select id="findStu" resultType="com.test.bean.StudentInfo">
select a.*,b.hid,b.hname from t_student a left join
(select sid,group_concat(h.id) as hid, group_concat(h.name) as hname
from t_s2h s2h,t_hobby h where s2h.hid=h.id group by s2h.sid) b
on a.id=b.sid
</select>
<insert id="saveStu" parameterType="com.test.bean.StudentInfo"
useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into t_student(name,grade) values(#{name},#{grade})
</insert>
<select id="findHobby" resultType="com.test.bean.HobbyInfo">
select * from t_hobby
</select>
<select id="findStuById" resultType="com.test.bean.StudentInfo">
select a.*,b.hid,b.hname from t_student a left join
(select sid,group_concat(h.id) as hid, group_concat(h.name) as hname
from t_s2h s2h,t_hobby h where s2h.hid=h.id group by s2h.sid) b
on a.id=b.sid where a.id=#{id}
</select>
<insert id="saveS2H" parameterType="com.test.bean.S2HInfo">
insert into t_s2h(sid,hid) values(#{sid},#{hid})
</insert>
</mapper>
SQL文件
create table t_student(id int auto_increment primary key,
name varchar(50),grade varchar(30));
create table t_hobby(id int auto_increment primary key,
name varchar(50));
create table t_s2h(sid int,hid int);
insert into t_hobby(name) values ('音乐');
insert into t_hobby(name) values ('体育');
insert into t_hobby(name) values ('美术');
insert into t_student(name,grade) values('学生A','一');
insert into t_student(name,grade) values('学生B','二');
insert into t_student(name,grade) values('学生C','三');
insert into t_s2h(sid,hid) values(1,1);
insert into t_s2h(sid,hid) values(1,2);
insert into t_s2h(sid,hid) values(2,2);
insert into t_s2h(sid,hid) values(2,3);
select * from t_s2h s2h,t_hobby h where s2h.hid=h.id
select sid,group_concat(h.id) as hid, group_concat(h.name) as hname
from t_s2h s2h,t_hobby h where s2h.hid=h.id group by s2h.sid;
内关联
select a.* from t_student a,
(select sid,group_concat(h.id) as hid, group_concat(h.name) as hname
from t_s2h s2h,t_hobby h where s2h.hid=h.id group by s2h.sid) b
where a.id=b.sid
左外关联
select a.*,b.hid,b.hname from t_student a left join
(select sid,group_concat(h.id) as hid, group_concat(h.name) as hname
from t_s2h s2h,t_hobby h where s2h.hid=h.id group by s2h.sid) b
on a.id=b.sid;
Mybatis Mapper接口定义
package com.test.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.test.bean.HobbyInfo;
import com.test.bean.S2HInfo;
import com.test.bean.StudentInfo;
public interface StuMapper {
public List<StudentInfo> findStu();
public StudentInfo findStuById(@Param("id") Integer id);
public List<HobbyInfo> findHobby();
public void saveStu(StudentInfo si);
public void saveS2H(S2HInfo s2h);
}
学生业务服务接口定义
package com.test.service;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.test.bean.HobbyInfo;
import com.test.bean.S2HInfo;
import com.test.bean.StudentInfo;
import com.test.bean.StudentPage;
public interface IStudentService {
public List<StudentInfo> findStu();
public StudentPage findPageStu(Integer page,Integer row);
public StudentInfo findStuById(Integer id);
public List<HobbyInfo> findHobby();
public StudentInfo saveStu(StudentInfo si);
public void saveS2H(S2HInfo s2h);
}
学生业务服务实现类定义
package com.test.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.bean.HobbyInfo;
import com.test.bean.S2HInfo;
import com.test.bean.StudentInfo;
import com.test.bean.StudentPage;
import com.test.mapper.StuMapper;
import com.test.service.IStudentService;
public class StudentServiceImpl implements IStudentService{
@Autowired
private StuMapper mapper;
//检索全部的学生
@Override
public List<StudentInfo> findStu() {
return mapper.findStu();
}
//根据学生ID查询学生对象,
@Override
public StudentInfo findStuById(Integer id) {
return mapper.findStuById(id);
}
//检索全部的爱好
@Override
public List<HobbyInfo> findHobby() {
return mapper.findHobby();
}
//保存学生对象到数据库
@Override
public StudentInfo saveStu(StudentInfo si) {
try
{
mapper.saveStu(si);
}
catch(Exception e)
{
e.printStackTrace();
}
return si;
}
//分页查询学生对象
@Override
public StudentPage findPageStu(Integer page, Integer row) {
PageHelper.startPage(page,row);
List<StudentInfo> lst = mapper.findStu();
PageInfo pi = new PageInfo(lst);
List<StudentInfo> lst2 = pi.getList();
Integer total = new Long(pi.getTotal()).intValue();
StudentPage spage = new StudentPage();
spage.setList(lst2);
spage.setTotal(total);
return spage;
}
@Override
public void saveS2H(S2HInfo s2h) {
mapper.saveS2H(s2h);
}
}
学生实体类
package com.test.bean;
import java.io.Serializable;
public class StudentInfo implements Serializable{
private Integer id = null;
private String name = null;
private String grade = null;
private String hname = null;//不同兴趣以逗号分隔
private String hid = null;//不同兴趣ID以逗号分隔
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getHname() {
return hname;
}
public void setHname(String hname) {
this.hname = hname;
}
public String getHid() {
return hid;
}
public void setHid(String hid) {
this.hid = hid;
}
}
兴趣爱好实体类
package com.test.bean;
import java.io.Serializable;
public class HobbyInfo implements Serializable{
private Integer id = null;
private String name = null;
private String checked = "";
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getChecked() {
return checked;
}
public void setChecked(String checked) {
this.checked = checked;
}
}
中间表实体类
package com.test.bean;
import java.io.Serializable;
public class S2HInfo implements Serializable{
private Integer sid = null;
private Integer hid = null;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public Integer getHid() {
return hid;
}
public void setHid(Integer hid) {
this.hid = hid;
}
}
分页显示学生数据结构定义
package com.test.bean;
import java.io.Serializable;
import java.util.List;
public class StudentPage implements Serializable{
private List<StudentInfo> list = null;
private Integer total = null;
public List<StudentInfo> getList() {
return list;
}
public void setList(List<StudentInfo> list) {
this.list = list;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
Dubbo启动类
package com.test.util;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboStarter {
public static void main(String[] args) {
try
{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("dubbo_service.xml");
ctx.start();
System.out.println("Dubbo服务启动");
System.in.read();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
启动Dubbo服务
Web系统
Web系统作为Dubbo消费者角色,通过注册中心Zookeeper远程注入Dubbo服务,实现对数据库的存取。
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Proj_ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Proj_ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.5.RELEASE</spring.version>
<mybatis.version>3.2.7</mybatis.version>
<mybatis.spring.version>1.2.4</mybatis.spring.version>
<mysql.version>5.1.8</mysql.version>
<slf4j.version>1.7.12</slf4j.version>
<druid.version>1.0.29</druid.version>
<commons-lang3.version>3.4</commons-lang3.version>
<commons-io.version>2.5</commons-io.version>
<commons-codec.version>1.6</commons-codec.version>
<jackson.version>2.5.1</jackson.version>
<fastjson.version>1.2.32</fastjson.version>
<spring.redis.version>1.7.2.RELEASE</spring.redis.version>
<mapper.version>3.4.5</mapper.version>
<pagehelper.version>4.1.6</pagehelper.version>
<amqp-client.version>3.6.5</amqp-client.version>
<cglib-full.version>2.0.2</cglib-full.version>
<dubbo.version>2.5.8</dubbo.version>
<freemarker.version>2.3.23</freemarker.version>
<spring.rabbit.version>1.6.6.RELEASE</spring.rabbit.version>
<zookeeper.version>3.3.1</zookeeper.version>
<zkclient.version>0.4</zkclient.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</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>
<!-- mybatis插件 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>${amqp-client.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-full</artifactId>
<version>${cglib-full.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${spring.rabbit.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>hr</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<extdirs>lib</extdirs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>6060</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
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/context
http://www.springframework.org/schema/context/spring-context.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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="student_customer"/>
<dubbo:registry address="zookeeper://192.168.137.1:2181"/>
<dubbo:reference id="studentServ" interface="com.test.service.IStudentService"/>
<!-- 扫描service -->
<context:component-scan base-package="com.test.rabbitmq"></context:component-scan>
<import resource="classpath*:/springrabbitmq.xml"/>
<import resource="classpath*:/springmvc.xml"/>
</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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- 扫描控制器 -->
<context:component-scan base-package="com.test.ctrl"></context:component-scan>
<!-- 视图解析器 -->
<!-- jsp视图解析器 -->
<!-- 配置InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 页面: templateLoaderPath + prefix + 视图名 + suffix -->
<!-- 模板位置 -->
<!-- <bean
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
模板包位置
<property name="templateLoaderPath" value="/WEB-INF"></property>
<property name="defaultEncoding" value="UTF-8"></property>
设置时间
<property name="freemarkerSettings">
<props>
<prop key="number_format">#.##</prop>
</props>
</property>
</bean>
视图解析器
<bean
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value="/ftl/"></property>
<property name="suffix" value=".ftl"></property>
UTF-8需要大写
<property name="contentType" value="text/html;charSet=UTF-8"></property>
<property name="requestContextAttribute" value="request"></property>
</bean> -->
<!-- 文件上传处理器,id是固定的 -->
<!-- Ctrl + Shift + T -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<!-- 注解驱动,静态资源 -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
</beans>
Rabbitmq与Spring集成配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<rabbit:connection-factory id="connectionFactory"
host="127.0.0.1" username="guest" password="guest" port="5672"
virtual-host="/"
/>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="springrabbit_queue" durable="false"
auto-delete="false" exclusive="false" />
<rabbit:direct-exchange name="springrabbit_exg"
durable="false" auto-delete="false">
<rabbit:bindings>
<!-- key Binding Key -->
<rabbit:binding queue="springrabbit_queue" key="direct" />
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:template id="directTemplate" exchange="springrabbit_exg"
connection-factory="connectionFactory" message-converter="serializerMessageConverter" />
<!--
<bean id="sendMsg" class="com.test.rabbitmq.SendMsg">
<property name="directTemplate" ref="directTemplate" />
</bean>
<bean id="receiveMsg" class="com.test.rabbitmq.ReceiveMsg">
</bean>
-->
<bean id="serializerMessageConverter"
class="org.springframework.amqp.support.converter.SerializerMessageConverter" />
<rabbit:listener-container
connection-factory="connectionFactory" acknowledge="manual"
prefetch="1">
<rabbit:listener queues="springrabbit_queue" ref="receiveMsg" />
</rabbit:listener-container>
</beans>
Dubbo服务端的部分代码需要以源码方式引入,或者以Jar包方式引入
Controller
package com.test.ctrl;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.bean.HobbyInfo;
import com.test.bean.StudentInfo;
import com.test.bean.StudentPage;
import com.test.rabbitmq.SendMsg;
import com.test.service.IStudentService;
import com.test.util.PageUtil;
@Controller
public class StudentCtrl {
@Autowired
private IStudentService serv;
@Autowired
private SendMsg sendmsg;
@RequestMapping("/list")
public String list(HttpServletRequest req,Integer page,Integer rows)
{
if(page == null)
page = 1;
if(rows == null)
rows = 4;
StudentPage sp = serv.findPageStu(page, rows);
List<StudentInfo> students = sp.getList();
Integer total = sp.getTotal();
PageUtil pu = new PageUtil("/list",page,rows,total.longValue());
String pageDiv = pu.toHtml();
req.setAttribute("students", students);
req.setAttribute("pageDiv", pageDiv);
return "student";
}
@ResponseBody
@RequestMapping("/save")
public boolean save(HttpServletRequest req,StudentInfo si)
{
System.out.println("Controller hid === "+si.getHid());
String exg = "springrabbit_exg";
String key = "direct";
sendmsg.sendObject(exg, key, si);
return true;
}
@RequestMapping("/add")
public String add(HttpServletRequest req)
{
List<HobbyInfo> hobby = serv.findHobby();
req.setAttribute("hobby", hobby);
return "add";
}
@RequestMapping("/view")
public String view(HttpServletRequest req,Integer id)
{
StudentInfo si = serv.findStuById(id);
String hid = si.getHid();
System.out.println("hid==="+hid);
List<HobbyInfo> hobby = serv.findHobby();
for(HobbyInfo hi:hobby)
{
if(hid.indexOf(hi.getId().toString())>=0)
{
hi.setChecked("checked");
}
}
req.setAttribute("hobby", hobby);
req.setAttribute("stu", si);
req.setAttribute("view", "true");
return "add";
}
}
消息发送类
package com.test.rabbitmq;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SendMsg {
@Autowired
private RabbitTemplate directTemplate;
public RabbitTemplate getDirectTemplate() {
return directTemplate;
}
public void setDirectTemplate(RabbitTemplate directTemplate) {
this.directTemplate = directTemplate;
}
public void sendObject(String exg, String key,Object obj)
{
byte[] data = toByte(obj);
Message msg = new Message(data, new MessageProperties());
directTemplate.send(exg, key, msg);
}
public byte[] toByte(Object obj)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
}
catch(Exception e)
{
e.printStackTrace();
}
return baos.toByteArray();
}
}
消息接收类
package com.test.rabbitmq;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rabbitmq.client.Channel;
import com.test.bean.S2HInfo;
import com.test.bean.StudentInfo;
import com.test.service.IStudentService;
@Service
public class ReceiveMsg implements ChannelAwareMessageListener{
@Autowired
private IStudentService serv;
@Override
public void onMessage(Message message, Channel channel) throws Exception {
try
{
byte[] b = message.getBody();
Object obj = toObject(b);
if(obj != null)
{
if(obj instanceof StudentInfo)
{
StudentInfo si = (StudentInfo)obj;
System.out.println("Receive Object = "+si);
System.out.println("Receive hid = "+si.getHid());
String hid = si.getHid();
si = serv.saveStu(si);
Integer sid = si.getId();
System.out.println("dubbo sid.id="+sid);
if(hid != null)
{
String[] dim = hid.split(",");
for(String hid2:dim)
{
S2HInfo s2h = new S2HInfo();
s2h.setHid(Integer.parseInt(hid2));
s2h.setSid(sid);
serv.saveS2H(s2h);
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
long tag = message.getMessageProperties().getDeliveryTag();
channel.basicAck(tag,false);
}
}
public Object toObject(byte[] data)
{
ByteArrayInputStream bais = new ByteArrayInputStream(data);
try
{
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
页面分页工具类
package com.test.util;
public class PageUtil {
private Integer page = 1;//默认显示第一页
private Integer rows = 4;//每页显示记录数
private Long total = null;//总行数
private String url = null;//点击页码跳转url
public PageUtil(String url,Integer page,Integer rows,Long total)
{
this.url = url;
this.page = page;
this.rows = rows;
this.total = total;
}
public String toHtml()
{
StringBuffer sb = new StringBuffer();
//计算总页数
int pages = 0;
if(total % rows == 0)
pages = total.intValue() / rows;
else
pages = (total.intValue() / rows) + 1;
sb.append("<div id='pagediv'>\r\n");
String firstUrl = null;
if(url.indexOf("?")>0)
firstUrl = url + "&page=1&rows="+rows;
else
firstUrl = url + "?page=1&rows="+rows;
sb.append("<a href='"+firstUrl+"'>首页</a>\r\n");
String backUrl = null;
if(url.indexOf("?")>0)
backUrl = url + "&page="+(page==1?1:(page-1))+"&rows="+rows;
else
backUrl = url + "?page="+(page==1?1:(page-1))+"&rows="+rows;
sb.append("<a href='"+backUrl+"'>上一页</a>\r\n");
for(int i=1;i<=pages;i++)
{
String pageUrl = null;
if(url.indexOf("?")>0)
pageUrl = url + "&page="+i+"&rows="+rows;
else
pageUrl = url + "?page="+i+"&rows="+rows;
if(i == page)
sb.append("<a href='"+pageUrl+"'><b><font color='red'>"+i+"</font></b></a>\r\n");
else
sb.append("<a href='"+pageUrl+"'>"+i+"</a>\r\n");
}
String nextUrl = null;
if(url.indexOf("?")>0)
nextUrl = url + "&page="+(page==pages?pages:(page+1))+"&rows="+rows;
else
nextUrl = url + "?page="+(page==pages?pages:(page+1))+"&rows="+rows;
sb.append("<a href='"+nextUrl+"'>下一页</a>\r\n");
String lastUrl = null;
if(url.indexOf("?")>0)
lastUrl = url + "&page="+pages+"&rows="+rows;
else
lastUrl = url + "?page="+pages+"&rows="+rows;
sb.append("<a href='"+lastUrl+"'>尾页</a>\r\n");
sb.append(" 第"+page+"/"+pages+"页\r\n");
sb.append(" 共"+total+"条记录\r\n");
sb.append("</div>\r\n");
return sb.toString();
}
}
效果图