代码准备
本次作者建立了一个小Demo来说明如何使用Dubbo实现解耦和的项目部署方式
作者使用的开发工具是idea,大家也可以使用eclipse或者myeclipse.
首先使用idea创建一个maven项目,
其中包括了5个子模块,分别是
- dao
- daoimpl
- service
- serviceimpl
- web
项目结构如下图,请原谅画的比较…..(额..抽象)
最上面是dubbodemo的项目,旗下分为5个子模块.
他们的依赖关系分别如图所示
- daoimpl→dao
- serviceimpl→service
- serviceimpl→dao
- web→service
具体需要的jar包pom文件如下
<?xml version="1.0" encoding="UTF-8"?>
<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>
<properties>
<spring.version>4.3.9.RELEASE</spring.version> <!--自己写的 -->
<jedis.version>2.9.0</jedis.version>
<dubbo.version>2.5.5</dubbo.version>
<junit.version>4.12</junit.version>
<jackson.version>2.9.1</jackson.version>
</properties>
<groupId>com.dsj</groupId>
<artifactId>dubbodemo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dao</module>
<module>daoimpl</module>
<module>service</module>
<module>serviceimpl</module>
<module>web</module>
</modules>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Dao
首先我们来编写一下dao,比如用户使用dao提供的服务
public interface UserDao {
int add(int i,int n);
}
显而易见,我们的dao只提供了一个add,下面编写daoimpl
DaoImpl
注:所有的模块均使用maven,包括打包
接下来是daoimpl的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbodemo</artifactId>
<groupId>com.dsj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dao-impl</artifactId>
<dependencies>
<dependency>
<groupId>com.dsj</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration> <classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<!--<useUniqueVersions>false</useUniqueVersions>-->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<!-- 结合com.alibaba.dubbo.container.Main -->
<resource>
<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>spring-dao-impl.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
以及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: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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="com.dsj"/>
<!-- 开启类中dubbo的注解能力,第一个例子中没用 -->
<!--<dubbo:annotation></dubbo:annotation>-->
<dubbo:application name="dsj-dao"/>
<dubbo:registry protocol="redis" address="192.168.201.113:6379" />
<bean id="userDao" class="com.dsj.dao.UserDaoImpl"/>
<dubbo:service interface="com.dsj.dao.UserDao" ref="userDao"/>
</beans>
接下来是dao的实现类daoimpl,被调用之后会在控制台打印使用dubbo提供加法服务
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Repository;
/**
* Created by Administrator on 2017/9/20 0020.
*/
@Repository
@Service
public class UserDaoImpl implements UserDao{
public int add(int i, int n) {
System.out.println("使用dubbo提供加法服务");
return i+n;
}
}
同时 写一个运行时的类运行jar包时候自动执行该类的main方法
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Created by Administrator on 2017/9/24 0024.
*/
public class RunJar {
public static void main(String[] args) throws IOException {
if (args.length==0){
System.err.println("Useage:java jar dao.1.0-SNAPSHOT.jar spring-dao-impl.xml");
}else {
ApplicationContext ac=new ClassPathXmlApplicationContext("args[0]");
System.out.println("服务已启动,按回车停止");
System.in.read();
}
}
}
ServiceImpl
接下来service不做过多的赘述和dao相同
serviceimpl中的spring-service-impl.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"
xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.dsj"/>
<dubbo:application name="dsj-service"/>
<dubbo:registry protocol="redis" address="192.168.201.113:6379" />
<dubbo:reference id="userDao" interface="com.dsj.dao.UserDao"/>
<dubbo:protocol name="dubbo" port="20888"/>
<bean id="userService" class="com.dsj.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<dubbo:service interface="com.dsj.service.UserService" ref="userService"/>
</beans>
以及pom文件中的设置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbodemo</artifactId>
<groupId>com.dsj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-impl</artifactId>
<dependencies>
<dependency>
<groupId>com.dsj</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dsj</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dsj</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<!--<useUniqueVersions>false</useUniqueVersions>-->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<!-- 结合com.alibaba.dubbo.container.Main -->
<resource>
<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>spring-service-impl.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
当然少不了运行时的main方法
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2017/9/24 0024.
*/
public class RunJar {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-service-impl.xml");
UserService us=ac.getBean(UserService.class);
us.add(5,6);
}
}
还有serviceimp
import com.dsj.dao.UserDao;
/**
* Created by Administrator on 2017/9/20 0020.
*/
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public int add(int i, int n) {
System.out.println(String.format("开始计算%d+%d的结果,等待dubbo服务响应",i,n));
int sum=userDao.add(i,n);
System.out.println(sum);
return sum;
}
}
Web
最后一个模块web模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbodemo</artifactId>
<groupId>com.dsj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>web</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.dsj</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
spring-webmvc.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="com.dsj"/> <!--扫描包 mvc以外注解-->
<mvc:annotation-driven/> <!--mvc的注解-->
<mvc:default-servlet-handler/> <!--请求mvc以外的东西(html)能被访问-->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<dubbo:annotation/>
<dubbo:application name="dsj-web"/>
<dubbo:registry protocol="redis" address="192.168.201.113:6379" />
<dubbo:reference interface ="com.dsj.service.UserService" id ="userService"/>
</beans>
UserController
package com.dsj.controller;
import com.dsj.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2017/9/24 0024.
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/add")
@ResponseBody
public Map add(Integer i, Integer n) {
Map data = new HashMap();
int sum = userService.add(i, n);
data.put("result", sum);
return data;
}
}
webapp/WEB-INF下的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>spring-webmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-webmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-webmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
以及一个非常简单的页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../js/jquery-1.10.2.min.js"></script>
<script>
$(function () {
$('button').click(function () {
var num1=$('#num1').val();
var num2=$('#num2').val();
$.get("add",{"i":num1,"n":num2},function (data) {
$('#rs').val(data.result);
})
})
})
</script>
</head>
<body>
<input type="number" id="num1">+<input type="number" id="num2"><button>=</button><input type="number" id="rs">
</body>
</html>
项目结构如下
项目部署
首先使用idea的工具
将 daoimpl
serviceimpl
web
三个模块分别进行打包
除了web是war包意外 前两个均为jar包 我们的maven配置方式可以直接运行打包好的jar
接下来在磁盘上找到打包好的jar包
打开一个终端执行
java -jar dao-xxx.jar
serviceimpl同上
注意运行的顺序,第一个是daoimpl
然后serviceimpl
最后是tomcat
将web打包好的war包放在tomcat的webapp下 改名为ROOT
如果之前使用过tomcat需要先将ROOT文件夹删除
就像这样
接下来运行tomcat的启动脚本
作者是在windows环境下运行所以启动的是bin目录下的startup.bat
其实这三个部分可以分别放在三台不同的主机上,也就是真正实现了代码的解耦
最后部署结构如下