001--最简单Dubbo项目

话题一:搭建Dubbo项目

  • 1.Dubbo依赖zookeeper进行服务注册与发现:先搭建zookeeper并运行
  • 2.创建Maven项目,添加pom的dubbo依赖

    4.0.0
    com.vincent.dubbo
    TDubbo
    0.0.1-SNAPSHOT
    jar
    server
    http://maven.apache.org

    
        UTF-8
    

    
        
        
            org.springframework
            spring-context
            4.1.6.RELEASE
        
        

        
        
            com.alibaba
            dubbo
            2.5.3
        
        

        
        
            org.apache.zookeeper
            zookeeper
            3.3.6
        
        

        
        
            commons-logging
            commons-logging
            1.1.1
        
        
            log4j
            log4j
            1.2.15
            
                
                    com.sun.jdmk
                    jmxtools
                
                
                    com.sun.jmx
                    jmxri
                
                
                    jms
                    javax.jms
                
                
                    mail
                    javax.mail
                
            
        
        

        
        
            org.jboss.netty
            netty
            3.2.0.Final
        
        
            com.101tec
            zkclient
            0.8
        
        
    
  
  • 3.创建服务提供者
    • 3.1 创建服务接口
public interface DemoService {  
    String sayHello(String name);  
} 
  • 3.2 实现服务接口
public class DemoServiceImpl implements DemoService {  
      public String sayHello(String name) {  
        System.out.println("init : " + name);  
        return "hello " + name;  
    }  
}
  • 3.3 添加配置文件:applicationProvider.xml


    
    
    
    

    
    

    
    

  
  • 3.4 发布服务
public class ServerMain {  
  
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {  
        
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationProvider.xml");  
        context.start();  
        System.out.println("Dubbo provider start...");

        try {
            System.in.read();   // 按任意键退出
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }  
}
  • 3.5 自此服务已经发布,等待服务消费者调用
  • 4.创建服务消费者
    • 4.1 新的项目,内容接口和接口和上面是一样的,不再需要服务实现类
public interface DemoService {  
     String sayHello(String name);  
}  
  • 4.2 添加配置文件:applicationProvider.xml


    
    
    
    
    
    
    

  • 5.创建服务消费者(服务消费)
public class ServerMain {  
  
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {  
        
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationProvider.xml");  
        context.start();  
        System.out.println("Dubbo Consumer start...");

        DemoService demoService = (DemoService) context.getBean("demoService");
        String hello = demoService.sayHello("world");
        System.out.println("------------------------"+hello);
        
        try {
            System.in.read();   // 按任意键退出
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }  
}  

话题二:参考网址

  • 参考网址:【Apache Dubbo官网】

你可能感兴趣的:(001--最简单Dubbo项目)