Solo  当前访客:3 登录 注册

喧哗博客-http://blog.xuahua.com

繁华过后的沉寂--技术经验分享
浏览次数: 95,037    文章总数: 91    评论总数: 3
标签:

dubbox框架实例

1,dubbox-service端

 接口与实现一起在service端。使用maven单独打成独立的interface.jar即可。

包名规则如下:

   com.xuahua --根包

   ---------------interfaces --接口包

----------------service --接口实现包

-----------------model --接口使用类对外的Pojo

创建接口类名 com.xuahua.interfaces.DemoService.java

/**
* @author wyong
* 定义测试例子
*/
public interface DemoService {

/**
* @param name
* @return
* 定义方法,与某人打招呼
*/
String sayHello(String name); 


}

 

接口实现类com.xuahua.service.DemoServiceImpl.java

package com.xuahua.service;

import org.springframework.stereotype.Service;

import com.xuahua.interfaces.DemoService;

@Service("demoService")
public class DemoServiceImpl implements DemoService {

	public String sayHello(String name) {
		return "Hello " + name;  
	}


}

 

加载独立的spring_dubbox.xml文件,可以在spring-mvc.xml中最下面增加

<!-- **************************** 导入其他XML文件 **************************** -->
<import resource="spring_dubbo.xml"/>
<!-- **************************** /导入其他XML文件 **************************** -->

spring_dubbo.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="demoService_provider" owner="programmer" organization="dubbox" />  
    <dubbo:monitor protocol= "registry" />
    <!-- 使用zookeeper注册中心暴露服务地址  192.168.0.101 为zookeeper的ip地址及开放端口-->  
    <dubbo:registry protocol="zookeeper" address="192.168.0.101:2181"  />   
    
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />  
   
    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="com.xuahua.interfaces.DemoService" ref="demoService"  timeout="6000"/>  
      
    <!-- 具体的实现bean -->  
    <bean id="demoService" class="com.xuahua.service.DemoServiceImpl" />  
</beans>  

 

最后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.xuahua</groupId>
	<artifactId>dubbo-model-service</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>com.xuahua Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<!-- 定义本pom.xml引用版本属性, org.springframework-version,org.hibernate-version,project.build.sourceEncodeing 
		此名可随便取,必须与下面引用相匹配。 -->
	<properties>
		<org.springframework-version>4.0.0.RELEASE</org.springframework-version>
		<org.hibernate-version>4.3.1.Final</org.hibernate-version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.parent.version>2.8.4</project.parent.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- 此处可以添加相关联的,使用的jar包 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>${project.parent.version}</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.2</version>
</dependency>
		<!-- 下面是springframwork 4.xx 引用的包 -->
		<!-- spring 4 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<!-- Apache Commons 基础组件 -->
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.8.3</version>
		</dependency>

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>commons-configuration</groupId>
			<artifactId>commons-configuration</artifactId>
			<version>1.10</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.6</version>
		</dependency>

		<!-- 下面是返回json串时,使用到的处理相关包 Jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.13</version>
		</dependency>

		<!-- slf4j 日志文件框架使用的jar包 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>

		<!-- mysql driver 数据库驱动包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.15</version>
		</dependency>

		<!-- c3p0 datasource 数据库联接池使用的,此处是c3p0 -->
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
		<!-- druid 联接池,同时也提供内部默认的监控,个人推荐 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.23</version>
		</dependency>
		<!-- dbcp使用的默认联接池 配合 commons-pool 使用 -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.5.4</version>
		</dependency>


		<!-- 中间件 ibatis -->
		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-core</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.3</version>
		</dependency>
		<!-- hiberante 4 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${org.hibernate-version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>${org.hibernate-version}</version>
		</dependency>
		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.18.1-GA</version>
		</dependency>
		<!-- http-client -->
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.3.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.3.3</version>
		</dependency>

		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.7.4</version>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.4</version>
		</dependency>
		<!-- json-lib -->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.7</version>
		</dependency>
		<!-- memcached -->
		<dependency>
			<groupId>com.whalin</groupId>
			<artifactId>Memcached-Java-Client</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.maven-play-plugin.spy</groupId>
			<artifactId>spymemcached</artifactId>
			<version>2.6</version>
		</dependency>

		<!-- servlet3 -->
		<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>servlet-api</artifactId>
			<version>2.3</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
	</dependencies>
	<build>
		<finalName>dubbo-service</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.0.2</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>jar</goal>
						</goals>
						<configuration>
							<classifier>interface</classifier>
							<includes>
								<include>**/interfaces/*</include>
								<include>**/model/*</include>
							</includes>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

 

####################################以上为dubbox-service的配置,########################

2,dubbox-client配置如下

包名规则如下

com.xuahua --------根包名

---------------controller --对外调用的服务,可以在内部使用上面暴露的接口。

 

com.xuahua.controller.DemoController.java 

内容如下

package com.xuahua.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xuahua.interfaces.DemoService;



/**
 * @author wyong
 * @version 1.0
 * @link QQ343886028
 */
@Controller
@RequestMapping("/demo")
public class DemoController {

	@Autowired
	DemoService demoService;
//	
	@RequestMapping("/index")
	public String index(Model model){
		String result =demoService.sayHello("xuahua");
		System.out.println("======="+result);
		
		//此处是模拟调用请求,本人测试,服务端默认参数,超过5000个必定会抛出异常。
//		for(int i=0;i<1000;i++){
//			new Thread(new DemoController.process(demoService,String.valueOf(i))).start();
//		}
		return "index";
	}
	 
	/**
	 * @author wyong
	 * 此处只是做了一个多线程请求,模拟压力测试。。
	 
	class process implements Runnable {
		DemoService demoService;
		String name;
		public process(DemoService demoService,String name){
			this.demoService = demoService;
			this.name = name;
			Thread.currentThread().setName(name);
		}
		public void run() {
			 long s = System.currentTimeMillis();
			 String result =demoService.sayHello(name);
			 long b = System.currentTimeMillis();
			System.out.println(Thread.currentThread().getName()+"=>"+result+",cost=>"+(b-s)+"ms");
		}
		
	}*/
}

 

spring_dubbox.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="consumer-of-helloworld-web"  />
  <dubbo:monitor protocol= "registry" />
  
    <!-- 使用zookeeper注册中心暴露服务地址  192.168.0.101 为zookeeper的ip地址及开放端口--> 
 <dubbo:registry protocol = "zookeeper" address="192.168.0.101:2181" /> 
 <dubbo:protocol name="dubbo"  port="20880" />
 
 <!-- 对于初始化,强制使用 demoService 参数init="true" -->
	<dubbo:reference id="demoService" interface="com.xuahua.interfaces.DemoService" check="false" timeout="5000" /> 
</beans>  

 

同样在spring-mvc.xml配置最后面引入

<!-- **************************** 导入其他XML文件 **************************** -->
<import resource="spring_dubbo.xml" />
<!-- **************************** /导入其他XML文件 **************************** -->

 

dubbx-client工程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.xuahua</groupId>
	<artifactId>dubbo-module-client</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
	<name>com.xuahua Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<!-- 定义本pom.xml引用版本属性, org.springframework-version,org.hibernate-version,project.build.sourceEncodeing 
		此名可随便取,必须与下面引用相匹配。 -->
	<properties>
		<org.springframework-version>4.0.0.RELEASE</org.springframework-version>
		<org.hibernate-version>4.3.1.Final</org.hibernate-version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.parent.version>2.8.4</project.parent.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- 从provider工程打包放到nexus上面后,再引用 -->
		<dependency>
			<groupId>com.xuahua.interfaces</groupId>
			<artifactId>demoService</artifactId>
			<version>1.0</version>
		</dependency>
		<!-- 此处可以添加相关联的,使用的jar包 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>${project.parent.version}</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.2</version>
		</dependency>

		<!-- 下面是springframwork 4.xx 引用的包 -->
		<!-- spring 4 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<!-- Apache Commons 基础组件 -->
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.8.3</version>
		</dependency>

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>commons-configuration</groupId>
			<artifactId>commons-configuration</artifactId>
			<version>1.10</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.6</version>
		</dependency>

		<!-- 下面是返回json串时,使用到的处理相关包 Jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.13</version>
		</dependency>

		<!-- slf4j 日志文件框架使用的jar包 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>

		<!-- mysql driver 数据库驱动包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.15</version>
		</dependency>

		<!-- c3p0 datasource 数据库联接池使用的,此处是c3p0 -->
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
		<!-- druid 联接池,同时也提供内部默认的监控,个人推荐 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.23</version>
		</dependency>
		<!-- dbcp使用的默认联接池 配合 commons-pool 使用 -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.5.4</version>
		</dependency>


		<!-- 中间件 ibatis -->
		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-core</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.3</version>
		</dependency>
		<!-- hiberante 4 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${org.hibernate-version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>${org.hibernate-version}</version>
		</dependency>
		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.18.1-GA</version>
		</dependency>
		<!-- http-client -->
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.3.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.3.3</version>
		</dependency>

		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.7.4</version>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.4</version>
		</dependency>
		<!-- json-lib -->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.7</version>
		</dependency>
		<!-- memcached -->
		<dependency>
			<groupId>com.whalin</groupId>
			<artifactId>Memcached-Java-Client</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.maven-play-plugin.spy</groupId>
			<artifactId>spymemcached</artifactId>
			<version>2.6</version>
		</dependency>

		<!-- servlet3 -->
		<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>servlet-api</artifactId>
			<version>2.3</version>
			<scope>provided</scope>
		</dependency>

	</dependencies>

</project>

 

特别要注意pom.xml中标红的,那是我使用dubbox-server工程打出来的接口jar包,我上传到了本地仓库中。如果你直接放到了lib中,就不需要此引用。

完整源码包下载

dubbox-service

dubbox-client

 

 

 

 

No provider available for the service 有更新!

1,在搭建dubbo生产者与消费者过程中,启动生产者正常。。

启动消费者时,提示报错。

报错如下  http://file.xuahua.com/blog/2016/0901/QQ%E5%9B%BE%E7%89%8720160901112511.png

 

配置如下:

生产者 配置文件独立的 spring-dubbo.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">  
   
    <!-- 具体的实现bean -->  
    <bean id="demoService" class="com.xuahua.service.DemoServiceImpl" />  
      
    <!-- 提供方应用信息,用于计算依赖关系 -->  
    <dubbo:application name="demoService_provider" owner="programmer" organization="dubbox" />  
   
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry protocol="zookeeper" address="192.168.0.101:2181"  />   
    
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />  
   
    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="com.xuahua.interfaces.DemoService" ref="demoService"  timeout="3000" />  
      
</beans> 

 

消费者配置文件 spring_dubbo.xml

 <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
    <dubbo:application name="demoService_consumer" owner="programmer" organization="dubbox" />  
  
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
 
    <dubbo:registry protocol="zookeeper" address="192.168.0.101:2181" />  
  
    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
    <dubbo:reference id="demoService"  
        interface="com.xuahua.interfaces.DemoService" check="false" />  

之前定义的interface接口文件  DemoService.java

package com.xuahua.interfaces;

import java.util.List;

/**
 * @author wyong
 * 定义测试例子
 */
public interface DemoService {

	/**
	 * @param name
	 * @return
	 * 定义方法,与某人打招呼
	 */
	String sayHello(String name);  
	  
    /**
     * @return
     * 用户列表
     */
    public List getUsers(); 
}

 

此文件与实现在生产端布署。所以实现文件为

DemoServiceImpl.java

package com.xuahua.service;

import java.util.ArrayList;
import java.util.List;




import org.springframework.stereotype.Service;

import com.xuahua.interfaces.DemoService;
import com.xuahua.model.User;

@Service("demoService")
public class DemoServiceImpl implements DemoService {

	public String sayHello(String name) {
		return "Hello " + name;  
	}

	public List getUsers() {
		 List list = new ArrayList();  
         User u1 = new User();  
         u1.setName("jack");  
         u1.setAge(20);  
         u1.setSex("男");  
           
         User u2 = new User();  
         u2.setName("tom");  
         u2.setAge(21);  
         u2.setSex("女");  
           
         User u3 = new User();  
         u3.setName("rose");  
         u3.setAge(19);  
         u3.setSex("女");  
           
         list.add(u1);  
         list.add(u2);  
         list.add(u3);  
         return list;  
	}

}

 

其中的User.java 自己定义了三个属性。

package com.xuahua.model;

public class User extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String name;
	Integer age;
	String sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}

}

以上为生产者的配置以及实现。

把DemoService接口打成独立 的jar。放到消费者端lib包中。

消费者端,实现

ackage com.xuahua.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xuahua.interfaces.DemoService;
import com.xuahua.model.User;

@Controller
@RequestMapping("/demo")
public class DemoController {

	DemoService demoService;
	
	
    //定义此方法Uri 请求路径为 /demo/index
	@RequestMapping("/index")
	public String index(Model model){
		List<User> users = demoService.getUsers();
		if(users!=null && !users.isEmpty()){
			for(User u : users){
				System.out.println(u.getName()+"--"+demoService.sayHello(u.getName()));
			}
		}
		System.out.println("=========");
		//默认会读取配置文件中 spring-webmvc.xml 中配置;
		//路径 /WEB-INF/views/index.jsp
		return "index";
	}


	public DemoService getDemoService() {
		return demoService;
	}


	public void setDemoService(DemoService demoService) {
		this.demoService = demoService;
	}

}

 

问题怎么解决?等待揭晓答案。。。。

从二个方面入手。

1,telnet 192.168.0.101 2181 是否ok

2,从消费者发起telnet 192.168.0.38 20880

输入命令应该可以看到提供的服务。

以上记录下。

 

分布式框架安装与布署-dubbo 有更新!

 前言

在电子商务行业发展到今天,各种应用支持高并发的开源框架进行无数的实践以及验证。其中首推淘宝的dubbo,虽说这套系统淘宝不再跟进升级,但是其他从dubbo衍生出来的分支,dubbox等很多继续在使用中。。

像京东,当当网都是在使用的dubbo框架。

好了,话入正题

 ;)淘宝dubbo,只支持spring2.x ,下载地址 https://github.com/alibaba/dubbo

:)当当网的分支dubbox 已经可以支持spring3.x 下载地址 https://github.com/dangdangdotcom/dubbox

 

root> git clone https://github.com/dangdangdotcom/dubbox dubbo

root> cd dubbo

root> mvn clean install -Dmaven.test.skip=true 

在这里不跳过测试,会出现 N多问题,曾经在此花了N多时间来解决或看源码,最后放弃。。先直接跑起来才是王道。所以加上 -Dmaven.test.skip=true

生成的 com.allibaba.dubbo.jar 在 dubbo/target/目录下。

为了后续应用的使用,把此jar包上传至maven 私有库nexus中。

上传私有库

 

至此,dubbox 编译完成。

二,布署 zookeeper注册中心  原址:http://blog.csdn.net/huwei2003/article/details/49101269

官网文档地址大全:

OverView(概述)
http://zookeeper.apache.org/doc/r3.4.6/zookeeperOver.html

Getting Started(开始入门)
http://zookeeper.apache.org/doc/r3.4.6/zookeeperStarted.html

Tutorial(教程)
http://zookeeper.apache.org/doc/r3.4.6/zookeeperTutorial.html

Java Example(Java示例)
http://zookeeper.apache.org/doc/r3.4.6/javaExample.html

Programmer's Guide(开发人员指南)
http://zookeeper.apache.org/doc/r3.4.6/zookeeperProgrammers.html

Recipes and Solutions(技巧及解决方案)
http://zookeeper.apache.org/doc/r3.4.6/recipes.html

3.4.6 API online(在线API速查)

http://zookeeper.apache.org/doc/r3.4.6/api/index.html

另外推荐园友sunddenly的zookeeper系列
http://www.cnblogs.com/sunddenly/category/620563.html

 

文在一台机器上模拟3个 zk server的集群安装

1.1 下载解压

解压到3个目录(模拟3台zk server):

  /home/hadoop/zookeeper-1
  /home/hadoop/zookeeper-2
  /home/hadoop/zookeeper-3
1.2 创建每个目录下conf/zoo.cfg配置文件

/home/hadoop/zookeeper-1/conf/zoo.cfg 内容如下:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/hadoop/tmp/zk1/data
dataLogDir=/home/hadoop/tmp/zk1/log
clientPort=2181
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

/home/hadoop/zookeeper-2/conf/zoo.cfg 内容如下:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/hadoop/tmp/zk2/data
dataLogDir=/home/hadoop/tmp/zk2/log
clientPort=2182
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

/home/hadoop/zookeeper-3/conf/zoo.cfg 内容如下:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/hadoop/tmp/zk3/data
dataLogDir=/home/hadoop/tmp/zk3/log
clientPort=2183
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

注:因为是在一台机器上模拟集群,所以端口不能重复,这里用2181~2183,2287~2289,以及3387~3389相互错开。另外每个zk的instance,都需要设置独立的数据存储目录、日志存储目录,所以dataDir、dataLogDir这二个节点对应的目录,需要手动先创建好。

另外还有一个灰常关键的设置,在每个zk server配置文件的dataDir所对应的目录下,必须创建一个名为myid的文件,其中的内容必须与zoo.cfg中server.x 中的x相同,即:

/home/hadoop/tmp/zk1/data/myid 中的内容为1,对应server.1中的1
/home/hadoop/tmp/zk2/data/myid 中的内容为2,对应server.2中的2
/home/hadoop/tmp/zk3/data/myid 中的内容为3,对应server.3中的3

生产环境中,分布式集群部署的步骤与上面基本相同,只不过因为各zk server分布在不同的机器,上述配置文件中的localhost换成各服务器的真实Ip即可。分布在不同的机器后,不存在端口冲突问题,可以让每个服务器的zk均采用相同的端口,这样管理起来比较方便。

1.3 启动验证

/home/hadoop/zookeeper-1/bin/zkServer.sh start

/home/hadoop/zookeeper-2/bin/zkServer.sh start

/home/hadoop/zookeeper-3/bin/zkServer.sh start
启用成功后,输入 jps 看下进程

20351 ZooKeeperMain
20791 QuorumPeerMain
20822 QuorumPeerMain
20865 QuorumPeerMain

应该至少能看到以上几个进程。

可以启动客户端测试下:

bin/zkCli.sh -server localhost:2181
(注:如果是远程连接,把localhost换成指定的IP即可)

成功后,应该会进到提示符下,类似下面这样:

[zk: localhost:2181(CONNECTED) 0]

 

二,布署dubbo-admin-2.8.4.war包,

进入dubbo目录

root> cd dubbo-admin

root> mvn clean package -Dmaven.test.skip=true

打包以后,复制 dubbo-admin.2.8.4.war包复制到 tomcat webapps/root中。

注意:

如果启动提示找不到 /META-INF/spring/dubbo-admin.xml

可以在 源码文件中找到或者修改 /WEB-INF/webx.xml 

<!-- 应用资源 -->
<beans:import resource="common/dubbo-admin.xml" />
<!-- <beans:import resource="classpath:/META-INF/spring/dubbo-admin.xml" /> -->

我把dubbo-admin.xml 复制到了 /WEB-INF/common/目录下。

同时修改tomcat中server.xml中 port=8090

访问 http://localhost:8090/dubbo-admin.2.8.4/ 根据个人情况,我是直接使用源码编译至 root目录下,所以访问路径有些变化,http://localhost:8090/

如下图dubbo后台

 

 

 

 

三,监控dubbo-monitor布署

下载

root> git clone https://github.com/handuyishe/dubbo-monitor dubbo-monitor

root> cd dubbo-monitor

root>mvn clean package -Dmaven.test.skip=true

进入 target 目录找到 dubbo-monitor.2.8.4.war 

布署到tomcat webapps/root/目录中。。我是解压后再扔进去,没有dubbo-monitor.2.8.4 这层目录,所以访问直接使用http://localhost:8091/即可;

修改service.xml 。因为已经存在一个host,需要同时复制一个host,同时修改port=8091

重启tomcat。

访问http://localhost:8091/

结果如下 dubbo-monitory

 

 

公告

喧哗博客--繁华过后的沉寂--技术经验分享^-^
Copyright (c) 2009-2019, b3log.org & hacpai.com