2

dubbo是目前非常流行的分布式服务技术,很多公司都在用。空闲之余,搭了个helloworld,分享给大家。
本文demo下载
1.下载 zookeeper
zookeeper是服务的注册中心,下载后进入到安装目录G:bakCenterzookeeper-3.3.6bin。
双击zkServer.cmd即可启动注册中心服务。
zkServer.sh【Linux】或zkServer.cmd【Windows】
Zookeeper的配置文件在 conf 目录下,这个目录下有 zoo_sample.cfg 和 log4j.properties,需要将zoo_sample.cfg 改名为 zoo.cfg,Zookeeper在启动时会找这个文件作为默认配置文件

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181

配置说明可以参考这里
2.服务提供者
定义服务接口:

package com.hy.service;
public interface DemoService {
    String sayHello(String name);
}

实现服务接口:

package com.hy.service.impl;

import com.hy.service.DemoService;
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        System.out.println("init : " + name);
        return "hello " + name;
    }
}

在Spring配置中注册服务。

<?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-demo" />
    <!-- zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:protocol name="dubbo" port="20880" />
    
    <!-- 和本地bean一样实现服务 --> 
    <bean id="demoService" class="com.hy.service.impl.DemoServiceImpl" />

    <!-- 向注册中心注册暴漏服务地址,注册服务 -->
    <dubbo:service interface="com.hy.service.DemoService"
        ref="demoService" executes="10" />

</beans>

执行服务提供方主方法启用服务:

package main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderMain {

    public static void main(String[] args) throws IOException {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });
        context.start();

        System.out.println("服务注册成功..");
        System.in.read();
        context.close();
    }
}

3.dubbo监控中心查看服务下载地址
下载后,将dubbo-admin-2.5.4-SNAPSHOT.war包放置tomcat服务器的webapps目录,启动tomcat服务器,打开下面链接即可查看可用的服务及其状态 http://localhost:8080/dubbo-a...图片描述
用户名和密码配置在C:Program FilesApache Software FoundationTomcat 7.0webappsdubbo-admin-2.5.4-SNAPSHOTWEB-INFdubbo.properties文件中

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

4.服务消费者
通过Spring配置订阅服务提供方暴露的服务

<?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="consumer-of-dubbo-demo" />

    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 向注册中心订阅服务 -->
    <dubbo:reference id="demoService" interface="com.hy.service.DemoService" />
</beans> 

执行服务消费方主方法启用服务:

package main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hy.service.DemoService;

public class ConsumerMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationConsumer.xml" });
        context.start();
        DemoService service = (DemoService) context.getBean("demoService");
        System.out.println(service.sayHello("world"));
        context.close();
    }
}

在控制台即可见服务提供方方法的调用结果。
图片描述


追寻
317 声望20 粉丝

混迹杭州 朝全栈的方向努力着...