dubbo系列(一)「建议收藏」

dubbo系列(一)「建议收藏」dubbo系列(一)

大家好,又见面了,我是你们的朋友全栈君。

进入官网之后,找到

http://dubbo.apache.org/en-us/docs/user/quick-start.html

有一个链接跳转到这里

http://dubbo.apache.org/en-us/docs/admin/install/provider-demo.html

使用git将项目下载下来

dubbo系列(一)「建议收藏」

修改如下Service实现类

dubbo系列(一)「建议收藏」

 1 /*
 2  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  * contributor license agreements.  See the NOTICE file distributed with
 4  * this work for additional information regarding copyright ownership.
 5  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  * (the "License"); you may not use this file except in compliance with
 7  * the License.  You may obtain a copy of the License at
 8  *
 9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.dubbo.demo.provider;
18 
19 import org.apache.dubbo.demo.DemoService;
20 import org.apache.dubbo.demo.TestForm;
21 import org.apache.dubbo.rpc.RpcContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.List;
30 
31 public class DemoServiceImpl implements DemoService {
32     private static Logger logger= LoggerFactory.getLogger(DemoServiceImpl.class);
33     @Override
34     public String sayHello(String name) {
35         System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
36         return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
37     }
38 
39     @Override
40     public List<TestForm> tranForm(TestForm testForm) {
41         if(testForm==null){
42             return Collections.emptyList();
43         }
44         logger.info("当前在{} 执行",RpcContext.getContext().getLocalAddress());
45         List<TestForm> testFormList=new ArrayList<>(1);
46         testFormList.add(testForm);
47         return testFormList;
48     }
49 
50 }

tranForm()是我新增的,TestForm 是一个普通的实体类
public class TestForm implements Serializable{
    private boolean b;
    private String s;
    private Integer i;
    private BigDecimal bigDecimal;
    private  Double d;
//这里省略get set方法 构造方法
}

修改dubbo-demo/dubbo-demo-consumer/的Consumer

 1 /*
 2  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  * contributor license agreements.  See the NOTICE file distributed with
 4  * this work for additional information regarding copyright ownership.
 5  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  * (the "License"); you may not use this file except in compliance with
 7  * the License.  You may obtain a copy of the License at
 8  *
 9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.dubbo.demo.consumer;
18 
19 import com.alibaba.fastjson.JSON;
20 import org.apache.dubbo.demo.DemoService;
21 import org.apache.dubbo.demo.TestForm;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.context.support.ClassPathXmlApplicationContext;
25 
26 import java.math.BigDecimal;
27 import java.util.List;
28 
29 public class Consumer {
30     private static Logger logger= LoggerFactory.getLogger(Consumer.class);
31 
32     /**
33      * To get ipv6 address to work, add
34      * System.setProperty("java.net.preferIPv6Addresses", "true");
35      * before running your application.
36      */
37     public static void main(String[] args) {
38         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
39         context.start();
40         DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
41         int i=10;
42         while (i-->0) {
43             try {
44                 List<TestForm> testFormList = demoService.tranForm(new TestForm(true,"s",i,new BigDecimal(99),null)); // call remote method
45                 logger.info("序号:{}:返回内容:{}",i,JSON.toJSON(testFormList)); // get result
46             } catch (Throwable throwable) {
47                 throwable.printStackTrace();
48             }
49         }
50     }
51 }

 

修改 <dubbo:protocol name=”dubbo” port=”端口号”/> 运行Provider中的main方法 ,依次启动三个provider服务,端口号分别是 20880,20881,20882

dubbo系列(一)「建议收藏」

运行Consumer中main方法,查看日志可以看出,Consumer分布式调用Provider已经成功了

dubbo系列(一)「建议收藏」

dubbo系列(一)「建议收藏」

 

 这个demo中,一共有三个模块

dubbo-demo-api 定义Service接口

dubbo-demo-consumer 传递实参调用Service

dubbo-demo-provider 定义Service实现类

        在consumer配置文件中,没有定义DemoService的实现类(文件路径:dubbo-demo\dubbo-demo-consumer\src\main\resources\META-INF\spring\dubbo-demo-consumer.xml)

而与DemoService相关的有这样的一个配置,我猜是dubbo创建了DemoService的bean并且放到了spring容器里,下面证实一个我的猜想:

 <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>

通过日志输入demoService的类型

logger.info("demoService实际类型:{}",demoService.getClass().getName());

[28/09/18 11:29:58:058 CST] main  INFO consumer.Consumer: demoService实际类型:org.apache.dubbo.common.bytecode.proxy0

打开 org.apache.dubbo.common.bytecode.Proxy 类,可以看到代理就是在这里创建的,继承了org.apache.dubbo.common.bytecode.Proxy抽象类

dubbo系列(一)「建议收藏」

 

那么这个代理有什么用呢?未完待续

 

转载于:https://www.cnblogs.com/LDDXFS/p/9719177.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107286.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【云原生】Docker镜像和容器的导入导出及常用命令

    【云原生】Docker镜像和容器的导入导出及常用命令本文是对Docker的镜像和容器的一些操作指令的汇总,比如镜像和容器的导入导出,以及一些在使用过程中的常用命令。

    2022年9月5日
    3
  • 基于arduino的光控窗帘_基于Arduino系统的智能窗帘设计与实现.doc

    基于arduino的光控窗帘_基于Arduino系统的智能窗帘设计与实现.doc摘要:跟随社会发展的潮流,现代科学技术正处于快速发展阶段,人们对智能家居的关注度也越来越高,人们开始寻求更加智能和舒适的生活及办公环境。智能遥控属于电子与信息工程的一个重要分支,在现代智能家居中有着良好的发展前景。本设计采用Arduino单片机来控制智能窗帘系统,实时监测室内温湿度情况并在LCD上显示,使用了红外遥控的技术,可以切换不同的工作模式从而来切换其控制方式,实现半自动控制、自动控制以及远…

    2022年6月23日
    29
  • 从char 数据类型到smalldatetime 数据类型的转换导致smalldatetime 值越界

    从char 数据类型到smalldatetime 数据类型的转换导致smalldatetime 值越界
    SQL:
    select*fromdbo.pds_operation_log  where(plan_code=12andcreate_timebetween’1900-01-01’and’2098-12-31′)orderbycreate_time asc
     
    出错:
    消息296,级别16,状态3,第1行
    从char数据类型到smalldatetime数据类型的转换导致smalldatetime值越界。

    2022年5月19日
    38
  • switch中的continue和break区别[通俗易懂]

    switch中的continue和break区别[通俗易懂]今天c程设期末考试,突然有一道选择题是关于switch中的break和continue问题。若switch外部没有循环,则break和continue没有区别。若switch外部还有循环,{一.若break,continue在switch外部,则二者作用的是外部循环。二.若break,continue在switch内部,则break作用于switch,continue作用于外部循环。…

    2022年9月12日
    1
  • java interface接口和多继承[通俗易懂]

    java interface接口和多继承[通俗易懂]很长时间不能很好解释多继承,今天看到一个特别好的解释,大快! 以下是引用:我认为你好像是不明白接口怎么用?也就是不明白为什么要定义那么多接口然后再用类去一个个继承他们。我跟你举个游戏的例子吧:这里有一个游戏,人猿泰山。主角是一个单独的类,这里我们主要用怪物说明接口的用法:怪物有很多种,按地域分:有的在天上飞,有的在地上跑,有的在水里游按攻击方式分:有的能近距离物理攻击,有的能

    2022年7月16日
    14
  • mysql 如何修改用户密码_如何更改MySQL用户密码

    mysql 如何修改用户密码_如何更改MySQL用户密码在本教程中,我们将向您展示如何更改MySQL用户密码。这些说明应适用于任何现代Linux发行版,例如Ubuntu18.04和CentOS7。先决条件根据系统上运行的MySQL或MariaDB服务器版本,您将需要使用不同的命令来更改用户密码。您可以通过发出以下命令来找到数据库服务器版本:mysql–version如果您的系统中安装了MySQL,则输出将类似于以下内容:mysqlVer14…

    2022年6月17日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号