zoukankan      html  css  js  c++  java
  • Dubbo不用注册中心实现远程调用的简单用法demo

    服务端:

    先在服务端写接口以及接口实现类:

    package com.gupao.dubbo;
    
    public interface GPDubboService {
        public String sayHello(String msg);
    }
    
    package com.gupao.dubbo;
    
    public class GPDubboServiceImpl implements GPDubboService{ @Override public String sayHello(String msg) { return "Hello ,liaoyang make you see " + msg; } }

      2 xml文件中注册服务

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://code.alibabatech.com/schema/dubbo
          http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <dubbo:application name="dubbo-server"  owner="liaoyy"/>
      --表示不用注册中心
        <dubbo:registry address="N/A"/>
    
        <dubbo:protocol name="dubbo" port="20880"/>
    
        <dubbo:service interface="com.gupao.dubbo.GPDubboService" ref="gphelloservice"/>
    
        <bean  id="gphelloservice" class="com.gupao.dubbo.GPDubboServiceImpl"/>
    
    </beans>
    

      运行代码发布服务

    public class BootStrap {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext classPathXmlApplicationContext = new
                    ClassPathXmlApplicationContext("dubbo-server.xml");
            classPathXmlApplicationContext.start();
            System.in.read();
        }
    }
    

      

    消费端:

    先把服务方依赖过来

        <dependency>
          <groupId>com.gupao.dubbo</groupId>
          <artifactId>server-api</artifactId>
          <version>1.0-SNAPSHOT</version>
        </dependency>
    

      由于没用注册中心,肯定要指定请求地址的

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://code.alibabatech.com/schema/dubbo
          http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <dubbo:application name="dubbo-client"  owner="liaoyy"/>
    
        <dubbo:registry address="N/A"/>
    
        <dubbo:protocol name="dubbo" port="20880"/>
      -- id为服务方bean的id  interfacce为服务方发布的接口路径
        <dubbo:reference id="gphelloservice" interface="com.gupao.dubbo.GPDubboService" url="dubbo://169.254.105.124:20880/com.gupao.dubbo.GPDubboService"/>
        
    
    </beans>
    

      然后调用就完事儿

    public class BootStrap {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext classPathXmlApplicationContext = new
                    ClassPathXmlApplicationContext("dubbo-client.xml");
            classPathXmlApplicationContext.start();
            GPDubboService gphelloservice
                    = (GPDubboService) classPathXmlApplicationContext.getBean("gphelloservice");
            System.out.println(gphelloservice.sayHello("liaoyyyyy"));
        }
    }
    

      运行结果

    七月 05, 2020 4:15:40 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6267c3bb: startup date [Sun Jul 05 16:15:40 CST 2020]; root of context hierarchy
    七月 05, 2020 4:15:40 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [dubbo-client.xml]
    七月 05, 2020 4:15:40 下午 com.alibaba.dubbo.common.logger.LoggerFactory info
    信息: using logger: com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter
    七月 05, 2020 4:15:41 下午 com.alibaba.dubbo.remoting.transport.AbstractClient info
    信息:  [DUBBO] Successed connect to server /169.254.105.124:20880 from NettyClient 169.254.105.124 using dubbo version 2.5.6, channel is NettyChannel [channel=[id: 0x0c33b74f, /169.254.105.124:59010 => /169.254.105.124:20880]], dubbo version: 2.5.6, current host: 169.254.105.124
    七月 05, 2020 4:15:41 下午 com.alibaba.dubbo.remoting.transport.AbstractClient info
    信息:  [DUBBO] Start NettyClient DESKTOP-TLTOEDB/169.254.105.124 connect to the server /169.254.105.124:20880, dubbo version: 2.5.6, current host: 169.254.105.124
    七月 05, 2020 4:15:42 下午 com.alibaba.dubbo.config.AbstractConfig info
    信息:  [DUBBO] Refer dubbo service com.gupao.dubbo.GPDubboService from url dubbo://169.254.105.124:20880/com.gupao.dubbo.GPDubboService?application=dubbo-client&dubbo=2.5.6&interface=com.gupao.dubbo.GPDubboService&methods=sayHello&owner=liaoyy&pid=29396&revision=1.0-SNAPSHOT&side=consumer&timestamp=1593936941363, dubbo version: 2.5.6, current host: 169.254.105.124
    Hello ,liaaoyang make you see liaoyyyyy
    七月 05, 2020 4:15:42 下午 com.alibaba.dubbo.config.AbstractConfig info
    信息:  [DUBBO] Run shutdown hook now., dubbo version: 2.5.6, current host: 169.254.105.124
    
    Process finished with exit code 0
    

      

  • 相关阅读:
    Casting
    hdu 1164 Eddy's research I
    hdu 1212 Big Number
    CF271 C. Secret
    hdu 1065 I Think I Need a Houseboat
    单档——状态码显示设置,状态码更改链表更新
    单身金额统计,更新显示到单头
    开窗设计器——条件,参数,返回值接收显示
    单档——单头内容新增、修改后同步更新到其他相关数据表
    TIPTOP之分割split函数方法、getIndexOf、subString、replace、临时表创建;
  • 原文地址:https://www.cnblogs.com/andydlz/p/13246591.html
Copyright © 2011-2022 走看看