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

 

微信公众号

                          
posted @ 2020-01-13 15:55  盲目的拾荒者  阅读(168)  评论(0编辑  收藏  举报