zoukankan      html  css  js  c++  java
  • 2.Dubbo初探

    新建Maven项目,redis充当注册中心

    1.pom.xml

    <!--dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.8</version>
    </dependency>
    <!--redis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>

    2.服务提供者

    GreetingService.java

    package org.niugang.dubbo.provider;
    
    /**
     * 
     * @ClassName:  GreetingService   
     * @Description:定义接口  
     * @author: niugang
     * @date:   2018年8月17日 上午10:11:11   
     * @Copyright: 863263957@qq.com. All rights reserved. 
     *
     */
    public interface GreetingService {
           String sayHello(String name);
    }

    GreetingServiceImpl.java

    package org.niugang.dubbo.provider;
    
    /**
     * 
     * @ClassName:  GreetingServiceImpl   
     * @Description:定义接口实现类  
     * @author: niugang
     * @date:   2018年8月17日 上午10:12:50   
     * @Copyright: 863263957@qq.com. All rights reserved. 
     *
     */
    public class GreetingServiceImpl implements GreetingService{
    
        public String sayHello(String name) {
            
            return "Hello " + name;
        }
    
    }

    Provider.java启动类

    package org.niugang.dubbo.provider;
    
    import java.io.IOException;
    
    import com.alibaba.dubbo.config.ApplicationConfig;
    import com.alibaba.dubbo.config.RegistryConfig;
    import com.alibaba.dubbo.config.ServiceConfig;
    /**
     * 
     * @ClassName:  Provider   
     * @Description:服务提供者
     * @author: niugang
     * @date:   2018年8月17日 上午11:03:44   
     * @Copyright: 863263957@qq.com. All rights reserved. 
     *
     */
    public class Provider {
        public static void main(String[] args) throws IOException {
    
            ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<GreetingService>();
            // 应用信息
            serviceConfig.setApplication(new ApplicationConfig("first-dubbo-provider"));
            // 注册中心 redis当注册中心
            serviceConfig.setRegistry(new RegistryConfig("redis://localhost:6379"));
            // 
            serviceConfig.setInterface(GreetingService.class);
            // 接口实现类引用
            serviceConfig.setRef(new GreetingServiceImpl());
            serviceConfig.export();
            System.in.read();
        }
    }

    3.服务消费者

    Consumer.java

    package org.niugang.dubbo.consumer;
    
    import org.niugang.dubbo.provider.GreetingService;
    
    import com.alibaba.dubbo.config.ApplicationConfig;
    import com.alibaba.dubbo.config.ReferenceConfig;
    import com.alibaba.dubbo.config.RegistryConfig;
    /**
     * 
     * @ClassName:  Consumer   
     * @Description:服务消费者  
     * @author: niugang
     * @date:   2018年8月17日 上午11:03:27   
     * @Copyright: 863263957@qq.com. All rights reserved. 
     *
     */
    public class Consumer {
    
        public static void main(String[] args) {
            ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<GreetingService>();
            referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer"));
            referenceConfig.setRegistry(new RegistryConfig("redis://localhost:6379"));
            referenceConfig.setInterface(GreetingService.class);
            GreetingService greetingService = referenceConfig.get();
            System.out.println(greetingService.sayHello("world"));
        }
    
    }

    启动服务提供者,在启动消费者。

    调用接口返回信息打印如下

    源码地址:https://gitee.com/niugangxy/dubbo

    微信公众号

                              
  • 相关阅读:
    PHP生成xml 无法识别或是无法读取或是浏览器不识别等问题
    关于PHP 采集类
    Centos7 下安装Docke
    Git使用之设置SSH Key
    yii2.0中Rbac 怎么添加超加管理员
    Undefined index: HTTP_RAW_POST_DATA的解决办法
    window下phpstudy的nginx配置虚拟主机
    yii2.0中添加二维数组,多条数据。
    预防onion比特币勒索病毒,如何快速关闭135,137,138,139,445端口
    github与git之间怎么建立连接
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12187689.html
Copyright © 2011-2022 走看看