dubbo整合SSM登录案例

基于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,调用服务
账号密码输入正确,跳转到主页
生产者暴露服务到注册中心---->注册中心---->客户端发送请求时消费者从注册中心获取服务---->根据业务逻辑判断返回
 
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/chx9832/p/11977865.html