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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 测试用例8大要素及额外10小要素【建议收藏】[通俗易懂]

    测试用例8大要素及额外10小要素【建议收藏】[通俗易懂] 测试用例八大要素  1.测试用例编号  由字母、字符、数字组合而成的字符串,有唯一性,易识别性。  eg:  1)系统测试:产品编号-ST-系统测试项名-系统测试子项名-编号  2)集成测试:产品编号-IT-集成测试项名-集成测试子项名-编号  3)单元测试:产品编号-UT-单元测试项名-单元测试子项名-编号  这样看到编号就可以知道是做的什么测试,测试的对象是什么,也方便维护。  2.测试项目  当前测试用例所在测试用例所属大类、被测需求、被测模块、被测单元等。 

    2022年6月28日
  • gis地理加权回归步骤_地理加权回归权重

    gis地理加权回归步骤_地理加权回归权重内容导读1)回归概念介绍;2)探索性回归工具(解释变量的选择)使用;3)广义线性回归工具(GLR)使用;*加更:广义线性回归工具的补充内容4)地理加权回归工具(GWR)使用+小结。说明:本节是这个学习笔记最后一部分。PART/04地理加权回归工具(GWR)使用上一节我们讲了GLR广义线性回归,它是一种全局模型,可以构造出最佳描述研究区域中整体数据关系的方程。如果这些关系在研究区域中是一致的,则GLR回归方程可以对这些关系进行很好的建模。不过,当这些关系在研

    2022年10月6日
  • android之 Activity跳转出现闪屏

    android之 Activity跳转出现闪屏属于个人开发小知识应用Activity之间相互跳转时可能会出现闪屏现象原因:由于finish原因,网传,在onPause()里延迟3s使用finish。解决方案:方案一://我的解决方案,自定义style然后找到对应的Activity进行设置<stylename=”Theme”parent=”android:Theme”><itemname=…

    2022年5月11日
  • 大数据篇:三大指标

    大数据篇:三大指标大数据篇:三大指标上一篇文章中文章讲了如何用服务等级协议(SLA)来评估我们的系统,并讲解了几个常用的SLA指标今天我们来讲分布式系统中另外几个基本概念可扩展性(Scalability)先从我们为什么需要分布式系统说起。原因是我们系统的数据量越来越大,从原来的GB到TB到现在的PB级,单机已经无法胜任这样的工作了。工作中也常有这样的场景,随着业务变得原来越复杂,之前设计的系统无法处理日渐…

    2022年5月10日
  • zoj1204「建议收藏」

    zoj1204「建议收藏」也是这个星期写的了然而zoj识别不聊to_string只好先贴上来#include#include#includeusingnamespacestd;intnum[30],a,b,f;intall;intadd(intx,ints,intn,stringstr);intmain(){ cin>>a; for(inti=0;i

    2022年7月12日
  • 人脸表情识别系统介绍——上篇(python实现,含UI界面及完整代码)

    人脸表情识别系统介绍——上篇(python实现,含UI界面及完整代码)摘要:这篇博文介绍基于深度卷积神经网络实现的人脸表情识别系统,系统程序由Keras,OpenCv,PyQt5的库实现,训练测试集采用fer2013表情库。如图系统可通过摄像头获取实时画面并识别其中的人脸表情,也可以通过读取图片识别,本文提供完整的程序文件并详细介绍其实现过程。博文要点如下:表情识别数据集、搭建表情识别的模型、数据增强的批量训练、系统UI界面的实现。点击跳转至博文涉及的全部文件下载页。

    2022年4月29日

发表回复

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

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