Dubbo入门示例原生API版_原生api实现dubbo调用-程序员宅基地

技术标签: zookeeper  原生api  dubbo  Dubbo  bootstrap  

有关dubbo的基础、架构等介绍请参考之前博客:Dubbo背景及架构简介
XML版(官方推荐)示例请参考:

1. 创建暴露服务模块(dubbo-demo-api)

本模块下没有实际的业务逻辑,主要是定义提供者和消费者公用服务接口

/**
 * 需要暴露出去的服务
 *
 */
public interface HelloService {
    
	/**
	 * say hello
	 * 
	 * @param name
	 * @return
	 */
	String sayHello(String name);

}

2. 创建服务提供者和消费者父模块管理和引入公用依赖

依赖版本管理在父工程(dubbo-demo),采用dubbo-2.7.5
和xml或者注解版本不同的是不需要依赖spring

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.qqxhb</groupId>
		<artifactId>dubbo-demo</artifactId>
		<version>0.0.1</version>
	</parent>
	<artifactId>dubbo-api-demo</artifactId>
	<packaging>pom</packaging>
	<name>dubbo-api-demo</name>

	<dependencies>
		<!-- 引入Dubbo依赖,原生api不需要依赖spring -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-context</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 使用zookeeper作为注册中心 -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<!-- 使用curator作为zookeeper客户端 -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
		</dependency>
		<!-- 引入需要暴露的服务 -->
		<dependency>
			<groupId>com.qqxhb</groupId>
			<artifactId>dubbo-demo-api</artifactId>
			<version>0.0.1</version>
		</dependency>
	</dependencies>
	<modules>
		<module>dubbo-api-demo-consumer</module>
		<module>dubbo-api-demo-provider</module>
	</modules>
</project>
3. 创建服务提供者

1).创建dubbo-api-demo-provider模块
2).编写服务实现类和启动类(两种启动方式)

1).创建dubbo-api-demo-provider模块

<?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>
		<groupId>com.qqxhb</groupId>
        <artifactId>dubbo-api-demo</artifactId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-api-demo-provider</artifactId>

</project>

2).编写服务实现类(HelloServiceImpl)和启动类(BootstrapProviderApplication及ExportProviderApplication)

package com.qqxhb.api.provider.impl;

import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qqxhb.demo.api.HelloService;

public class HelloServiceImpl implements HelloService {
    
	private static final Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);

	public String sayHello(String name) {
    
		logger.info("========Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
		return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
	}
}

package com.qqxhb.api.provider;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qqxhb.api.provider.impl.HelloServiceImpl;
import com.qqxhb.demo.api.HelloService;

/**
 * 
 * 通过DubboBootstrap方式启动服务
 *
 */
public class BootstrapProviderApplication {
    
	private static final Logger logger = LoggerFactory.getLogger(ExportProviderApplication.class);

	public static void main(String[] args) throws Exception {
    
		ServiceConfig<HelloServiceImpl> service = new ServiceConfig<>();
		service.setInterface(HelloService.class);
		service.setRef(new HelloServiceImpl());

		DubboBootstrap bootstrap = DubboBootstrap.getInstance();
		bootstrap.application(new ApplicationConfig("dubbo-api-demo-provider"))
				.registry(new RegistryConfig("zookeeper://127.0.0.1:2181")).service(service).start();
		logger.info("dubbo service started......");
		bootstrap.await();
	}
}

package com.qqxhb.api.provider;

import java.util.concurrent.CountDownLatch;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qqxhb.api.provider.impl.HelloServiceImpl;
import com.qqxhb.demo.api.HelloService;

/**
 * 
 * 通过服务导出的方式启动服务
 *
 */
public class ExportProviderApplication {
    
	private static final Logger logger = LoggerFactory.getLogger(ExportProviderApplication.class);

	public static void main(String[] args) throws Exception {
    
		ServiceConfig<HelloServiceImpl> service = new ServiceConfig<>();
		service.setInterface(HelloService.class);
		service.setRef(new HelloServiceImpl());

		service.setApplication(new ApplicationConfig("dubbo-api-demo-provider"));
		service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
		service.export();
		logger.info("dubbo service started......");
		new CountDownLatch(1).await();
	}
}

4. 创建服务消费者

1).创建dubbo-api-demo-consumer模块
2).消费者启动类(同样有两种方式)

1).创建dubbo-api-demo-consumer模块

<?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>
		<groupId>com.qqxhb</groupId>
        <artifactId>dubbo-api-demo</artifactId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-api-demo-consumer</artifactId>
</project>

2).消费者启动类(BootstrapConsumerApplication 、ExportConsumerApplication)

package com.qqxhb.api.consumer;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qqxhb.demo.api.HelloService;

/**
 * 
 * 通过DubboBootstrap方式启动服务
 *
 */
public class BootstrapConsumerApplication {
    
	private static final Logger logger = LoggerFactory.getLogger(BootstrapConsumerApplication.class);

	public static void main(String[] args) throws Exception {
    
		ReferenceConfig<HelloService> reference = new ReferenceConfig<>();
		reference.setInterface(HelloService.class);

		DubboBootstrap bootstrap = DubboBootstrap.getInstance();
		bootstrap.application(new ApplicationConfig("dubbo-api-demo-consumer"))
				.registry(new RegistryConfig("zookeeper://127.0.0.1:2181")).reference(reference).start();
		HelloService helloService = ReferenceConfigCache.getCache().get(reference);
		String message = helloService.sayHello("Dubbo Bootstrap");
		logger.info("============{}", message);

	}
}

package com.qqxhb.api.consumer;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qqxhb.demo.api.HelloService;

/**
 * 
 * 通过服务导出的方式调用服务
 *
 */
public class ExportConsumerApplication {
    
	private static final Logger logger = LoggerFactory.getLogger(BootstrapConsumerApplication.class);

	public static void main(String[] args) throws Exception {
    
		ReferenceConfig<HelloService> reference = new ReferenceConfig<>();
		reference.setApplication(new ApplicationConfig("dubbo-demo-api-consumer"));
		reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
		reference.setInterface(HelloService.class);
		HelloService service = reference.get();
		String message = service.sayHello("Dubbo Export");
		logger.info("============{}", message);
	}
}

5. 启动测试

1).依赖zookeeper作为注册中心,因此需要优先启动zookeeper
zookeeper相关知识请参考之前博客:Zookeeper入门及单机及集群环境搭建
2). 启动任意一个 ProviderApplication 、启动任意一个ConsumerApplication
ProviderApplication 端窗口打印日志======== result: Hello Dubbo Bootstrap, response from provider: 192.168.25.1:20880
ConsumerApplication 端窗口打印日志========Hello Dubbo Bootstrap, request from consumer: /192.168.25.1:7079

源码地址:https://github.com/qqxhb/dubbo-demo

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43792385/article/details/105240075

智能推荐

240320俄罗斯方块java,JAVA游戏编程之三----j2me 手机游戏入门开发--俄罗斯方块_2-程序员宅基地

文章浏览阅读202次。packagecode;//importjava.awt.*;//importjava.awt.Canvas;//importjava.awt.event.*;//importjavax.swing.*;importjava.util.Random;importjavax.microedition.lcdui.*;//写界面所需要的包/***//***俄罗斯方块*高雷*2007年1..._240×320java游戏

在线电影院售票平台(源码+开题报告)-程序员宅基地

文章浏览阅读779次,点赞14次,收藏19次。然后,实现系统的数据管理和服务功能,包括用户的注册与登录、电影的分类与展示、电影信息的查询与推荐、座位的选择与预订、在线支付与电子票生成等。此外,随着在线视频平台的兴起,越来越多的人选择在线观看电影,这对传统电影院产生了巨大的冲击。研究意义: 开发在线电影院售票平台对于提升用户的观影体验、优化电影院的运营效率、促进电影产业的发展具有重要的意义。该系统旨在通过技术手段解决传统电影院售票中的问题,提供一个集成化的电影信息展示、座位选择、在线支付和用户评价平台,同时也为电影院和电影制作方提供有效的工具。

程序员熬夜写代码,用C/C++打造一个安全的即时聊天系统!_基于c++的即时聊天系统设计-程序员宅基地

文章浏览阅读509次。保护我们剩下的人的通话信息安全,使用TOX可以让你在和家人,朋友,爱人交流时保护你的隐私不受政府无孔不入的的偷窥.关于TOX:其他牛逼的软件因为一些细化服务问你要钱的时候, TOX分文不取 . 你用了TOX, 想干嘛就干嘛.网友评论:项目源码展示:源码测试效果:最后,如果你学C/C++编程有什么不懂的,可以来问问我哦,或许我能够..._基于c++的即时聊天系统设计

linux Java服务swap分区被占用内存泄露问题故障及解决方法_linux swap占用很高-程序员宅基地

文章浏览阅读584次。鱼弦:CSDN内容合伙人、CSDN新星导师、全栈领域创作新星创作者 、51CTO(Top红人+专家博主) 、github开源爱好者(go-zero源码二次开发、游戏后端架构 https://github.com/Peakchen)当Java服务在Linux系统中运行时,可能会出现swap分区被占用的内存泄露问题,导致系统性能下降或者崩溃。下面是该问题的故障及解决方法、底层结构、架构图、工作原理、使用场景详解和实际应用方式、原理详细描述、相关命令使用示例以及文献材料链接。_linux swap占用很高

word中利用宏替换标点标点全角与半角-程序员宅基地

文章浏览阅读662次。Alt+F11,然后插入-模块:复制下面代码到编辑窗口:Sub 半角标点符号转换为全角标点符号()'中英互译文档中将中文段落中的英文标点符号替换为中文标点符号 Dim i As Paragraph, ChineseInterpunction() As Variant, EnglishInterpunction() As Variant Dim MyRange..._替换半角宏

Android WebView使用总结_android webview真正加载完成-程序员宅基地

文章浏览阅读2.8k次。#.简介: WebView是Android提供的用来展示展示web页面的View,内部使用webkit浏览器引擎(一个轻量级的浏览器引擎),除了展示Web页面外,还可与Web页面内的JS脚本交互调用。WebView内部的WebSetting对象负责管理WebView的参数配置; WebViewClient负责处理WebView的各种请求和通知事件,在对应事件发生时会执行WebViewClient的对应回调; ChromeWebviewClient辅助Webview处理与JS一些交互......_android webview真正加载完成

随便推点

bitcoin 调试环境搭建-程序员宅基地

文章浏览阅读1.6k次。_bitcoin 调试环境搭建

曲线生成 | 图解B样条曲线生成原理(基本概念与节点生成算法)-程序员宅基地

文章浏览阅读4.3k次,点赞93次,收藏94次。为了解决贝塞尔曲线无法局部修正、控制性减弱、曲线次数过高、不易拼接的缺陷,引入B样条曲线(B-Spline)。本文介绍B样条曲线的基本概念:节点向量、支撑性、次数阶数、加权性质、节点生成算法等,为后续曲线计算打下基础。_样条曲线生成

CDH安装宝典之ClouderaManager_/opt/cloudera/cm-agent/service/mgmt/mgmt.sh: line -程序员宅基地

文章浏览阅读902次。配置本地repo库下载我的阿里云盘文件文件放置#创建目录mkdir -p /opt/cloudera/parcel-repo/mkdir -p /opt/cloudera/cm/yum install createrepoCDH 6.2.0 的三个文件放到/opt/cloudera/parcel-repo/中,并且注意把sha256后缀的文件名修改为sha#执行createrepo命令生成rpm元数据 最终/opt/cloudera/parcel-repo/会多一个repodata目录_/opt/cloudera/cm-agent/service/mgmt/mgmt.sh: line 76: /usr/java/jdk1.8.0_181

uni.canvasToTempFilePath在app正常,微信小程序报错: fail canvas is empty-程序员宅基地

文章浏览阅读943次,点赞2次,收藏2次。uni.canvasToTempFilePath_uni.canvastotempfilepath

SDRAM笔记_sdram 干扰-程序员宅基地

文章浏览阅读3.1k次。SRAM :静态RAM,不用刷新,速度可以非常快,像CPU内部的cache,都是静态RAM,缺点是一个内存单元需要的晶体管数量多,因而价格昂贵,容量不大。DRAM:动态RAM,需要刷新,容量大。SDRAM:同步动态RAM,需要刷新,速度较快,容量大。DDR SDRAM:双通道同步动态RAM,需要刷新,速度快,容量大。........................_sdram 干扰

Excel转SQL语句_excel数据怎么生成sql语句-程序员宅基地

文章浏览阅读7.3k次。假设表格有A、B、C、D四列数据,希望导入到你的数据库中表格table,对应的字段分别是col1、col2、col3、col4。_excel数据怎么生成sql语句

推荐文章

热门文章

相关标签