基于
dubbo/zookeeper/SSM的分布式工程
一.项目结构
1.1公共业务
公共业务
提取生产者与消费者都需要用到的
实体类与业务接口,生产者与消费者只需要引入公共项目的依赖即可
********注意:
实体类要实现序列化Serializable接口*******
1.2生产者
生产者只关注数据访问层和业务的具体实现
1.3消费者
消费者只关注与客户端的交互(接收请求/返回响应)
二.具体实现
2.1生产者
使用tomcat发布服务,所以创建的是webapp项目
web.xml配置中央调度器(加载
配置文件)与编码过滤器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsy
stems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<
display-name>Archetype Created Web Application</
display-name>
<filter>
<filter-name>ch
aracterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.Ch
aracterEncodingFilter</filter-class>
<!--初始化参数-->
<init-p
aram>
<p
aram-name>encoding</p
aram-name>
<p
aram-value>UTF-8</p
aram-value>
</init-p
aram>
<!--强制使用UTF-8编码-->
<init-p
aram>
<p
aram-name>forceEncoding</p
aram-name>
<p
aram-value>true</p
aram-value>
</init-p
aram>
</filter>
<filter-mapping>
<filter-name>ch
aracterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<!--中央调度器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.
dispatcherServlet</servlet-class>
<init-p
aram>
<p
aram-name>contextCon
figLocation</p
aram-name>
<p
aram-value>cla
sspath:applicationContext.xml</p
aram-value>
</init-p
aram>
<load-on-startup>1</load-on-startup>
</servlet>
<
servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</
servlet-mapping>
</web-app>
配置文件applicationContext.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: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/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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://code.alibabatech.com/schema/
dubbo http://code.alibabatech.com/schema/
dubbo/
dubbo.xsd">
<!--扫描注解-->
<context:component-scan base-package="com.dologin"/>
<!--
添加MVC
支持-->
<mvc:annotation-driven/>
<!--加载
配置文件-->
<context:
property-placeholder location="cla
sspath:jdbc.properties"/>
<!--
配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--
sqlSessionFactory-->
<bean class="org.mybatis.spring.
sqlSession
factorybean">
<!--加载数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载Mybatis
配置文件-->
<property name="con
figLocation" value="cla
sspath:mybatis-con
fig.xml"/>
</bean>
<!--扫描Dao层-->
<bean class="org.mybatis.spring.mapper.MapperScannerCon
figurer">
<property name="basePackage" value="com.dologin.dao"/>
</bean>
<!-- 提供方应用信息,用于计算依赖关系 -->
<
dubbo:application name="provider" />
<!-- 使用zookeeper
注册中心暴露服务地址 -->
<
dubbo:registry address="192.168.42.88:2181,192.168.42.89:2181,192.168.42.90:2181" protocol="zookeeper" />
<!-- 用
dubbo协议在29015端口暴露服务 -->
<
dubbo:protocol name="
dubbo" port="29015" />
<!-- 声明需要暴露的服务接口 -->
<
dubbo:service interface="com.dologin.service.IDevuserService" ref="iDevuserService" />
<!-- 具体的实现bean -->
<!-- <bean id="iDevuserService" class="com.dologin.service.impl.IDevuserServiceImpl" />-->
</beans>
主要
配置数据源,通过
dubbo暴露服务
暴露服务的地址为zookeeper集群的地址
协议端口自定
暴露服务的接口和具体的实现bean(这里暴露的服务接口要与消费者的引用一致)
2.2消费者
消费者同生产者也是web项目
web.xml同生产者
applicationContext.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: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/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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://code.alibabatech.com/schema/
dubbo http://code.alibabatech.com/schema/
dubbo/
dubbo.xsd">
<!--扫描注解-->
<context:component-scan base-package="com.dologin"/>
<!--
添加MVC
支持-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--释放静态资源-->
<mvc:default-servlet-handler/>
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<
dubbo:application name="consumer" />
<!-- 使用multicast广播
注册中心暴露发现服务地址 -->
<
dubbo:registry protocol="zookeeper" address="192.168.42.88:2181,192.168.42.89:2181,192.168.42.90:2181" />
<!--
生成远程服务代理,与生产者暴露的服务接口一致 -->
<
dubbo:reference id="iDevuserService" interface="com.dologin.service.IDevuserService" />
</beans>
配置一些前端需要的配置,视图解析器,释放静态资源等...
配置地址,发现暴露的服务
controller
登录方法
登录成功
跳转到主页,
登录失败返回
登录页面
三.执行流程
1.启动zookeeper环境
2.先启动生产者的tomcat,暴露服务
3.再启动消费者的tomcat,
调用服务
账号密码输入正确,
跳转到主页
生产者暴露服务到
注册中心---->
注册中心---->客户端发送请求时消费者从
注册中心
获取服务---->根据业务逻辑判断返回
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。