spring 4.3.20.RELEASE

dubbo 2.6.2

curator-framework 2.12.0

注册中心 zookeeper

jdk 1.8


一、服务提供方  

1、pom.xml配置


  
      org.springframework
      spring-core
      4.3.20.RELEASE
      
      
      
        com.alibaba
        dubbo
        2.6.2
    
    
    
        org.apache.curator
        curator-framework
        2.12.0
    
    
        org.slf4j
        slf4j-nop
        1.7.20
    
    
        junit
        junit
        4.12
        test
    

  

  
      
            org.apache.maven.plugins
            maven-compiler-plugin
            3.8.0
            
            1.8
            1.8
            
      
  

  

  2、接口

public interface HelloService {
    String sayHello(String name);
}


3、接口实现类

public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}


4、spring配置文件


    
        
        
        
        
      


5、log4j配置文件

log4j.rootLogger=INFO,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd\u00A0HH:mm:ss,SSS}\u00A0[%t]\u00A0[%c]-[%p]\u00A0%m%n


6、注册,并提供服务的测试类

public class DubboProviderTest {
    @Test
    public void test() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        
        //使服务不中断
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



二、服务调用方

1、pom.xml文件


  
      org.springframework
      spring-core
      4.3.0.RELEASE
      
      
        com.alibaba
        dubbo
        2.6.2
    
    
    
        org.apache.curator
        curator-framework
        2.12.0
    
    
        org.slf4j
        slf4j-nop
        1.7.20
    
    
        junit
        junit
        4.12
        test
    

  

  
      
        org.apache.maven.plugins
        maven-compiler-plugin
        3.8.0
        
        1.8
        1.8
        
      
  

  

  2、 接口

  //此处接口与提供方完全一致

public interface HelloService { 
    String sayHello(String name);
}


3、spring配置文件


    
        
        
        


4、log4j配置文件

log4j.rootLogger=INFO,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd\u00A0HH:mm:ss,SSS}\u00A0[%t]\u00A0[%c]-[%p]\u00A0%m%n

5、调用服务的测试类

public class DubboConsumerTest {
    @Test
    public void test() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        
        HelloService helloService = (HelloService)context.getBean("helloService");
        
        System.out.println("----------------------------------------------------------");
        System.out.println(helloService.sayHello("DanDan"));
        System.out.println("----------------------------------------------------------");
        
    }
}


三、输出如下结果

----------------------------------------------------------
Hello DanDan
----------------------------------------------------------