SSM+dubbo+zookeeper实现基本的增删改查

Posted hanzx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM+dubbo+zookeeper实现基本的增删改查相关的知识,希望对你有一定的参考价值。

前言

本文中使用的项目是由上一篇文章中的项目改造而来。具体来说,就是引入了dubbo和zookeeper,并将Controller层与service层dao层进行了拆分,使双方通过service接口远程调用的形式,再次实现了基本的增删改查。

上一篇文章:http://www.cnblogs.com/hanzx/p/10016468.html

 

名词解释

dubbo:dubbo是阿里开源的一款优秀的java RPC框架,可以配合spring和zookeeper使用。它提供了这些功能:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

zookeeper:zookeeper 是由雅虎创建的一个开源的分布式协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它提供了这些功能:配置维护、域名服务、分布式同步、组服务等。

 

zookeeper的windows版安装方法

https://blog.csdn.net/weixin_37715446/article/details/78642052

 

程序结构

interface:

provider:

consumer:

 

程序源码

interface

pom.xml

没添加什么东西,所以不贴出来了

 

UserModel

 1 package org.hanzx.model;
 2 
 3 
 4 import java.io.Serializable;
 5 
 6 public class UserModel implements Serializable{
 7 
 8     private Integer id;
 9 
10     private String name;
11 
12     private Integer age;
13 
14     private String password;
15 
16     public Integer getId() {
17         return id;
18     }
19 
20     public void setId(Integer id) {
21         this.id = id;
22     }
23 
24     public String getName() {
25         return name;
26     }
27 
28     public void setName(String name) {
29         this.name = name;
30     }
31 
32     public Integer getAge() {
33         return age;
34     }
35 
36     public void setAge(Integer age) {
37         this.age = age;
38     }
39 
40     public String getPassword() {
41         return password;
42     }
43 
44     public void setPassword(String password) {
45         this.password = password;
46     }
47 }
View Code

 

UserService

 1 package org.hanzx.service;
 2 
 3 
 4 import org.hanzx.model.UserModel;
 5 
 6 import java.util.List;
 7 
 8 public interface UserService {
 9 
10     List<UserModel> getAllUser();
11 
12     void addUser(UserModel userModel);
13 
14     UserModel getUserById(Integer id);
15 
16     void updateUser(UserModel userModel);
17 
18     void deleteUser(Integer[] ids);
19 }
View Code

 

provider

pom.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <parent>
  6         <artifactId>web_ssm_dubbo</artifactId>
  7         <groupId>org.hanzx</groupId>
  8         <version>1.0-SNAPSHOT</version>
  9     </parent>
 10     <modelVersion>4.0.0</modelVersion>
 11 
 12     <artifactId>provider</artifactId>
 13 
 14     <properties>
 15         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 16         <spring.version>4.2.5.RELEASE</spring.version>
 17         <mybatis.version>3.2.1</mybatis.version>
 18         <slf4j.version>1.6.6</slf4j.version>
 19         <log4j.version>1.2.12</log4j.version>
 20         <mysql.version>5.1.35</mysql.version>
 21     </properties>
 22 
 23     <dependencies>
 24         <dependency>
 25             <groupId>org.hanzx</groupId>
 26             <artifactId>interface</artifactId>
 27             <version>1.0-SNAPSHOT</version>
 28         </dependency>
 29 
 30         <dependency>
 31             <groupId>junit</groupId>
 32             <artifactId>junit</artifactId>
 33             <version>4.11</version>
 34             <scope>test</scope>
 35         </dependency>
 36         <!-- 添加Spring依赖 -->
 37         <dependency>
 38             <groupId>org.springframework</groupId>
 39             <artifactId>spring-core</artifactId>
 40             <version>${spring.version}</version>
 41         </dependency>
 42         <dependency>
 43             <groupId>org.springframework</groupId>
 44             <artifactId>spring-context</artifactId>
 45             <version>${spring.version}</version>
 46         </dependency>
 47         <dependency>
 48             <groupId>org.springframework</groupId>
 49             <artifactId>spring-context-support</artifactId>
 50             <version>${spring.version}</version>
 51         </dependency>
 52         <dependency>
 53             <groupId>org.springframework</groupId>
 54             <artifactId>spring-aop</artifactId>
 55             <version>${spring.version}</version>
 56         </dependency>
 57         <dependency>
 58             <groupId>org.springframework</groupId>
 59             <artifactId>spring-aspects</artifactId>
 60             <version>${spring.version}</version>
 61         </dependency>
 62         <dependency>
 63             <groupId>org.springframework</groupId>
 64             <artifactId>spring-tx</artifactId>
 65             <version>${spring.version}</version>
 66         </dependency>
 67         <dependency>
 68             <groupId>org.springframework</groupId>
 69             <artifactId>spring-jdbc</artifactId>
 70             <version>${spring.version}</version>
 71         </dependency>
 72         <!--spring单元测试依赖 -->
 73         <dependency>
 74             <groupId>org.springframework</groupId>
 75             <artifactId>spring-test</artifactId>
 76             <version>${spring.version}</version>
 77             <scope>test</scope>
 78         </dependency>
 79         <!-- mysql驱动包 -->
 80         <dependency>
 81             <groupId>mysql</groupId>
 82             <artifactId>mysql-connector-java</artifactId>
 83             <version>${mysql.version}</version>
 84         </dependency>
 85 
 86         <!-- alibaba data source 相关jar包-->
 87         <dependency>
 88             <groupId>com.alibaba</groupId>
 89             <artifactId>druid</artifactId>
 90             <version>0.2.23</version>
 91         </dependency>
 92         <!-- logback start -->
 93         <dependency>
 94             <groupId>log4j</groupId>
 95             <artifactId>log4j</artifactId>
 96             <version>${log4j.version}</version>
 97         </dependency>
 98         <dependency>
 99             <groupId>org.slf4j</groupId>
100             <artifactId>slf4j-api</artifactId>
101             <version>${slf4j.version}</version>
102         </dependency>
103         <dependency>
104             <groupId>ch.qos.logback</groupId>
105             <artifactId>logback-classic</artifactId>
106             <version>1.1.2</version>
107         </dependency>
108         <dependency>
109             <groupId>ch.qos.logback</groupId>
110             <artifactId>logback-core</artifactId>
111             <version>1.1.2</version>
112         </dependency>
113         <dependency>
114             <groupId>org.logback-extensions</groupId>
115             <artifactId>logback-ext-spring</artifactId>
116             <version>0.1.1</version>
117         </dependency>
118 
119         <!--mybatis依赖 -->
120         <dependency>
121             <groupId>org.mybatis</groupId>
122             <artifactId>mybatis</artifactId>
123             <version>${mybatis.version}</version>
124         </dependency>
125 
126         <!-- mybatis/spring包 -->
127         <dependency>
128             <groupId>org.mybatis</groupId>
129             <artifactId>mybatis-spring</artifactId>
130             <version>1.2.0</version>
131         </dependency>
132 
133         <!--dubbo-->
134         <dependency>
135             <groupId>com.alibaba</groupId>
136             <artifactId>dubbo</artifactId>
137             <version>2.5.7</version>
138         </dependency>
139         <dependency>
140             <groupId>org.apache.zookeeper</groupId>
141             <artifactId>zookeeper</artifactId>
142             <version>3.5.4-beta</version>
143             <type>pom</type>
144         </dependency>
145         <dependency>
146             <groupId>com.101tec</groupId>
147             <artifactId>zkclient</artifactId>
148             <version>0.10</version>
149         </dependency>
150         <dependency>
151             <groupId>org.apache.curator</groupId>
152             <artifactId>curator-framework</artifactId>
153             <version>2.7.1</version>
154         </dependency>
155     </dependencies>
156 
157 
158 </project>
View Code

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        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">
 6 
 7     <!-- 1.配置jdbc文件 -->
 8     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 9         <property name="locations" value="classpath:jdbc.properties"/>
10     </bean>
11     <!-- 2.扫描的包路径 --><!--使用<context:component-scan/> 可以不再配置<context:annotation-config/> -->
12     <context:component-scan base-package="org.hanzx"/>
13     <!--6 容器自动扫描IOC组件  -->
14     <!--<context:component-scan base-package="org.hanzx.model"/>-->
15     <import resource="spring-mybatis.xml"/>
16     <import resource="spring-dubbo.xml"/>
17 </beans>
View Code

jdbc.properties