項目分為三部分,這裡分為三個maven項目(基於web,所以最後一個為maven創建的web項目) 1.介面定義以及實體類定義(api+pojo) maven創建java項目,打包成jar 2.dubbo的服務提供方:定義介面實現,底層使用mybatis持久層框架,mysql資料庫 在這裡配置map ...
項目分為三部分,這裡分為三個maven項目(基於web,所以最後一個為maven創建的web項目)
1.介面定義以及實體類定義(api+pojo) --- maven創建java項目,打包成jar
2.dubbo的服務提供方:定義介面實現,底層使用mybatis持久層框架,mysql資料庫 在這裡配置mapper文件以及dubbo-provider的配置文件,還有spring和mybatis整合的文件 ---maven創建java項目,打包成jar
3.dubbo的服務消費方:定義controller類,Jsp頁面,配置springmvc的配置文件,web.xml文件,dubbo-consumer的配置文件 ---maven創建的web項目,打包成war包
4.zookeeper:dubbo的註冊中心(自己下載 ,找到bin目錄下的zkServer.cmd雙擊打開 )找不到下載地址的可以從我的github上面下載 ,這是我的整個項目鏈接,第四個zookeeper.zip就是zookeeper的包 需要項目源碼的也可以直接下載 https://github.com/DFX339/dubbo_zookeeper_SSM.git
--------------------------------------------------------------------------------------------------------
(1)**********************************資料庫(MYSQL)***************************************
創建資料庫dubbo : create dababase dubbo
在dubbo資料庫中創建user表,欄位如下:
************************************資料庫 (end) *************************************************
(2)***********************************介面設計(maven創建的java項目 打包成jar)************************************
Eclipse使用maven工具創建java項目,項目結構如下
介面maven項目的pom.xml配置如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dfx.dubbo</groupId> <artifactId>demo_interface</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo_interface</name> <url>http://maven.apache.org</url> <build> <finalName>demo_interface</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <!-- 源碼打包 <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> --> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <junit.version>4.12</junit.version> <!-- spring版本號 --> <spring.version>4.0.2.RELEASE</spring.version> <!-- mybatis版本號 --> <mybatis.version>3.2.6</mybatis.version> <!-- log4j日誌文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.12</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> </dependency> <dependency> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> <version>3.2.5.Final</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- 日誌文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> </dependencies> </project>View Code
介面類 UserIService.java如下
package com.dfx.dubbo.demo_interface.api; import com.dfx.dubbo.demo_interface.pojo.User; /** * 業務方法介面設計 * @author Administrator * */ public interface UserIService { public void add(User user); public void edit(User user); public User findById(int id); public void delete(int id); }View Code
實體類 User.java如下
package com.dfx.dubbo.demo_interface.pojo; import java.io.Serializable; /** * 實體類 * @author Administrator * */ public class User implements Serializable{ private int id; private String username; private String password; private int age; public User(){} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }View Code
************************************interface (end) *************************************************
(3)******************dubbo 服務提供方 Provider (maven創建的java項目,打包成jar)********************
服務提供方的項目目錄結構如下
服務提供者的pom.xml如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dfx.dubbo</groupId> <artifactId>demo_provider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo_provider</name> <url>http://maven.apache.org</url> <build> <finalName>demo_provider</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <!-- 源碼打包 <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> --> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <junit.version>4.12</junit.version> <!-- spring版本號 --> <spring.version>4.0.2.RELEASE</spring.version> <!-- mybatis版本號 --> <mybatis.version>3.2.6</mybatis.version> <!-- log4j日誌文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 引入介面依賴 --> <dependency> <groupId>com.dfx.dubbo</groupId> <artifactId>demo_interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 導入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 導入Mysql資料庫鏈接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- 導入dbcp的jar包,用來在applicationContext.xml中配置資料庫 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- JSTL標簽類 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 格式化對象,方便輸出日誌 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <!-- 映入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- 上傳組件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> </dependencies> </project>View Code
持久層介面 UserMapper.java設計如下
1 package com.dfx.dubbo.demo_provider.mapper; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 5 import com.dfx.dubbo.demo_interface.pojo.User; 6 7 @MapperScan 8 public interface UserMapper { 9 10 public void add(User user); 11 12 public void edit(User user); 13 14 public User findById(int id); 15 16 public void delete(int id); 17 }View Code
持久層介面的Mapper配置文件UserMapper.xml如下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dfx.dubbo.demo_provider.mapper.UserMapper"> <!-- 結果集定義 <resultMap id="userMap" type="com.dfx.dubbo.demo_provider.mapper.UserMapper"> <id column="id" property="id" /> <result column="username" property="username" /> <result column="password" property="password" /> <result column="age" property="age" /> </resultMap> --> <!-- 新增操作 --> <insert id="add" parameterType="com.dfx.dubbo.demo_interface.pojo.User" > insert into user(username,password,age) values(#{username},#{password},#{age}) </insert> <!-- 修改操作 --> <update id="edit" parameterType="com.dfx.dubbo.demo_interface.pojo.User"> update user set username=#{username},password=#{password},age=#{age} where id=#{id} </update> <!-- 刪除操作 --> <delete id="delete" parameterType="int"> delete from user where id=#{0} </delete> <!-- 查詢操作 --> <select id="findById" parameterType="int" resultType="com.dfx.dubbo.demo_interface.pojo.User"> select id,username,password,age from user where id=#{0} </select> </mapper>View Code
業務實現類UserService.java如下
package com.dfx.dubbo.demo_provider.service.impl; import javax.annotation.Resource; import com.dfx.dubbo.demo_interface.api.UserIService; import com.dfx.dubbo.demo_interface.pojo.User; import com.dfx.dubbo.demo_provider.mapper.UserMapper; /** * 業務層介面方法實現類 * @author Administrator * */ public class UserService implements UserIService { @Resource private UserMapper userDao; @Override public void add(User user) { userDao.add(user); } @Override public void edit(User user) { userDao.edit(user); } @Override public User findById(int id) { return userDao.findById(id); } @Override public void delete(int id) { userDao.delete(id); } }View Code
服務提供者開啟服務的主類 Provider.java如下
package com.dfx.dubbo.demo_provider.main; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * 開啟服務主類 * * */ public class Provider { static int count=0; public static void main(String[] args) throws IOException{ count++; System.out.println("123開始,count="+count+",執行了--"+count+"次"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContex_provider.xml"}); context.start(); System.in.read(); } }View Code
mybatis的配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 這個配置使全局的映射器啟用或禁用 緩存 -->
<setting name="cacheEnabled" value="true" />
<!-- 全局啟用或禁用延遲載入。當禁用時, 所有關聯對象都會即時載入 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 允許或不允許多種結果集從一個單獨 的語句中返回(需要適合的驅動) -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 使用列標簽代替列名。 不同的驅動在這 方便表現不同。 參考驅動文檔或充分測 試兩種方法來決定所使用的驅動 -->
<setting name="useColumnLabel" value="true" />
<!-- 允許 JDBC 支持生成的鍵。 需要適合的 驅動。 如果設置為 true 則這個設置強制 生成的鍵被使用, 儘管一些驅動拒絕兼 容但仍然有效(比如 Derby) -->
<setting name="useGeneratedKeys" value="false" />
<!-- 配置預設的執行器。SIMPLE 執行器沒 有什麼特別之處。REUSE 執行器重用 預處理語句。BATCH 執行器重用語句 和批量更新 -->
<setting name="defaultExecutorType" value="SIMPLE" />
<!-- 設置超時時間, 它決定驅動等待一個數 據庫響應的時間 -->
<setting name="defaultStatementTimeout" value="100" />
<setting name="safeRowBoundsEnabled" value="false" />
<setting name="mapUnderscoreToCamelCase" value="false" />
<setting name="localCacheScope" value="SESSION" />
<setting name="jdbcTypeForNull" value="OTHER" />
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
</settings>
</configuration>
View Code
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:context="http://www.springframework.org/schema/context" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 具體的實現bean --> <bean id="userService" class="com.dfx.dubbo.demo_provider.service.impl.UserService"></bean> <dubbo:application name="user-provider" /> <!-- 使用zookeeper廣播註冊中心暴露服務地址 --> <dubbo:registry address="localhost:2181" protocol="zookeeper"/> <!-- 用dubbo協議在20880埠暴露服務 --> <dubbo:protocol name="dubbo" port="20880" id="mydubbo"/> <dubbo:service interface="com.dfx.dubbo.demo_interface.api.UserIService" ref="userService"></dubbo:service> </beans>View Code
spring和mybatis整合的配置文件 applicationContex_provider.xml (註意:這裡Context拼寫錯誤,這邊沒改正過來 寫項目的時候註意)
<?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 自動掃描 --> <context:component-scan base-package="com.dfx.dubbo.demo_provider"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/dubbo" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/dfx/dubbo/demo_provider/mapper/UserMapper.xml"/> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> <property name="basePackage" value="com.dfx.dubbo.demo_provider.mapper; "/> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <import resource="classpath:provider.xml"/> </beans>View Code
到這裡了就可以下載zookeeper,然後打開文件夾,進入bin目錄,雙擊zkServer.cmd 啟動 (讓它一直處於啟動狀態,可以放置一邊了)
先將介面項目 demo-interface打包成jar包(打包方式:Eclipse工具 右擊項目 maven install)
再將服務提供方項目 demo_provider打包為jar包(打包方式:Eclipse工具 右擊項目 maven install)
找到demo_provider項目的Provider.java,以Java Applicaion的方式執行 執行結果如下
看到這裡,說明前面的配置都沒問題
************************************privoder (end) *************************************************
(4)***********************dubbo 服務消費方 (maven創建的web項目,打包成jwar)*************************
demo_consumer 項目架構如下
消費者的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dfx.dubbo</groupId> <artifactId>demo_consumer</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>demo_consumer Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>demo_consumer</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> <compilerArguments> <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> <!-- 源碼打包 <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> --> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <!-- servlet/jsp/EL (2.4/2.0/?)(2.5/2.1/2.1),(3.0/2.2/2.2),(3.1/2.3/3.0) --> <servlet.version>3.1.0</servlet.version> <jsp.version>2.3.1</jsp.version> <jstl.version>1.2</jstl.version> <junit.version>4.12</junit.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- 引入介面依賴 --> <dependency> <groupId>com.dfx.dubbo</groupId> <artifactId>demo_interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.dfx.dubbo</groupId> <artifactId>demo_provider</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>View Code
Controller類 UserController.java
package com.dfx.dubbo.demo_consumer.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.dfx.dubbo.demo_interface.api.UserIService; import com.dfx.dubbo.demo_interface.pojo.User; /** * 前端控制類 * @author Administrator * */ @Controller public class UserController { @Resource private UserIService userService; @RequestMapping("/add") public String add(User user){ userService.add(user); return "main"; } @RequestMapping("/delete") public String delete(int id){ userService.delete(id); return "main"; } @RequestMapping("/edit") public String edit(User user){ userService.edit(user); return "main"; } @RequestMapping("/find") public String find(int id,HttpServletRequest request){ User user = userService.findById(id); System.out.println(user+"--"+user.getPassword()+user.getUsername()+user.getAge()); request.setAttribute(" user", user); return "main"; } }View Code
配置文件 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:context="http://www.springframework.org/schema/context" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="user-consumer" /> <dubbo:registry address="zookeeper://localhost:2181" protocol="zookeeper"/> <dubbo:reference interface="com.dfx.dubbo.demo_interface.api.UserIService" id="userService" check="false" timeout="10000"/> </beans>View Code
配置文件Springmvc的配置 applicationContext_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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 --> <context:component-scan base-package="com.dfx.dubbo.demo_consumer" /> <!-- 定義跳轉的文件的前尾碼 ,視圖模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 這裡的配置我的理解是自動給後面action的方法return的字元串加上首碼和尾碼,變成一個 可用的url地址 --> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> <import resource="classpath:consumer.xml" /> </beans>View Code
配置文件 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="demo_consumer" version="3.1"> <display-name>demo_consumer</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext_consumer.xml</param-value> </context-param> <!-- 編碼過濾器 --> <filter> <filter-name>encodingFilter</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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring記憶體溢出監聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 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:applicationContext_consumer.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此處可以可以配置成*.do,對應struts的尾碼習慣 --> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>View Code
jsp頁面 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h2>Hello World! index</h2>
<a href="add.do?username='admin'&password='123'&age=22">添加</a><br>
<a href="edit.do?id=15&username='user15'&password='123'&age=20">修改</a><br>
<a href="delete.do?id=2">刪除</a><br>
<a href="find.do?id=15">查詢</a><br>
</body>
</html>
View Code
jsp頁面 main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> 《result page》<hr> <c:if test="${user != null }" > id:${user.id }<br> username:${user.username }<br> password:${user.password }<br> age:${user.age }<br> </c:if> </body> </html>View Code
配置結束,接下來就是部署項目到Tomcat上了
先將項目打包為war, Eclipse開發工具 右擊項目 run as --> maven install
運行項目: 右擊項目--》run as -->Run on Server (執行成功的前提: zkServer.cmd處於打開狀態,Provider.java處於運行狀態,demo_interface,demo_provider兩個項目都已經使用maven命令打包為jar)
運行項目的結果
點擊超鏈接 查詢 的結果(其餘增加、修改、刪除都是一樣 )
over (全劇終啦啦啦)
************************************consumer (end) *************************************************</p