dubbo 起步

2019-07-27  本文已影响0人  dayeshisir

dubbo介绍

dubbo 是由阿里巴巴于2009年开始在内部使用,在经过内部多个版本的演化后在2012年开源的一款高性能高可用的高性能、轻量级的开源Java RPC框架。开源后虽然得到了很多互联网公司的普遍使用,但还是于2014年10月30号发布2.4.11版本后停止维护,即便如此业界对Dubbo的使用热情并没有受到太大影响,各自维护和演化自己的内部版本。在停止维护3年后的2017年9月7日Dubbo又开始更新,并且在3个月时间内连续发布了4个维护版本,最终于2018年2月15日成为了Apache基金会孵化项目

dubbo本地安装

dubbo的注册中心是依赖zookeeper,所以本地安装包含zookeeper的按照和配置;

zookeeper 安装

// 解压
tar -xzvf apache-zookeeper-3.5.5.tar.gz

// 重命名
mv apache-zookeeper-3.5.5 zookeeper

// 配置
mv zoo_sample.cfg zoo.cfg

zoo.cfg的默认配置就可以,不需要修改

./bin/zkServer.sh start
// 连接本地zookeeper
./bin/zkCli.sh

// 查看跟节点
ls /
image.png

[备注:这是启动dubbo后的截图,可以看到有dubbo节点]

dubbo的安装

// 下载dubbo-admin代码
git clone https://github.com/apache/dubbo-admin.git

// 编译代码
mvn clean package

// 启动代码
mvn --projects dubbo-admin-server spring-boot:run

dubbo-admin的按照,git上有详细介绍,按照提示来即可;

这里,走了点弯路,其他安装资料说的步骤和这个有点出入,可能是版本问题吧;
其次,这里是要下载dubbo-admin分支,不要下错为dubbo分支代码

安装好了之后,启动浏览器的http://localhost:8080/#/service就可以看到久违、熟悉的界面了

image.png

dubbo 示例

下面来写个实例,尝试下dubbo的魅力

服务提供者

package com.shisir.service;

public interface GreetingService {
    String sayHello(String name);
}
package com.shisir.service.impl;

import com.shisir.service.GreetingService;

public class GreetingServiceImpl implements GreetingService {
    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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <bean id="demoService" class="com.shisir.service.impl.GreetingServiceImpl"/>

    <dubbo:application name="provider"/>

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

    <dubbo:protocol name="dubbo" port="20881"/>
    
    <dubbo:service interface="com.shisir.service.GreetingService" ref="demoService"/>

</beans>
package com;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class DubboProvider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");

        context.start();

        System.in.read();
    }
}

可以看到服务如期起来了

可以看到有刚注册的服务,也有一些节点的属性信息,不做具体展开

服务消费者

<?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.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="consumer"/>

    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <dubbo:reference  id="demoService"  interface="com.shisir.service.GreetingService"/>

</beans>
package com;

import com.shisir.service.GreetingService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class DubboConsumer {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");

        context.start();

        GreetingService demoService = (GreetingService)context.getBean("demoService");

        String hello = demoService.sayHello("shiyibo");

        System.out.println(hello);

        System.in.read();
    }
}

结束语

参考

上一篇 下一篇

猜你喜欢

热点阅读