springboot整合apache dubbo

Apache Dubbo 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

ZooKeeper安装启动

下载地址:http://zookeeper.apache.org/releases.html

下载完毕后解压缩,在zookeeper的conf目录下 把 zoo_sample.cfg复制一份重命名为 zoo.cfg ,然后只想 bin 目录下的 zkServer.cmd
tickTime=2000 
initLimit=10 
syncLimit=5 
#这个参数可以改为自己的想要的位置
dataDir=../data 
#zookeeper的启动端口
clientPort=2181 

dubbo-admin安装启动

源码地址:https://github.com/apache/dubbo-admin

通过git clone 来回来源码自己编译,mvn clean package , 
编译之前需要改一下配置 dubbo-admin\dubbo-admin-server\src\main\resources\application.properties
#如果要换启动端口可以在这设置
server.port=8081
# centers in dubbo2.7
admin.registry.address=zookeeper://127.0.0.1:2181
admin.config-center=zookeeper://127.0.0.1:2181
admin.metadata-report.address=zookeeper://127.0.0.1:2181

#group
admin.registry.group=dubbo
admin.config-center.group=dubbo
admin.metadata-report.group=dubbo

admin.apollo.token=e16e5cd903fd0c97a116c873b448544b9d086de9
admin.apollo.appId=test
admin.apollo.env=dev
admin.apollo.cluster=default
admin.apollo.namespace=dubbo
编译完执行 jave -jar dubbo-admin\dubbo-admin-distribution\target\dubbo-admin-0.1.jar
访问本地 dubbo-admin 

在这里插入图片描述

dubbo服务提供者

  1. 引入依赖
		<dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.3</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
  1. application配置
server:
  port: 9080

spring:
  application:
    name: dubbo-p

dubbo:
  scan:
    base-packages: me.zingon.test.dubbop
  protocol:
    name: dubbo
    port: -1
  registry:
    address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
    file: ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
  metadata-report:
    address: zookeeper://127.0.0.1:2181

embedded:
  zookeeper:
    port: 2181
  1. 创建接口(此接口dubbo消费者也需要使用,最好统一写在一个模块内)
public interface TestService{  
    Object getTest(String id);
} 
  1. 创建接口实现(注意 Service注解要用dubbo的org.apache.dubbo.config.annotation.Service)


import me.zingon.test.interfac.TestService;
import org.apache.dubbo.config.annotation.Service;

import java.util.HashMap;
import java.util.Map;
//注意这个注解,可以通过此注解设置相关参数,如超时,重试次数。。。
@Service
public class TestServiceImpl implements TestService {
    @Override
    public Object getTest(String id) {
        Map<String,Object> result=new HashMap<>();
        result.put("id",id);
        result.put("test","this is dubbop");
        return result;
    }
}
  1. 启动

     此dubbo服务提供者已完成,只需要启动此sproogboot项目,在dubbo-admin中即可看到此服务
    

在这里插入图片描述
点击测试,即可通过dubbo测试此服务
测试成功

dubbo服务消费者

  1. 引入依赖,跟上一步一样
  2. application配置
spring:
  application:
    name: dubbo-c 
embedded:
  zookeeper:
    port: 2181 
dubbo:
  registry:
    address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
    file: ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
  1. 创建接口(引入上一步创建的接口)
  2. 调用此接口
import me.zingon.test.interfac.TestService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

@Service
public class CallService {
	//通过此注解注入 dubbo 服务提供者的接口
    @Reference
    private TestService testService;

    @PostConstruct
    public void test(){
        Map<String,Object> result= (Map<String, Object>) testService.getTest("asdasd");
        System.out.println(result.size());
        result.forEach((k,v)->{
            System.out.println(k+":"+v);
        });
    }

}
/*
最后执行结果:
2
test:this is dubbop
id:asdasd
*/

总结

通过starter+注解的方式使用 dubbo 非常方便简洁

posted @ 2019-12-12 08:56  A_yes  阅读(1086)  评论(0编辑  收藏  举报