dubbo-zookeeper

 说到分布式,你们可能很快就能想到阿里巴巴的dubbo,zoookeeper集群,目前spring-cloud也是比较热门的,spring-cloud是基于spinrg-boot之上进行协做的,java

 接下来就先说一下使用dubbo远程接口调用zookeeper集群web

 一、首先建立一个maven工程,命名为:dubbo-interface,该工程主要是进行接口的声明spring

 在其工程下建立一个包名为:com.dubbo.test.inter.service,包下建立一个接口类名为:TestService,其内容为以下:express

public interface TestService {
    public String getName(String name);
}
TestService接口类中的接口也就是咱们须要暴露的远程接口,第一个工程建立完成。
 
二、建立一个maven工程,命名为dubbo-provider,该工程主要是实现dubbo-interface工程中的接口。而且将接口暴露。
 建立好工程后,在pom.xml文件中引入须要的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.dubbo.test </ groupId >
       < artifactId > dubbo_provider </ artifactId >
       < version > 0.0.1-SNAPSHOT </ version >
       < packaging > war </ packaging >
       < dependencies >
          <!-- 此处为provider工程 依赖 dubbo-interface工程 的依赖配置 -->
             < dependency >
                   < groupId > com.dubbo.test.interface </ groupId >
                   < artifactId > dubbo_service_interface </ artifactId >
                   < version > 0.0.1-SNAPSHOT </ version >
             </ dependency >
             <!-- 配置Spring -->
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-aop </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-aspects </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-beans </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-context </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-context-support </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-core </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-expression </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-instrument </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-instrument-tomcat </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-messaging </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-orm </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-oxm </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-test </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-tx </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-web </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-webmvc </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-webmvc-portlet </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             < dependency >
                   < groupId > org.springframework </ groupId >
                   < artifactId > spring-websocket </ artifactId >
                   < version > 4.3.1.RELEASE </ version >
             </ dependency >
             <!-- end -->
             <!-- dubbo -->
             < dependency >
                   < groupId > org.apache.zookeeper </ groupId >
                   < artifactId > zookeeper </ artifactId >
                   < version > 3.4.8 </ version >
             </ dependency >
             < dependency >
                   < groupId > com.alibaba </ groupId >
                   < artifactId > dubbo </ artifactId >
                   < version > 2.5.3 </ version >
             </ dependency >
             < dependency >
                   < groupId > com.101tec </ groupId >
                   < artifactId > zkclient </ artifactId >
                   < version > 0.4 </ version >
             </ dependency >
             < dependency >
                   < groupId > org.javassist </ groupId >
                   < artifactId > javassist </ artifactId >
                   < version > 3.18.1-GA </ version >
             </ dependency >
             < dependency >
                   < groupId > org.slf4j </ groupId >
                   < artifactId > slf4j-api </ artifactId >
                   < version > 1.6.1 </ version >
             </ dependency >
             < dependency >
                   < groupId > org.slf4j </ groupId >
                   < artifactId > slf4j-log4j12 </ artifactId >
                   < version > 1.6.1 </ version >
                   < scope > test </ scope >
             </ dependency >
             < dependency >
                   < groupId > log4j </ groupId >
                   < artifactId > log4j </ artifactId >
                   < version > 1.2.16 </ version >
             </ dependency >
             < dependency >
                   < groupId > commons-io </ groupId >
                   < artifactId > commons-io </ artifactId >
                   < version > 2.5 </ version >
             </ dependency >
             < dependency >
                   < groupId > commons-logging </ groupId >
                   < artifactId > commons-logging </ artifactId >
                   < version > 1.2 </ version >
             </ dependency >
             <!-- end -->
       </ dependencies >
       <!-- 项目发布仓库 -->
      <!-- < distributionManagement >
             < repository >
                   < id > releases </ id
                   < name > Nexus Release Repository </ name >
                   < url > http://localhost:8081/nexus/content/repositories/releases/ </ url >
             </ repository >
             < snapshotRepository >
                   < id > snapshots </ id >
                   < name > Nexus Snapshot Repository </ name >
                   < url > http://localhost:8081/nexus/content/repositories/snapshots/ </ url >
             </ snapshotRepository >
       </ distributionManagement >-->
      <!--注意:我这里把项目的发布仓库注释了,由于这里涉及到maven中settings.xml文件的配置以及nexus,只须要知足上面依赖的jar包存在就ok--> 
</ project >
 
接下来建立一个包,名为 com.dubbo.test.provider,而后建立一个TestServiceImpl实现TestService接口,而且实现其接口类的方法。内容以下:
package com.dubbo.test.provider;
import com.dubbo.test.inter.service.TestService;
 
public class TestServiceImpl implements TestService{
    /*
     * @see com.dubbo.test.inter.service.TestService#getName(java.lang.String)
     */
    public String getName(String name) {
        return "hello " + name + ", success!!";
    }
}
 
接着就是比较重要的一步,那就是spring整合dubbo的配置,主要配置接口的暴露,由于jar包都已经依赖进来了。建立spring-dubbo-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:aop = "http://www.springframework.org/schema/aop"
    xmlns:context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc"
    xmlns:dubbo = "http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" >
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    < dubbo:application name = "dubbo-service" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    < dubbo:registry address = "zookeeper://localhost:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    < dubbo:protocol name = "dubbo" port = "20880" />
    <!-- 声明须要暴露的服务接口 -->
    < dubbo:service interface = "com.dubbo.test.inter.service.TestService"
        ref = "testService" />
    < bean id = "testService" class = "com.dubbo.test.provider.TestServiceImpl" />    
</ beans >
 
到目前,dubbo的远程接口提供者已经实现了,接下来建立一个测试类,命名为:DubboTest,内容以下:
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class DubboTest {    
    public static void main(String[] args ) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "spring-dubbo-provider.xml" );
        context .start();
        System. out .println( "服务启动成功!!!" + context .getBean( "testService" ));
    }  
}
dubbo-provider测试方式: ①首先启动zookeeper服务 ②而后在运行DubboTest中的main方法,main方法启动后,TestService类中的接口就被暴露了  不然是确定不能连上zookeeper的。
 
三、前两步已经把interface和provider建立好了,而且测试经过了,接下来咱们来实现它的消费者。
 建立一个maven工程名为: dubbo-consumer,该工程主要是远程调用dubbo-provider工程中暴露的接口来实现咱们须要的业务开发。
 首先pom文件的配置,这里我就很少说了,跟dubbo-provider工程中的pom.xml文件内容如出一辙。
 
 接下来就是spring整合dubbo,主要配置怎样使用到dubbo-provider工程中暴露的接口。建立一个spring-dubbo-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:aop = "http://www.springframework.org/schema/aop"
    xmlns:context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc"
    xmlns:dubbo = "http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" >
   
    <!-- 提供方应用信息,用于计算依赖关系 -->
    < dubbo:application name = "dubbo_provider" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    < dubbo:registry address = "zookeeper://localhost:2181" />
    < dubbo:consumer timeout = "5000" />
    < dubbo:reference id = "testService" interface = "com.dubbo.test.inter.service.TestService" />
</ beans >  
 
最后就是测试了,建立一个DubboTest类,实现以下:
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dubbo.test.inter.service.TestService;
 
public class DubboTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubbo-consumer.xml");
        context.start();
        TestService testService = context.getBean(TestService.class);
        System.out.println(testService.getName("xxx"));
        System.out.println("dubbo success!!!!!");
    }
}
 
到目前三个工程都已近完成了,接下来就是终极测试:
重点:
①启动zookeeper服务
②启动dubbo-provider工程中的main方法,暴露接口
③启动dubbo-consumer工程中的main方法,调用接口实现本身业务。
 
后续将会 继续把集成的知识点 和 其余的知识点补全(在web端查看暴露的服务而且设置)
相关文章
相关标签/搜索