idea搭建Dubbo入门案例

1、什么是Dubbo,这里我们引用官网的说明

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

2、Dubbo的优势是什么

  1. 提供高性能的基于代理的远程调用能力,服务以接口为粒度,为开发者屏蔽远程调用底层细节。
  2. 内置多种负载均衡策略,智能感知下游节点健康状况,显著减少调用延迟,提高系统吞吐量。
  3. 支持多种注册中心服务,服务实例上下线实时感
  4. 遵循微内核+插件的设计原则,所有核心能力如Protocol、Transport、Serialization被设计为扩展点,平等对待内置实现和第三方实现。
  5. 内置条件、脚本等路由策略,通过配置不同的路由规则,轻松实现灰度发布,同机房优先等功能。
  6. 提供丰富服务治理、运维工具:随时查询服务元数据、服务健康状态及调用统计,实时下发路由策略、调整配置参数。

3、官网提供了我们的实例地址 Dubbo实例地址,可以直接使用,接下来是手动搭建的过程

  1. 配置注册中心。dubbo提供了多种配置中心,但是官网推荐我们使用zookeeper,所以我们这边也使用的zookeeper,这里安装过程不做描述。
  2. 创建3个maven项目,接口信息、服务的提供者、服务的消费者。结构如下在这里插入图片描述
    interface的pom文件内容:
	<?xml version="1.0" encoding="UTF-8"?>
	<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">
	    <parent>
	        <artifactId>com-dubbo-demo</artifactId>
	        <groupId>com.weicc.demo</groupId>
	        <version>1.0-SNAPSHOT</version>
	    </parent>
	    <modelVersion>4.0.0</modelVersion>
	
	    <artifactId>common-base-interface</artifactId>
	
	</project>

provide的pom文件内容,增加dubbo和zookeeper的依赖,因为Dubbo从2.3版本以后开始支持可选 curator 实现。Curator 是 Netflix 开源的一个 Zookeeper 客户端实现,所以我们这边引入了curator

	<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>com-dubbo-demo</artifactId>
        <groupId>com.weicc.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>com.orderMenu.provide</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.weicc.demo</groupId>
            <artifactId>common-base-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>


</project>

consume的pom文件内容,也增加了Dubbo和zookeeper的依赖

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>com-dubbo-demo</artifactId>
        <groupId>com.weicc.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>com-orderMenu-consume</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.weicc.demo</groupId>
            <artifactId>common-base-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.5</version>
        </dependency>
    </dependencies>

</project>
  1. 配置文件
    下面展示 provide配置文件
	<?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-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
		
		    <!-- 提供方应用信息,用于计算依赖关系 -->
		    <dubbo:application name="com-orderMenu-provide"  />
		
		    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
		    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
		
		    <!-- 用dubbo协议在20880端口暴露服务 -->
		    <dubbo:protocol name="dubbo" port="20880" />
		
		    <!-- 声明需要暴露的服务接口 -->
		    <dubbo:service interface="userInfo.GetUserInfo" ref="getOrderInfoImpl" />
		
		    <!-- 和本地bean一样实现服务 -->
		    <bean id="getOrderInfoImpl" class="order.GetOrderInfoImpl" />
		</beans>

下面展示 consume配置文件

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

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="com-orderMenu-consume"  />

    <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="userInfo"  interface="userInfo.GetUserInfo" check="false"/>
</beans>

基本的配置信息就这么多,剩下的一些都是接口的定义和实现了,相信聪明的你肯定能够把该Demo跑起来的。自己实在跑不起来,按照渣男的话说;我都这么做了,你还跑不起来,我能怎么办呀。
不过可别忘记开头的官网demo,他会给你参考的,一起加油!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值