zookeeper + dubbo + maven 基础实例(一)

1、搭建Maven工程:java

右键new->projecrt -> maven project git

  

 

2、配置Dubbo:github

DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架。算法

dubbo的工做模式:spring

 

节点角色说明:apache

  • Provider: 暴露服务的服务提供方。
  • Consumer: 调用远程服务的服务消费方。
  • Registry: 服务注册与发现的注册中心。
  • Monitor: 统计服务的调用次数和调用时间的监控中心。
  • Container: 服务运行容器。

调用关系说明:app

  • 0. 服务容器负责启动,加载,运行服务提供者。
  • 1. 服务提供者在启动时,向注册中心注册本身提供的服务。
  • 2. 服务消费者在启动时,向注册中心订阅本身所需的服务。
  • 3. 注册中心返回服务提供者地址列表给消费者,若是有变动,注册中心将基于长链接推送变动数据给消费者。
  • 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,若是调用失败,再选另外一台调用。
  • 5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

具体例子:负载均衡

一、编写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.fan.test</groupId> <artifactId>dubboTest</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.9</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</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> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.netflix.curator</groupId> <artifactId>curator-framework</artifactId> <version>1.1.16</version> </dependency> </dependencies> </project>

二、配置提供者:maven

package com.alibaba.dubbo.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider
{
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        context.start();
        System.out.println("请按任意键退出");
        System.in.read(); // 按任意键退出
    }
    
}
package com.alibaba.dubbo.demo;

/**
 * 提供者和消费者公用接口
 * 若是提供者和消费者是分别在两个不一样的工程中,则公共接口必须如出一辙,包括包名
 */
public interface DemoService 
{
    String sayHello(String name);
}
package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService
{
    public String sayHello(String name)
    {
        return "Hello " + name;
    }
}
<?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="dubbo-test-service"  />
  
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!--  <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" />
  
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
  
    <!-- 声明须要暴露的服务接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
  
    <!-- 和本地bean同样实现服务 -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
     
</beans>

三、配置消费者:

package com.alibaba.dubbo.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer
{
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
        context.start();
  
        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
        String res = demoService.sayHello("world"); // 执行远程方法
        System.out.println( res ); // 显示调用结果
    }
}
<?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="dubbo-test-consumer"  />
  
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!--   <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  
    <!-- 生成远程服务代理,能够和本地bean同样使用demoService -->
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
     
</beans>

3、启动zookeeper:

保证启动的zookeeper的ip和端口号和provider.xml、consummer.xml中的 <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 保持一致。

 

四:运行:

启动zookpeeper、启动provider.java 而后启动consummer.java,最后的运行结果以下:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Hello world 

 

源码下载: dubboTest.zip

相关文章
相关标签/搜索