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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • linux软链接的创建、删除和更新[通俗易懂]

    linux软链接的创建、删除和更新[通俗易懂]大家都知道,有的时候,我们为了省下空间,都会使用链接的方式来进行引用操作。同样的,在系统级别也有。在Windows系列中,我们称其为快捷方式,在Linux中我们称其为软链接(基本上都差不多了,其中可能有差别,但是那又怎样呢?我们只要实现我们的效果,谁会有精力去管它茴香的茴字有几种写法呢?)。Windows老姑娘的那几个姿势这里就不赘述了,我们今天主要说下Linux中的茴香的茴字怎么写。

  • PLANTINUM_plantuml流程图

    PLANTINUM_plantuml流程图bytotinunsplash1.背景随着工作时间的增长,越发觉得用专业的图形(用例图,时序图,ER图等等)去准确表达想法是很重要的。比如针对某个需求绘制的的用例图,比一段乏味的文字来的更加有意义,也便于别人理解。加之最近在学习源码的时候,发现很多书籍中都会使用类图,时序图等UML语言来描述逻辑关系。于是就在网上找了找绘制UML语言时,业界主流的一些工具都用什么,找了半天,大部分都用了下面…

    2022年10月26日
  • Moya 浅析_motivationally

    Moya 浅析_motivationallyMoya是一个高度抽象的网络库,他的理念是让你不用关心网络请求的底层的实现细节,只用定义你关心的业务。且Moya采用桥接和组合来进行封装(默认桥接了Alamofire),使得Moya非常好扩展,让你不用修改Moya源码就可以轻易定制。官方给出几个Moya主要优点:编译时检查APIendpoint权限让你使用枚举定义各种不同Target,endpoints把stubs当做…

    2022年10月25日
  • Spring Boot第八章-Spring Data JPA

    Spring Boot第八章-Spring Data JPA

  • AWVS10.5&12超详细使用教程

    AWVS10.5&12超详细使用教程AWVS介绍awvs

  • 软件缺陷,缺陷报告怎么写_缺陷报告通常包括哪些内容

    软件缺陷,缺陷报告怎么写_缺陷报告通常包括哪些内容软件缺陷软件缺陷:常常又被叫做Bug。所谓软件缺陷,即为计算机软件或程序中存在的某种破坏正常运行能力的问题、错误,或者隐藏的功能缺陷。缺陷的存在会导致软件产品在某种程度上不能满足用户的需要。从软件测试观点出发,软件缺陷有以下五大类:功能缺陷、系统缺陷、加工缺陷、数据缺陷、代码缺陷软件类别:缺陷的表现形式不仅体现在功能的失效方面,还体现在其他方面。主要类型有:软件没有实现产品规格说明所…

发表回复

您的电子邮箱地址不会被公开。

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