dubbo简单示例

dubbo简单示例

2019-09-06

 

1 Zookeeper注册中心的搭建(windows单机)

  1. 下载zookeeper压缩包并解压到 D:\zookeeper\apache-zookeeper-3.5.5-bin,官网地址:http://www.apache.org/dyn/closer.cgi/zookeeper/
  2. 进入conf目录将 zoo_sample.cfg 改名为 zoo.cfg。
    默认配置如下:
    # The number of milliseconds of each tick
    tickTime=2000
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=5
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=2
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=D:\\zookeeper\\data
    dataLogDir=D:\\zookeeper\\log
    # the port at which the clients will connect
    clientPort=2181
    # the maximum number of client connections.
    # increase this if you need to handle more clients
    #maxClientCnxns=60
    #
    # Be sure to read the maintenance section of the 
    # administrator guide before turning on autopurge.
    #
    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    #
    # The number of snapshots to retain in dataDir
    #autopurge.snapRetainCount=3
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    #autopurge.purgeInterval=1
    View Code
  3. 进入bin目录双击 zkServer.cmd,若启动成功,则windows单机版zookeeper搭建成功

2 dubbo-admin安装与使用

dubbo-admin git: https://github.com/apache/incubator-dubbo-ops下载。

注意:最新的dubbo-admin集成了springboot,可以打包为jar运行。

也可以从 https://gitee.com/jingyang3877/all-examples 直接下载jar包

执行以下命令打jar包:

mvn package -DskipTests=true

在jar包目录下,执行以下命令启动

java -jar dubbo-admin-xxx.jar

注意:dubbo-admin的端口默认是7001,dubbo注册中心的地址是本机的2181端口

 

图1 dubbo管理

 

Socket通信原理

如何实现一个简单的RPC

最全最简单的dubbo教程-开篇《一》

服务之间的调用之RPC、Restful深入理解

面试问题:REST与RPC区别?

Zookeeper注册中心的搭建-windows单机

dubbo-admin安装与使用

 

转载于:https://www.cnblogs.com/Ming8006/p/11328962.html

以下是一个简单Dubbo 网络示例: 1. 创建一个接口 ```java public interface HelloWorldService { String sayHello(String name); } ``` 2. 实现接口 ```java public class HelloWorldServiceImpl implements HelloWorldService { public String sayHello(String name) { return "Hello, " + name; } } ``` 3. 配置 Dubbo ```xml <!-- 配置服务提供方应用名 --> <dubbo:application name="hello-world-service-provider"/> <!-- 配置 Dubbo 协议端口,缺省为 20880 --> <dubbo:protocol name="dubbo" port="12345"/> <!-- 配置服务提供者 --> <dubbo:service interface="com.example.service.HelloWorldService" ref="helloWorldServiceImpl"/> <!-- 配置服务实现 --> <bean id="helloWorldServiceImpl" class="com.example.service.impl.HelloWorldServiceImpl"/> ``` 4. 启动服务提供者 ```java public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read(); // 按任意键退出 } } ``` 5. 配置 Dubbo 消费者 ```xml <!-- 配置服务消费者应用名 --> <dubbo:application name="hello-world-service-consumer"/> <!-- 配置 Dubbo 协议端口,缺省为 20880 --> <dubbo:protocol name="dubbo" port="12346"/> <!-- 配置服务提供者的地址 --> <dubbo:reference id="helloWorldService" interface="com.example.service.HelloWorldService" url="dubbo://localhost:12345"/> ``` 6. 调用服务 ```java public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); HelloWorldService helloWorldService = (HelloWorldService) context.getBean("helloWorldService"); String result = helloWorldService.sayHello("dubbo"); System.out.println(result); } } ``` 以上就是一个简单Dubbo 网络示例。通过 Dubbo,我们可以轻松地实现分布式服务的调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值