dubbo整合SSM登录案例
2024-09-06 20:58:35
基于dubbo/zookeeper/SSM的分布式工程
一.项目结构
1.1公共业务
公共业务提取生产者与消费者都需要用到的实体类与业务接口,生产者与消费者只需要引入公共项目的依赖即可
********注意:实体类要实现序列化Serializable接口*******
1.2生产者
生产者只关注数据访问层和业务的具体实现
1.3消费者
消费者只关注与客户端的交互(接收请求/返回响应)
二.具体实现
2.1生产者
使用tomcat发布服务,所以创建的是webapp项目
web.xml配置中央调度器(加载配置文件)与编码过滤器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, 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>characterEncodingFilter</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>
<!--强制使用UTF-8编码-->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<!--中央调度器-->
<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:applicationContext.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>
</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="classpath: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.SqlSessionFactoryBean">
<!--加载数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载Mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--扫描Dao层-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<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,调用服务
账号密码输入正确,跳转到主页
生产者暴露服务到注册中心---->注册中心---->客户端发送请求时消费者从注册中心获取服务---->根据业务逻辑判断返回
最新文章
- [转]Android自定义控件三部曲系列完全解析(动画, 绘图, 自定义View)
- Linux教程:SSH免密码登录的方法
- Eclipse/JavaWeb (一)三大框架之struts框架 持续更新中...
- 【初学者教程】在电脑上安装Python,写第一个程序
- python 之socket 网络编程
- Codeforces #Round 376 部分题解
- 二叉树-二叉查找树-AVL树-遍历
- angular+ckeditor最后上传的最后一张图片不会被添加(bug)
- 构建基于WinRT的WP8.1 App 03:Page控件
- PHP学习系列(1)——字符串处理函数(3)
- jQuery 中的 Ajax $.ajax() load() $.get() $.post() $.getJSON() $.getScript()
- TCP发送源码学习(3)--tcp_transmit_skb
- shell利用mysql表项的icmp检测
- css和css3弹性盒模型实现元素宽度(高度)自适应
- Python成绩雷达图
- css3实现不同进度条
- 基于Ubuntu + nextCloud 搭建自己的私人网盘
- 深入了解HyperServer
- SystemTap 工作原理
- hdu 1205 吃糖果【鸽巢原理】
热门文章
- 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)
- js中(function(){})()的写法用处
- TCP/IP详解阅读记录----第二章 数据链路层
- 树莓派搭载CentOS7系统初始配置
- KVM-virsh 创建虚拟网络
- XSS之Beef的使用
- 流程图GGEditor 之 自定义节点相关属性
- python变量加逗号,的含义
- Java JDK 1.8 安装及配置
- 关于 DP 的一些内容