dubbo+zookeeper+jsp+springmvc+spring+mybatis+…
2018-06-23 13:28:19来源:未知 阅读 ()
项目分为三部分,这里分为三个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>
接口类 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); }
实体类 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; } }
************************************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>
持久层接口 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 }
持久层接口的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>
业务实现类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); } }
服务提供者开启服务的主类 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(); } }
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>
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>
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>
到这里了就可以下载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>
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"; } }
配置文件 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>
配置文件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>
配置文件 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>
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>
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>
配置结束,接下来就是部署项目到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
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash