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將項目下載下來
修改如下Service實現類
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
運行Consumer中main方法,查看日志可以看出,Consumer分布式調用Provider已經成功了?
?這個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抽象類
?
那么這個代理有什么用呢?未完待續
?
轉載于:https://www.cnblogs.com/LDDXFS/p/9719177.html
總結
以上是生活随笔為你收集整理的dubbo系列(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网管经验
- 下一篇: 历届蓝桥杯真题下载(省赛)