dubbo 和zookeeper的DEMO的搭建

先安装zookeeper 注册中心  这个自己查一下

项目结构:


要分三个项目的  第一个是接口  第二个是提供者 第三个是消费者  

接口就一个

接口就一个文件:

   

package connector;

public interface DubboService {
	String sayHello(String name);
}

这个接口写好了打成jar包


提供者


导入 上面打的jar包

实现类

package dubbo.impl;

import connector.DubboService;

public class DubboServiceImpl implements DubboService {

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

}

启动入口

package dubbo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

	public static void main(String[] args) throws IOException {
		System.out.println("启动成功!");
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
	}

}

配置文件: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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"   />
 
    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.254.41:2181" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="connector.DubboService" ref="dubboService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="dubboService" class="dubbo.impl.DubboServiceImpl" />
</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>dubbo</groupId>
  <artifactId>dubbo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <dubbo.version>2.6.2</dubbo.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      <!-- dubbo start-->
	   <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
         <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>
        </exclusions>
      </dependency>
       <!-- dubbo end-->
       <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
     <!--  <dependency>
        <groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.6</version>
			<exclusions>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
       </dependency> -->
      <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.9</version>
      </dependency>
       <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
      <dependency>
       <groupId>org.apache.curator</groupId>
       <artifactId>curator-framework</artifactId>
      <version>4.0.1</version>
      </dependency>
  </dependencies>
</project>

消费者:

导入相同的jar包  

使用相同的pom.xml  

启动入口:

package dubboConsumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import connector.DubboService;

public class Consumer {

	public static void main(String[] args) {
		    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
	        context.start();
	        DubboService demoService = (DubboService)context.getBean("dubboService"); // 获取远程服务代理
	        String hello = demoService.sayHello("world"); // 执行远程方法
	        System.out.println( hello ); // 显示调用结果
	}

}

配置文件 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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer" owner="hxl"  />
 
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://192.168.254.41:2181" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="dubboService" interface="connector.DubboService" />
</beans>

然后先启动zookeeper注册中心

再启动提供者 

在运行消费者


猜你喜欢

转载自blog.csdn.net/qq_36497454/article/details/81001564