JAVA后台开发_从入门到精通dubbo

2 在项目中应用Dubbo与ZooKeeper

2017-06-16  本文已影响183人  笑Skr人啊
a):准备工作

1:首先,要熟悉如何创建Maven项目,请参照 MyEclipse配置Maven 和 创建Maven项目
2:下载Dubbo
3:下载ZooKeeper

b): 项目结构介绍

三个项目的目录结构,生产者(provider),接口(interface),消费者(consumer)

其中接口只是提供了接口的方法,无需其他配置。而生产者和消费者需要dubbo.xml配置文件来配置。

Paste_Image.png Paste_Image.png

** SayHelloWorldService **

package com.gp6.inter;

public interface SayHelloWorldService {
    
    public void sayHelloWorld();
}

Paste_Image.png

** 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.gp6</groupId>
  <artifactId>provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build/>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.6.RELEASE</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.4.9</version>
    </dependency>
    <dependency>
        <groupId>com.gp6</groupId>
        <artifactId>interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
    </dependency>
  </dependencies>
</project>

** 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:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    default-lazy-init="false">

    <bean id="helloService" class="com.gp6.provider.SayHelloWorldServiceImpl"></bean>

    <!-- 提供方应用名称-->
    <dubbo:application name="dubbo_product"></dubbo:application>
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register="">
    </dubbo:registry>
    
    <!-- 要暴露的服务接口 -->
    <dubbo:service interface="com.gp6.inter.SayHelloWorldService" ref="helloService" />
    
    <dubbo:protocol accesslog="true" name="dubbo" port="20880" />
</beans>

** SayHelloWorldServiceImpl **

package com.gp6.provider;

import com.gp6.inter.SayHelloWorldService;

public class SayHelloWorldServiceImpl implements SayHelloWorldService {

    public void sayHelloWorld() {
        
       System.out.println("Say Hello World");
    }
}

** ProviderTest **

package com.gp6.provider;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {
    public static void main(String[] args){
            
        //加载dubbo.xml
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath*:dubbo.xml" });
        
        //执行不退出程序的操作
        while(true);
    }
}

Paste_Image.png

** 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.gp6</groupId>
  <artifactId>consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.6.RELEASE</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.4.6</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>com.gp6</groupId>
        <artifactId>interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>
        consumer
    </finalName>
  </build>
</project>

** 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:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    default-lazy-init="false">

    <dubbo:application name="dubbo_customer"></dubbo:application>
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false">
    </dubbo:registry>
    
    <!-- 要引用的服务 -->
    <dubbo:reference interface="com.gp6.inter.SayHelloWorldService" id="helloService"  >
    </dubbo:reference>
</beans>

** ConsumerTest **

package com.gp6.consumer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gp6.inter.SayHelloWorldService;

public class ConsumerTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath*:dubbo.xml" });
        SayHelloWorldService  sayHelloWorldService = (SayHelloWorldService) applicationContext.getBean("helloService");
        
        //使用接口中的方法
        sayHelloWorldService.sayHelloWorld();
        while(true);
    }
}

** 生产者provider **

生产者所需jar包 消费者所需jar包

将下载的ZooKeeper解压,打开命令行窗口,输入命令

Paste_Image.png

将下载的Dubbo解压,放到Tomcat下的webapps下,启动Tomcat

Paste_Image.png Paste_Image.png

其实和运行其他javaweb项目一样 :locallhost:8080/ 加项目名称即可访问。localhost:8080/dubbo-admin/或127.0.0.1:8080/dubbo-admin

此时会提示输入账号密码,

Paste_Image.png

账号密码配置地址:E:\WorkSpace\Study\Apache_Tomcat_6.0\webapps\dubbo-admin\WEB-INF\dubbo.properties

Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读