分布式–案例(上)
应该有小伙伴对此有一定的了解,我在此讲解初级案例。
案例下载地址:分布式
互联网项目应用分布式框架的好处在于:
1.可以将分布在各处的资源综合利用。而这种利用对用户而言是透明的。
2.可以将负载由单个节点转移到多个,从而提高效率。
3.分布式技术可以避免由于单个节点失效而使整个系统崩溃的危险
资料来源——[ 百度]
前提条件:你能用熟练使用maven搭建SSH、SSM之类的框架!
(最好strut、spring、mybatis、hibernate随意组合,楼主我只教其中一种,其余的组合小伙伴有兴趣可以自己研究)
1、首先搭建maven+springmvc+spring3+mybatis框架
2、修改fenbushi_servicedao的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">
<parent>
<artifactId>app-core</artifactId>
<groupId>com.fangjian.framework</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dongjun.platform</groupId>
<artifactId>fenbushi_servicedao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fenbushi_servicedao</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 引入PO依赖 -->
<dependency>
<groupId>com.dongjun.platform</groupId>
<artifactId>fenbushi_po</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
3、修改fenbushi_demoweb的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">
<parent>
<artifactId>app-core</artifactId>
<groupId>com.fangjian.framework</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dongjun.platform</groupId>
<artifactId>fenbushi_demoweb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>fenbushi_demoweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<dependencies>
<!-- 引入PO-->
<dependency>
<groupId>com.dongjun.platform</groupId>
<artifactId>fenbushi_po</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入service-->
<dependency>
<groupId>com.dongjun.platform</groupId>
<artifactId>fenbushi_servicedao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>fenbushi_demoweb</finalName>
</build>
</project>
4、(1)修改fenbushi_demoweb的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">
<parent>
<artifactId>app-core</artifactId>
<groupId>com.fangjian.framework</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dongjun.platform</groupId>
<artifactId>fenbushi_demoweb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>fenbushi_demoweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<dependencies>
<!-- 引入PO-->
<dependency>
<groupId>com.dongjun.platform</groupId>
<artifactId>fenbushi_po</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入service-->
<dependency>
<groupId>com.dongjun.platform</groupId>
<artifactId>fenbushi_servicedao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>fenbushi_demoweb</finalName>
</build>
</project>
(2)添加配置
日志
#\u8bbe\u7f6e\u7ea7\u522b\u548c\u591a\u4e2a\u76ee\u7684\u5730
log4j.rootLogger=INFO,appender1,appender2
#\u8f93\u51fa\u5230\u63a7\u5236\u53f0
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
#\u8bbe\u7f6e\u8f93\u51fa\u6837\u5f0f
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
#\u8f93\u51fa\u5230\u6587\u4ef6(\u8fd9\u91cc\u9ed8\u8ba4\u4e3a\u8ffd\u52a0\u65b9\u5f0f)
log4j.appender.appender2=org.apache.log4j.FileAppender
#\u8bbe\u7f6e\u6587\u4ef6\u8f93\u51fa\u8def\u5f84
#\u30101\u3011\u6587\u672c\u6587\u4ef6
log4j.appender.appender2.File=d:/demo.log
#\u30102\u3011HTML\u6587\u4ef6
#log4j.appender.appender2.File=c:/Log4JDemo02.html
#\u8bbe\u7f6e\u6587\u4ef6\u8f93\u51fa\u6837\u5f0f
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout
#log4j.appender.appender2.layout=org.apache.log4j.HTMLLayout
spring-dubbo-customer.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: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="dubbo-service-client" />
<dubbo:registry address="zookeeper://192.168.3.249:2181" />
<dubbo:reference id="userservice" interface="com.dongjun.platform.service.UserService" check="false" timeout="10000" retries="0"/>
</beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.dongjun.web">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
<!-- 日期转换 必须放在<mvc:annotation-driven />前面 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<mvc:annotation-driven />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/jsp"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
下节讲述分布式中篇