您的位置:首页 > 编程语言 > Java开发

maven搭建ssm分模块框架+dubbo (myeclipse版) (三)

2016-05-23 22:36 711 查看
前面已经把ssm的框架搭好了,在第二篇的地址可以下载到源码,博客中也有搭建ssm相关的教程地址,相信大家可以搭建起来的

接下来就把dubbo引入进去了

关于dubbo的一些概念性的东西 请大家自行百度 或google一下吧

官方的地址是 dubbo.io

我这边使用的是官方推荐的zookeeper来 调度dubbo服务的 zookeeper和dubbo一样是分布式模式的

有四种注册中心模式



接下来直接进入主题了

首先需要下载一个zookeeper

官网下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/

下载好解压之后,在conf目录下 将zoo_sample.cfg改名为zoo.cfg

里面的内容我没有改,如果要改的话可以参考:

/article/4740088.html

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=D:\\zookeeper-3.4.5\\data
dataLogDir=D:\\zookeeper-3.4.5\\log
# the port at which the clients will connect
clientPort=2181


然后就是 pom.xml引入的jar包

<!-- dubbo start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>netty</artifactId>
<groupId>org.jboss.netty</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- dubbo end -->


jar包引入之后,就是服务消费者 和 提供者文件配置了,我之前 搭好的ssm框架中 service 是用来提供 也就是用来暴露服务的那层

而web 是用来消费 也就是引用服务的那层

web 和 service 都在pom中引用了interface层,当dubbo使用起来之后,web层就只需要Interface层的jar包 就可以调用service 提供的服务了,也算是达到了解耦 ,看起来web层跟service是没有任何的联系

interface层 只是接口

public interface DubboDao {
public void dubboTest();
}


service层 服务提供者配置文件dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系  -->
<dubbo:application name="ssmDemo-service" />
<!-- 使用zookeeper注册中心暴露服务地址   -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- dubbo发布服务  配置信息表 -->
<dubbo:service interface="cn.com.test.dao.DubboDao" ref="DubboService"/>
</beans>


对于这个端口,是使用zookeeper默认的2181端口

<dubbo:registry address="zookeeper://localhost:2181"/>


dubbo的端口也是使用默认的

<dubbo:protocol name="dubbo" port="20880" />


服务的暴露,interface的值是interface层接口的路径,ref的值则是service层实现 @Service() 注解设定的名称

<dubbo:service interface="cn.com.test.dao.DubboDao" ref="DubboService"/>


=========================================

DubboServiceImpl.java

@Service("DubboService")
public class DubboServiceImpl implements DubboDao{

@Override
public void dubboTest() {
System.out.println("web层引用service服务成功");
}

}


service层配置好之后,我们要怎么让不是web工程的service工程读取配置文件呢,这里是直接用了一个main方法来加载这些配置文件

main.java

public class TestMain {

public static void main(String[] args){
String[] xmls=new String[]{"classpath:spring-mybatis.xml","classpath:dubbo-provider.xml"};
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(xmls);
context.start();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
} // 按任意键退出
}
}


web层 服务消费者配置dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系  -->
<dubbo:application name="ssmDemo-web" />
<!-- 使用zookeeper注册中心暴露服务地址   -->
<dubbo:registry address="zookeeper://localhost:2181" />
<!-- 引用service-authority的远程服务   接口鉴权服务 -->
<dubbo:reference id="DubboDao" interface="cn.com.test.dao.DubboDao" check="false" />
</beans>


对于 id的名称没有特定的要求,可以跟接口的名称一样,interface的值也就是接口的路径,check=false是在工程启动的时候不会去检查 对应的服务是否已经注册到注册中心,不会报该服务没有被提供的异常错误

<dubbo:reference id="DubboDao" interface="cn.com.test.dao.DubboDao" check="false" />


之后要让容器加载dubbo-consumer.xml文件,我是放在web.xml中,大家也可以考虑把它在applicationContext.xml中使用Import resource引入进去。

web.xml

<!-- Spring MVC servlet -->
<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:spring-mvc.xml,classpath:dubbo-consumer.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>


PS:之前提供的ssm框架 web层的pom中忘记引入interface层了,这里补上

<dependency>
<groupId>cn.com.test</groupId>
<artifactId>ssmDemo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>


接下来要做的就是先将zookeeper跑起来,在zookeeper的目录bin下,启动zkServer.cmd

然后将service的main方法跑起来

然后把web层加入到tomcat 跑起来

最后访问页面 localhost:8080/test/dubbo

TestMain打印的信息



web层controller打印信息



zookeeper的日志,我是把服务停止,但是可以看出之前服务是成功注册到zookeeper上了



还有一个小问题

运行的时候可能会遇到解析不了XML文件的错误SAXException:

在maven的本地仓库下找到maven->com->alibaba->dubbo->2.5.3->dubbo-2.5.3.jar (具体的看自己的本地仓库) 解压它获得dubbo.xsd文件

然后打开Eclipse:1. Window->Preferences->XML->XML Catalog->User Specified Entries窗口中,选择Add 按纽



还有dubbo还提供了一个服务管理 平台 可以查看消费者和提供者的使用情况

我这里没有做这个…有点懒了

可以参考下面的地址 下载 并配置下

http://www.mamicode.com/info-detail-168312.html

http://my.oschina.net/ihanfeng/blog/525262?p={{totalPage}}

顺便再说一下,下载的ssm框架 使用的话需要 clean install一下

代码下载地址:

http://download.csdn.net/detail/i_popular/9528986
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: