dubbo使用的三种配置

一. 准备注册中心

  1. dubbo的注册中心在生产环境中,一般都会选择 ZooKeeper
    下载 ZooKeeper ZooKeeper_3.4.14下载地址
  2. 启动ZK
# 解压安装包
tar -zxvf zookeeper-3.4.14.tar.gz# 进入安装目录,
cp conf/zoo_sample.cfg conf/zoo.cfg# 启动ZK
./bin/zkServer.sh start

二. 根据 xml 配置

1. Provider 准备

一. 引入依赖
        <!-- 引入公共接口 --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-demo-interface</artifactId><version>${project.parent.version}</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-multicast</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-zookeeper</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-configcenter-zookeeper</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-configcenter-nacos</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-metadata-report-nacos</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-metadata-report-zookeeper</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-rpc-dubbo</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-config-spring</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-remoting-netty4</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-serialization-hessian2</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-qos</artifactId></dependency>
二. 服务接口

这个接口需要被 Provider 中的服务具体实现,同时也能被 Consumer 引用,所有可以考虑将所有 dubbo中的接口封装在一个二方包中,这样 Provider, Consumer 引入同一个包即可。

package org.apache.dubbo.demo;import java.util.concurrent.CompletableFuture;public interface DemoService {String sayHello(String name);default CompletableFuture<String> sayHelloAsync(String name) {return CompletableFuture.completedFuture(sayHello(name));}}
三. 编写service
package org.apache.dubbo.demo.provider;import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.rpc.RpcContext;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.concurrent.CompletableFuture;public class DemoServiceImpl implements DemoService {private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);@Overridepublic String sayHello(String name) {logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();}@Overridepublic CompletableFuture<String> sayHelloAsync(String name) {return CompletableFuture.supplyAsync(() -> "async result");}
}
四. 在xml中配置 service
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"><dubbo:application metadata-type="remote" name="demo-provider"/><dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/><dubbo:registry address="zookeeper://127.0.0.1:2181"/><dubbo:protocol name="dubbo"/><bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/><dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/></beans>
五. 启动 Provider
package org.apache.dubbo.demo.provider;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Application {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");context.start();System.in.read();}
}

2. Consumer 准备

一. 引入依赖
        <dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-metadata-report-zookeeper</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-demo-interface</artifactId><version>${project.parent.version}</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-multicast</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-zookeeper</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-configcenter-zookeeper</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-configcenter-nacos</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-metadata-report-nacos</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-config-spring</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-rpc-dubbo</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-remoting-netty4</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-serialization-hessian2</artifactId></dependency>
二. 在xml中配置 service

因为已经引入了 interface 包
可以直接配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"><dubbo:application name="demo-consumer"/><dubbo:registry address="zookeeper://127.0.0.1:2181"/><dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/></beans>
三. 启动 Consumer
package org.apache.dubbo.demo.consumer;import org.apache.dubbo.demo.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.concurrent.CompletableFuture;public class Application {/*** In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before* launch the application*/public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");context.start();DemoService demoService = context.getBean("demoService", DemoService.class);String world = demoService.sayHello("world");System.out.println(">>>>>>>>>>>>>>result: " + world);}
}

结果:

在这里插入图片描述

三. 基于注解配置

依赖引入同上,或者参考官方文档

1. Provider 准备

一. 实现接口
package org.apache.dubbo.demo.provider;import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.rpc.RpcContext;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.concurrent.CompletableFuture;@Service
public class DemoServiceImpl implements DemoService {private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);@Overridepublic String sayHello(String name) {logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();}@Overridepublic CompletableFuture<String> sayHelloAsync(String name) {return null;}}
二. 通过注解实现配置
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.provider")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static class ProviderConfiguration {@Beanpublic RegistryConfig registryConfig() {RegistryConfig registryConfig = new RegistryConfig();registryConfig.setAddress("zookeeper://127.0.0.1:2181");return registryConfig;}
}

classpath:/spring/dubbo-provider.properties文件

dubbo.application.name=dubbo-demo-annotation-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

2. Consumer 准备

一. 创建接口
package org.apache.dubbo.demo.consumer.comp;import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.demo.DemoService;import org.springframework.stereotype.Component;import java.util.concurrent.CompletableFuture;@Component("demoServiceComponent")
public class DemoServiceComponent implements DemoService {@Referenceprivate DemoService demoService;@Overridepublic String sayHello(String name) {return demoService.sayHello(name);}@Overridepublic CompletableFuture<String> sayHelloAsync(String name) {return null;}
}
二. 通过注解添加配置
    @Configuration@EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.consumer.comp")@PropertySource("classpath:/spring/dubbo-consumer.properties")@ComponentScan(value = {"org.apache.dubbo.demo.consumer.comp"})static class ConsumerConfiguration {}

spring/dubbo-consumer.properties

dubbo.application.name=dubbo-demo-annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181

消费结果

在这里插入图片描述

四. 基于API的配置

不依赖于 spring框架。详情见dubbo-demo-api

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/323122.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

(湖科大教书匠)计算机网络微课堂(下)

第四章、网络层 网络层概述 网络层主要任务是实习网络互连&#xff0c;进而实现数据包在各网络之间的传输 因特网使用TCP/IP协议栈 由于TCP/IP协议栈的网络层使用网际协议IP&#xff0c;是整个协议栈的核心协议&#xff0c;因此TCP/IP协议栈的网络层常称为网际层 网络层提供…

【小白专用】PHP识别是电脑或手机访问网站

现在通过手机访问网站越来越流行了&#xff0c;如果我们希望统计一下网站通过pc&#xff0c;手机移动端的各自访问量的情况&#xff0c;或者需要为手机移动端做一些特别的处理的话&#xff0c;那么我们就需要对访问网站的用户的客户端做一下鉴别了&#xff0c;下面这个实例就是…

Apache网页优化

本章主要介绍如何对Apache网页进行优化 Apache 网页压缩Apache 网页缓存Apache 隐藏版本信息Apache 网页防盗链 目录 1、网页压缩与缓存 1.1、网页压缩 &#xff08;1&#xff09;gzip介绍 &#xff08;2&#xff09;HTTP压缩的过程 &#xff08;3&#xff09;Apache的…

记一次:职业规划与心灵成长

前言&#xff1a;csdn组织了2023年的年度征文&#xff0c;为了响应号召&#xff08;嘘~其实为了那个铭牌&#xff09;&#xff0c;加上这么多年了也该总结一下了&#xff0c;就写了这么一篇&#xff08;公司正好让写年终总结&#xff0c;巧了么这不是&#xff09; 一、感谢部分…

机器学习:手撕 AlphaGo(二)

计算机下围棋的问题描述请见上篇&#xff1a;机器学习&#xff1a;手撕 AlphaGo&#xff08;一&#xff09;-CSDN博客 3. MCTS 算法介绍 MCTS&#xff08;Monte Carlo Tree Search&#xff09; 算法的中文名称叫做蒙特卡洛树搜 索。第一次接触这个算法时&#xff0c;便惊叹于它…

解决:ModuleNotFoundError: No module named ‘zhon’

解决&#xff1a;ModuleNotFoundError: No module named ‘zhon’ 文章目录 解决&#xff1a;ModuleNotFoundError: No module named zhon背景报错问题报错翻译报错位置代码报错原因解决方法方法一&#xff0c;直接安装方法二&#xff0c;手动下载安装方法三&#xff0c;编译安…

知识图谱 vs GPT

简介&#xff1a; 当我们谈论知识图谱时&#xff0c;我们指的是一种结构化的知识表示形式&#xff0c;是一种描述真实世界中事物及其关系的语义模型&#xff0c;用于描述实体之间的关系。它通过将知识组织成图形结构&#xff0c;提供了一种更全面、准确和智能的信息处理方式。知…

使用Go语言的HTTP客户端进行并发请求

Go语言是一种高性能、简洁的编程语言&#xff0c;它非常适合用于构建并发密集型的网络应用。在Go中&#xff0c;标准库提供了强大的HTTP客户端和服务器功能&#xff0c;使得并发HTTP请求变得简单而高效。 首先&#xff0c;让我们了解为什么需要并发HTTP请求。在许多应用场景中…

私有化部署你的甘特图协作工具

安装 首先去官网 https://zz-plan.com/deploy 下载对应的版本 arm是对应m1 m2 m3的mac amd是老的intel处理器 准备工作 安装mysql zz-plan需要依赖mysql 生成token 解压下载的压缩包 创建token./zz-plan -c 复制创建的token去获取授权码&#xff0c;点击获取免费授权码 …

安全测试之SSRF请求伪造

前言 SSRF漏洞是一种在未能获取服务器权限时&#xff0c;利用服务器漏洞&#xff0c;由攻击者构造请求&#xff0c;服务器端发起请求的安全漏洞&#xff0c;攻击者可以利用该漏洞诱使服务器端应用程序向攻击者选择的任意域发出HTTP请求。 很多Web应用都提供了从其他的服务器上…

限制选中指定个数CheckBox控件(1/2)

限制选中指定个数CheckBox控件&#xff08;1/2&#xff09; 实例需求&#xff1a;工作表中有8个CheckBox控件&#xff08;下文中简称为控件&#xff09;&#xff0c;现在需要实现限制用户最多只能勾选4个控件。 Dim OnDic As Object Sub CheckboxeEvent()Dim oCB As CheckBox…

微信小程序+前后端开发学习材料

目录结构 全局文件 1.app.json 文件 用来对微信小程序进行全局配置&#xff0c;决定页面文件的路径、窗口表现、设置网络超时时间、设置多 tab 等。文件内容为一个 JSON 对象。 1.1 page用于指定小程序由哪些页面组成&#xff0c;每一项都对应一个页面的 路径&#xff08;含文…