`
Reverie夜
  • 浏览: 20322 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

【简单实例】Dubbo+Zookeeper+Spring

阅读更多

关于Dubbo~不介绍~不介绍~不介绍~(重要的事情要说三遍)

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓官网↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

http://dubbo.io/

↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

 

准备:

zookeeper(下载地址:https://zookeeper.apache.org/releases.html#download);

四个项目(只要服务提供端和服务消费端也行后面说~):

    (父项目)dubboparent

    (接口)dubboapi

    (服务提供端)dubboserver

    (服务消费端)dubboclient

 ヽ(  ̄д ̄;) 顺便一说,我用的是maven工具构建的项目

 

 启动zookeeper 

 下载 zookeeper 解压,把zoo_sample.cfg改名为zoo.cfg,因为Zookeeper在启动时会找这个文件作为默认配置文件;可以设置文件里的dataDir,设置为自己喜欢的输出路径~其他设置请找度娘~

设完后启动,为了好看直接用命令了
(linux是zkServer.sh,有点多余谁不知道?)

成功启动后,占用2181端口↓

 
 构建项目:

~写项目~写项目~写项目~写项目 ヽ(  ̄д ̄;)



 

dubboparent          /pom.xml (包括spring、dubbo、zookeeper的jar包)其他子项目需要依赖这货√

<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.night</groupId>
  <artifactId>dubboparent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>dubboparent</name>
  <url>http://maven.apache.org</url>
  
  <modules>
  	<module>../dubboapi</module>
  	<module>../dubboserver</module>
  	<module>../dubboclient</module>
  </modules>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-version>3.1.4.RELEASE</spring-version>  
	<slf4j-version>1.6.6</slf4j-version>
  </properties>

  <dependencyManagement>
  	<dependencies>
  		<dependency>
	    	<groupId>com.night</groupId>
			<artifactId>dubboapi</artifactId>
			<version>1.0-SNAPSHOT</version>
    	</dependency>
  	</dependencies>
  </dependencyManagement>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
	<!-- Spring -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
		<version>${spring-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-asm</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-beans</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-expression</artifactId>
		<version>${spring-version}</version>
	</dependency>
	<!-- spring end -->
	<!-- log -->
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.16</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>
	<!-- dubbo -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<version>2.5.3</version>
	</dependency>
	<!-- zkclient -->
	<dependency>
		<groupId>com.github.sgroschupf</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.1</version>
	</dependency>
	<!-- zookeeper -->
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.4.8</version>
	</dependency>
  </dependencies>
  
  <build>
		<plugins>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.12.4</version>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
		</plugins>
	</build>
  
  <distributionManagement>
  	<repository>
  		<id>releases</id>
  		<name>Internal Releases</name>
  		<url>http://192.168.1.233:8081/nexus/content/repositories/releases/</url>
  	</repository>
  	<snapshotRepository>
  		<id>snapshots</id>
  		<name>Internal Snapshots</name>
  		<url>http://192.168.1.233:8081/nexus/content/repositories/snapshots/</url>
  	</snapshotRepository>
  </distributionManagement>
  
</project>
 

 

dubboapi:(dubboserver和dubboclient的依赖这货√的接口)


 DemoService.java

package com.night.dubboapi.service;

/**
 * 
 * @author ReverieNight@Foxmail.com
 *
 */
public interface DemoService {
	String sayHi(String name);
}
 

 

dubboserver :


 

DemoServiceImpl.java

package com.night.dubboserver.service;

import java.text.SimpleDateFormat;

import com.night.dubboapi.service.DemoService;

/**
 * 
 * @author ReverieNight@Foxmail.com
 *
 */
public class DemoServiceImpl implements DemoService {

	@Override
	public String sayHi(String name) {
		String s = "Server: Hi~ " + name + " time: " 
				+ new SimpleDateFormat("EEEE HH:mm:ss").format(System.currentTimeMillis());
		System.out.println(s);
		return s;
	}

}

 

Main.java

package com.night.dubboserver;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @author ReverieNight@Foxmail.com
 *
 */
public class Main {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext content = new ClassPathXmlApplicationContext("applicationProvider.xml");
		content.start();
		System.out.println("按任意键退出");
		try {
			System.in.read();	//输入流阻塞,保持服务
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

 

applicationProvider.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="dubboserver-hi" />  
    
    <!-- 使用zookeeper注册中心暴露服务地址 --> 
    <dubbo:registry address="zookeeper://192.168.1.233:2181"  />
     
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />
          
    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="com.night.dubboapi.service.DemoService" ref="demoService" />  
    
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.night.dubboserver.service.DemoServiceImpl" />  
    
</beans>  
 

 

dubboclient:


 

Main.java

package com.night.dubboclient;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.night.dubboapi.service.DemoService;

/**
 * 
 * @author ReverieNight@Foxmail.com
 *
 */
public class Main {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext content = new ClassPathXmlApplicationContext("applicationConsumer.xml");
		content.start();
		
		DemoService demo = (DemoService) content.getBean("demoService");
		String s = demo.sayHi("reverie");
		System.out.println(s);
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 

applicationConsumer.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="dubboclient-hi" />  
    
    <dubbo:registry address="zookeeper://192.168.1.233:2181"  />
     
    <!-- 声明需要引用的服务接口 -->  
    <dubbo:reference id="demoService" interface="com.night.dubboapi.service.DemoService" />  
    
    
</beans>  
 

 

运行:

启动顺序

    zookeeper

    服务端 dubboserver

    消费端 dubboclient

 

运行结果

    
服务端:

消费端:
 
  附:ヽ(  ̄д ̄;) 如果只想一个服务端一个消费端(客户端)的话,由于暴露和引用的接口必须一样,所以在消费端需要有一个于消费端暴露的接口一样的接口;可以写一个一模一样的也可以把服务端打包引入消费端。

 

 -------------------------------------------------------华丽的分割线-----------------------------------------------------------

 

 注解方式:

 

 服务端spring配置文件加入:

<dubbo:annotation package="com.night.dubboserver.service" />

 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 ← 官方原话

 

消费端spring配置文件加入:

<dubbo:annotation package="com.night.dubboclient" />

 

 

 

 

 

 

 

 

  • 大小: 1.2 KB
  • 大小: 49.6 KB
  • 大小: 2 KB
  • 大小: 12.9 KB
  • 大小: 15.3 KB
  • 大小: 14.1 KB
  • 大小: 3.4 KB
  • 大小: 4.7 KB
  • 大小: 4.4 KB
  • 大小: 5.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics