处女博客,不知道规则怎么写,直接入主题吧。

很多人刚接触分布式,集群负载均衡时都觉得高深莫测,难度有点大,其实起码入手是很简单的。我刚入公司就让我搞这个的研究,之前完全小白,nginx都没有配置过。搞了1天半,总算弄了个完整的测试项目出来,写了这个博客,以兹鼓励与纪念!
Dubbo不多说了,在国内分布式服务这块还是很牛逼的,是基于服务分布式框架,能轻松实现服务层面的负载均衡。官方文档地址http://dubbo.io/User+Guide-zh.htm
Nginx也不用多说,通过异步非阻塞能够有很大的并发量,据说是百万级,我没有真正压测过。上手十分简单,广泛应用于网站负载均衡,能轻松实现HTTP层面的负载均衡。

今天的的博客介绍的是服务层面用dubbo+zookeeper实现分布式服务,dubbo本身已经实现了服务负载均衡,所以很简单,然后Http web层用nginx负载均衡。

第一步:Zookeeper下载,安装,配置
1.1:下载: http://apache.fayea.com/zookeeper/   一般选择稳定版本,我选的是3.4.9

1.2:然后解压到一个目录,打开conf目录copy一份zoo_sample.cfg文件,并重命名为zoo.cfg(必须是这个名字)

1.3:打开zoo.cfg,入手阶段基本不用做什么修改,唯一需要注意的是端口号,后面的dubbo 管理配置需要和这个保持一致(后面说)

1.4:找到bin目录下的 zKserver.cmd 启动zookeeper

这里zookeeper就OK了

第二步:下载,配置dubbo的管理项目

2.1 下载 dubbo admin 项目 地址: http://dubbo.io/Download-zh.htm

2.2 解压,取出war包。
2.3 拷贝一个tomcat,改一下端口号(用默认容易和其他的tomcat端口冲突)


2.4 删除 tomcat webapp 目录下面的文件,将dubbo admin 包解压内容放到root目录中,修改classpath 目录下的dubbo.properties, 将这个监测中心注册到zookeeper,即将dubbo.registry.address改成你安装zookeeper的机子IP+zookeeper配置的端口。我这里是一台机子演示,默认端口与zookeeper是一致的所以不用修改。用户名密码就没有改动的必要了。



2.5 bin目录启动tomcat,输入这个tomat的访问网址 http://localhost:8180/  (端口号注意修改成你自己改的那个),然后登陆密码都是就是上面配置文件中的,root  root登录


这里可以看到,提供者为0,消费者为0

第三步:创建提供者

3.1:创建公用接口项目 service,创建两个provider (provider01,provider02)



service包,公用接口,很简单,就是个maven基础java包,不需要加入任何依赖,也不需要加入任何配置文件
<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.test</groupId>
  <artifactId>service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

2个provider包引入service依赖,且需要引入dubbo,zookeeper,log4依赖,这里不需要引入Spring,dubbo自带依赖,不过版本很旧,也可以自己手动移除然后引入新版本Spring。下面给出其中一个的pom实例,copy请自行修改artifactId
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.test</groupId>
	<artifactId>provider02</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>com.test</groupId>
			<artifactId>service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.3</version>
		</dependency>

		<!-- 日志相关 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.5</version>
		</dependency>

		<!-- zookeeper client依赖,用于向zookeeper注册服务 -->
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-jar-plugin</artifactId>
					<configuration>
						<source>1.8</source>
						<target>1.8</target>
						<archive>
							<manifest>
								<mainClass>com.test.provider.Provider</mainClass>
								<addClasspath>true</addClasspath>
								<classpathPrefix>lib/</classpathPrefix>
							</manifest>

						</archive>
						<classesDirectory>
						</classesDirectory>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

</project>


3.2  在service公有服务接口包中新建接口TestService,内容很简单,就包含一个测试接口
TestService.java:
package com.test.service;

/**
 * @author xianghui1
 *
 *test
 */
public interface TestService {

	public String test();
}




3.3  provider 提供者需要实现这个公用接口以向消费者提供服务,两个provider基本是一样的,除了部分配置
provider02 :   TestServiceImpl.java实现公有接口 testService
package com.test.service;

import com.test.service.TestService;

public class TestServiceImpl implements TestService {

	public String test() {
		System.out.println("test success");
		return "provider02";
	}

}


provider01 :   TestServiceImpl.java实现公有接口 testService
package com.test.service;

import com.test.service.TestService;

public class TestServiceImpl implements TestService {

	public String test() {
		System.out.println("test success");
		return "provider01";
	}

}

2个provider,随意实现,返回效果不同就行了,因为后面要测试集群负载均衡效果

3.4 到这里提供者代码层面基本完了,该项目增加配置文件,在classpath目录下新建3个配置文件:applicationContext.xml(Spring配置文件),log4.properties,provider.xml( dubbo 提供者配置文件,重要


applicationContext.xml:(2个provider一模一样)
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
    <context:component-scan base-package="com.test" />
    
</beans>
log4.properties:(2个provider一样)
log4j.appender.mylog = org.apache.log4j.ConsoleAppender

log4j.appender.mylog.target = System.out

log4j.appender.mylog.layout= org.apache.log4j.PatternLayout

log4j.appender.mylog.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

log4j.appender.my2log = org.apache.log4j.RollingFileAppender

log4j.appender.my2log.File = D://Logger//mylog.log

log4j.appender.my2log.MaxFileSize = 50KB

log4j.appender.my2log.MaxBackupIndex = 1

log4j.appender.my2log.layout= org.apache.log4j.PatternLayout
 
log4j.appender.my2log.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

log4j.rootLogger = info,mylog,my2log



provider01:    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: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="provider"  />
 
    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20881" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="testService" class="com.test.service.TestServiceImpl" />
    
     
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.test.service.TestService" ref="testService" />
 
</beans>



provider02:    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: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="provider" />

	<!-- 使用zookeeper广播注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://10.120.106.35:2181" />

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20882" />

	<!-- 和本地bean一样实现服务 -->
	<bean id="testService" class="com.test.service.TestServiceImpl" />


	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="com.test.service.TestService" ref="testService" />

</beans>




这里需要注意,2个provider的应用名,一样,因为模拟集群负载均衡,所以对Dubbo来说是一个服务。2个provider的端口需不一样,因为是同一台机子模拟2个服务,如果是多台就无所谓了。dubbo:Service须保持一致,因为2个provider负载均衡是模拟一个服务,所以提供的服务外面看来须一样。

3.5,业务代码以及配置文件都完成了,该启动2个provider了,新建provider类,并调用main启动,代码如下;
Provider.java(2个provider一样即可)
package com.test.provider;

import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
	// start provider
	static{
		PropertyConfigurator.configure("src/main/resources/log4.properties");
	}
	public static void main(String args[]) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/*.xml");
		context.start();
		System.in.read();
	}
}




成功启动之后,显示注册到了zookeeper。



这个时候我们刷新下dubbo.admin页面看看效果,显示提供者为1,说明一切正常



然后启动第二个provider,至这里显示提供者为2,服务数为1,一切OK,下一步我们开始消费者端。


第四步:创建消费者,这里创建2个Spring mvc web项目来充当消费的角色。

2个消费者(dubbo-consumer01,dubbo-consumer02)也一样,基本一样,就是服务实现不一样来测试负载均衡,还有个消费者配置文件不一样。


4.1 2个消费者POM.xml基本是一样的,标准的Spring mvc web项目,需要引入dubbo,zookeeper,公有服务service,Spring mvc系列,log4系列。这里需要注意的是,dubbo的Spring实在过于老旧了,对于web项目来说是不合适的,遇到了版本冲突,所以需要剔除dubbo中依赖的spring2.5

pom.xml(copy请自行修改 groupId与artifactId):
<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.test</groupId>
  <artifactId>dubbo-consumer02</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>customer02 Maven Webapp</name>
  <url>http://maven.apache.org</url>
 	<properties>
		<spring.version>4.3.8.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.test</groupId>
			<artifactId>service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- spring begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</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-context</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-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
				<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- spring end -->

		<!-- web jar -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.1.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.3.1</version>
			<scope>provided</scope>
		</dependency>
		<!-- jstl jar -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>

		<!-- jackson jar -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-jaxrs</artifactId>
			<version>1.9.11</version>
		</dependency>

		<!-- commons jar -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-collections4</artifactId>
			<version>4.0</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.8.3</version>
		</dependency>
		<dependency>
			<groupId>commons-chain</groupId>
			<artifactId>commons-chain</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-math3</artifactId>
			<version>3.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-digester3</artifactId>
			<version>3.2</version>
		</dependency>
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.3</version>
		</dependency>
		<dependency>
			<groupId>commons-dbutils</groupId>
			<artifactId>commons-dbutils</artifactId>
			<version>1.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-email</artifactId>
			<version>1.3.3</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

		<!-- 日志相关 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.5</version>
		</dependency>
		<!-- zookeeper client依赖,用于向zookeeper注册服务 -->
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>dubbo-customer02</finalName>
	</build>
</project>


4.2,两个web项目的主要配置文件,共5个:web.xml、applicationContext.xml、consumer.xml、log4.properties、springmvc-servlet.xml。

web.xml :基本一样这里只放出一个
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<display-name>Archetype Created Web Application</display-name>


	<!-- 关键配置 contextListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<!-- log4 -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4.properties</param-value>
	</context-param>

	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>3000</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	
	<!-- 字符过滤器 -->
	<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>


	<!-- 设置tomcat对静态资源默认拦截 -->
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.jpg</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.png</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>
	<!-- 关键配置 dispatcherServlet -->
	<!-- 拦截所有的请求,包括静态资源 -->
	<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*:springmvc-servlet.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>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>



applicationContext.xml    2个consumer一模一样,只是引入consumer消费者配置文件,很简单

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
    <import resource="classpath*:/consumer.xml" />
    
</beans>


consumer.xml:    消费者配置文件,这个很重要,这里消费者应用名不能一样了,因为我模拟是2个消费者,然后需要向zookeeper中心注册,还有个很重要的就是引用提供者提供的远程服务

consumer01:   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: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="consumer01"  />
 
    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="testService" interface="com.test.service.TestService" />
 
</beans>

consumer02:   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: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="consumer02"  />
 
    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="testService" interface="com.test.service.TestService" />
 
</beans>



log4配置文件,没什么好说的,同provider,这里不粘了


springmvc-servlet.xml :   配置文件,很简单,启用注解与controller扫描,没了。

<?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:p="http://www.springframework.org/schema/p"
	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.3.xsd    
http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-4.3.xsd 
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
	default-lazy-init="true">

	<!-- 启用注解 -->
	<context:annotation-config />

	<!-- 扫描Controller -->
	<context:component-scan base-package="com.test.controller" />
	<!-- 完成请求与注解的POJO映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
		
		
	<bean id="defaultViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:order="3">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="contentType" value="text/html" />
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

4.3   spring mvc web 项目配置已经完成了,这一步,可以直接启动项目,启动成功,且能进helloworld 页则说明你的配置没有出现细微错误,否则需要检查检查,保证项目是正常的,不然后面排错懵逼。

现在我们新建个controller,很简单,就弄个接口,调用提供者的远程服务
consumer01:     TestController:   
package com.test.controller;


import java.util.Map;


import javax.annotation.Resource;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.test.service.TestService;




/**
 * @author xianghui1
 *
 *         测试controller
 */
@Controller
public class TestController {


	@Resource(name = "testService")
	TestService testInterface;


	@RequestMapping(value = { "/consumer/testMain" })
	@ResponseBody
	public String testDubbo() {
		return "consuemr01" + testInterface.test();
	}


}




consumer02    TestController: 
package com.test.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.service.TestService;


/**
 * @author xianghui1
 *
 *测试controller
 */
@Controller
public class TestController {

	@Resource(name="testService")
	TestService testInterface;
	
	@RequestMapping(value={"/consumer/testMain"})
	@ResponseBody
	public String testDubbo(){
		return "consuemr02"+testInterface.test();
	}
	
}


4.4,然后我们可以启动了,我们将2个consuemr分别放在两个tomcat中运行,2个tomcat端口要不一致的

可以看出2个consumer,2个provider,一切正常,我们在浏览器中访问我们的接口,也是正常


第五步:到这里我们的dubbo+zookeeper+Spring mvc 搭建完成了,下一步,加入nginx 实现http层面的负载均衡


5.1   下载nginx,解压,并修改conf目录下的nginx.conf

5.2我们新建个upstream 负载均衡,采取默认的nginx负载均衡策略,然后监听80端口,将80端口所有的请求代理转发到负载上面


5.3 命令行启动nginx,然后在浏览器中输入接口网址即可发现会出现四种结果,consumer01provider01、consumer02provider01、consumer01provider02、consumer02provider02.   dubbo+zookeeper+spring mvc+nginx负载均衡测试完成



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐