dubbo入门-dubbo和spring整合

创建一个maven的工程dubbo-demo

dubbo入门-dubbo和spring整合_第1张图片
工程结构.png

包含服务接口模块,服务接口模块:dubbo-demo-api,服务消费者模块:dubbo-demo-consumer,服务生产者模块:dubbo-demo-provider

1.服务接口模块:dubbo-demo-api

  • 创建接口DemoService


    dubbo入门-dubbo和spring整合_第2张图片
    DemoService.png
  • pom文件
    删除parent节点
    并创建artifactId,以对外提供依赖
    com.imooc
    dubbo-demo-api
    1.0-SNAPSHOT

2.服务生产者模块:dubbo-demo-provider

  • 创建Provider启动类
package com.alibaba.dubbo.demo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"dubbo-provider.xml"});
        context.start();
        System.in.read(); // press any key to exit
    }
}
  • 创建DemoServiceImpl实现类
package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}
  • Resources目录下创建dubbo-provider.xml


    
    
    
    
    
    

  • 添加依赖
    
        
            com.alibaba
            dubbo
            2.6.0
        
        
            com.imooc
            dubbo-demo-api
            1.0-SNAPSHOT
        
    

3.服务消费者模块:dubbo-demo-consumer

  • 创建Consumer 启动类
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"dubbo-consumer.xml"});
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation
        String hello = demoService.sayHello("fanfan"); // execute remote invocation
        System.out.println(hello); // show the result
    }
}
  • 创建dubbo-consumer.xml


    
    
    
    

启动生产者,启动消费者

控制台输出:Hello world


dubbo入门-dubbo和spring整合_第3张图片
输出效果.png

本地调试还可采用直连的方式

dubbo-provider.xml


    
    

    
    
    
    
    



dubbo-consumer.xml

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    
   
    
    

参考https://juejin.im/post/5a37600b6fb9a0450671b86a

你可能感兴趣的:(dubbo入门-dubbo和spring整合)