一、准备工作
1、下载eclipse
我用的是jetty,因为很方便,直接搜索jetty第一个安装插件即可
mybatis自动生成插件
2、下载JDK,配置环境变量
3、下载zookeeper解压并配置
4、下载mysql并解压配置
二、配置eclipse环境
1、设置maven路径
2、开始创建maven多模块
a、创建maven project
如果这个界面出现有问题或者显示很慢,就是archetype没下载下来,
创建完成后显示:
将pom.xml里的packaging的jar修改成pom(修改后才能作为parent节点)修改之后右键maven ->update project
之后创建maven module
创建之后显示:
最后新建一个webapp
最后的效果如下:
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="schedule-console" version="3.0"> <!-- 配置 Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 配置springmvc --> <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:spring-mvc.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> <!-- 字符集过滤器 --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list>index.jsp</welcome-file-list> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven /> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="org.legend" /> <mvc:resources location="/" mapping="/**/*.html" /> <mvc:resources location="/" mapping="/**/*.js" /> <mvc:resources location="/" mapping="/**/*.css" /> <mvc:resources location="/" mapping="/**/*.png" /> <mvc:resources location="/" mapping="/**/*.gif" /> <mvc:resources location="/" mapping="/**/*.jpg" /> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/" /> <property name="velocityProperties"> <props> <prop key="input.encoding">UTF-8</prop> <prop key="output.encoding">UTF-8</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> <property name="cache" value="true" /> <property name="layoutUrl" value="/common/layout.vm" /> <property name="suffix" value=".vm"></property> <property name="contentType"> <value>text/html;charset=UTF-8</value> </property> </bean> <!-- <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/legend/*"/> <bean class="com.legend.web.interceptor.UserInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> --> <!-- 定义跳转的文件的前后缀 ,视图模式配置 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> --> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean> </beans>
配置spring数据源连接池,事务spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- 扫描service、dao组件 --> <context:component-scan base-package="org.legend.*" /> <!-- 分解配置 jdbc.properites --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 数据源c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="${c3p0.pool.size.max}" /> <property name="minPoolSize" value="${c3p0.pool.size.min}" /> <property name="initialPoolSize" value="${c3p0.pool.size.ini}" /> <property name="acquireIncrement" value="${c3p0.pool.size.increment}" /> </bean> <!-- sessionFactory 将spring和mybatis整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:spring-mybatis.xml" /> <property name="mapperLocations" value="classpath*:org/legend/dal/mapper/**/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.legend.dal" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="repair" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* org.legend.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> </beans>
新建表
配置mybatis自动生成
下载mysql-connector-java-5.1.39.jar 、mybatis-generator-core-1.3.2.jar
新建generatorUser.xml文件:路径我放这:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 数据库驱动 --> <classPathEntry location="D:\.m2\repository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/legend?useSSL=false" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="org.legend.dal.module" targetProject="java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> <property name="useActualColumnNames" value="true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage="org.legend.dal.mapper" targetProject="resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="org.legend.dal.mapper" targetProject="java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 --> <table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="<span style="font-family:Arial, Helvetica, sans-serif;">true</span>" selectByExampleQueryId="true" > </table> </context> </generatorConfiguration>我设置的是
enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="<span style="font-family:Arial, Helvetica, sans-serif;">true</span>"因此看着会不太一样多了SysUserExample
最后执行结果:
接着建立service
编写test类:
页面
我还多加了个layout.vm
配置jetty:
project选择发布的modul即有web.xml的,webapp folder选择该web.xml对应的webapp,context Path 设置项目名称我随便写
记得多模块互相依赖需要引用的
<dependency> <groupId>org.legend.invocation</groupId> <artifactId>dal</artifactId> <version>${project.version}</version> </dependency>测试结果:
ok,到这里SpringMVC+mybatis集成完成,接下来集成dubbo+zookeeper
首先安装好zookeeper,很简单
然后如果有耐心直接参考dubbo官网的指南:http://dubbo.io/User+Guide-zh.htm
首先确认一下依赖是否引正确,由于dubbo一直未更新使用的是spring2.x版本,因此这里exclusions
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>
然后重点配置两个配置文件:
服务提供者配置文件和接口例子
<?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: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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="serverProducer" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://localhost:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <span style="color:#cc0000;"><dubbo:service interface="org.legend.service.Service_I" ref="service_I" /></span> <!-- 和本地bean一样实现服务 --> <span style="color:#cc0000;"><bean id="service_I" class="org.legend.service.ServiceImpl" /></span> </beans>
/** * */ package org.legend.service; /** * @author cai * */ public interface Service_I<T,M> { public T doMethod(M m); }
/** * */ package org.legend.service; import java.util.List; import org.legend.dal.module.SysUser; import org.legend.dal.module.SysUserExample; import org.springframework.beans.factory.annotation.Autowired; /** * @author cai * @param <T> * @param <M> * */ public class ServiceImpl implements Service_I<String, SysUser>{ @Autowired private SysUserService userService; @Override public String doMethod(SysUser user) { SysUserExample s = new SysUserExample(); s.createCriteria().andIdEqualTo(user.getId()); List<SysUser> lists = userService.selectByExample(s); return lists.get(0).getUserName(); } }
将spring-dubbo-producer.xml文件加到web.xml里,让其启动自动加载
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="schedule-console" version="3.0"> <!-- 配置 Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 配置springmvc --> <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:spring-mvc.xml,<span style="color:#ff0000;">classpath:spring-dubbo-producer.xml</span></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> <!-- 字符集过滤器 --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list>index.jsp</welcome-file-list> </web-app>
随便新建一个web工程或拷贝一份,配置服务消费者
<?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: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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="serviceComsumer" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry address="zookeeper://localhost:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <span style="color:#990000;"> <dubbo:reference id="service_I" interface="org.legend.web.Service_I" /></span> </beans>
服务消费者
<pre name="code" class="html">/** * */ package org.legend.service; /** * @author cai * */ public interface Service_I<T,M> { public T doMethod(M m); }
/** * */ package org.legend.dubbo.consumer; import org.legend.dal.module.SysUser; import org.legend.service.Service_I; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author cai * */ public class ComsumerMain { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "spring-dubbo-consumer.xml" }); context.start(); @SuppressWarnings("unchecked") Service_I<String, SysUser> demoService = (Service_I<String, SysUser>) context.getBean("service_I"); // 获取远程服务代理 SysUser user = new SysUser(); user.setId(1); String hello = demoService.doMethod(user); // 执行远程方法 System.out.println(hello); // 显示调用结果 } }
好了启动测试吧需要注意的是Zookeeper服务启动会占用8080端口建议在zoo.cfg文件加admin.serverPort=7999
过段时间把消息队列Kafka集成进来做分布式消息分发,然后再集成一下redis做分布式缓存和session共享,使用Spring JTA做分布式事务,最后部署在nginx上做负载均衡