Dubbo --- first experience

1, project structure (maven project) 

                    

2、dubbotest.pom

<? xml Version = "1.0" encoding = "UTF-8"?> 
<Project xmlns = "http://maven.apache.org/POM/4.0.0" 
         xmlns: xsi = "HTTP: //www.w3 .org / 2001 / "the XMLSchema-instance 
         http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">: the xsi the schemaLocation = " 
    <modelVersion> 4.0.0 </ modelVersion> 

    <the groupId> com.an </ the groupId> 
    <the artifactId> Dubbo-Test </ the artifactId> 
    <Packaging> POM </ Packaging> 
    <Version> the SNAPSHOT-1.0 </ Version> 

    <Properties > 
        <motan.version> 0.3.0 </motan.version> 
        <- GA version is widely used in internal Alibaba:! 2.4.9, this version is strongly recommended -> 
        <Dubbo.version>2.5.3</dubbo.version>
        <dubbox.version>2.8.4</dubbox.version>version>2.5.3</dubbo.version>
        <spring.version>4.3.6.RELEASE</spring.version>
        <java.version>1.7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <modules>
        <module>provider</module>
        <module>consumer</module>
        <module>api</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <!-- spring相关 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.11</version>
        </dependency>
    </dependencies>

</project>

3、api

    3.1、pom

      

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-test</artifactId>
        <groupId>com.an</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>api</artifactId>


</project>

    3.2、HelloService

package com.an.service;

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

4、provider

    4.1、pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-test</artifactId>
        <groupId>com.an</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.an</groupId>
            <artifactId>api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project>

    4.2、log4j.properties

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

  

     
    4.3、HelloServiceImpl

package com.an.service;


public class HelloServiceImpl implements HelloService{

    public String sayHello(String msg) {
        return msg;
    }
}

 

    4.4、dubbo-provider.xml

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xmlns: Dubbo =" http://code.alibabatech.com/schema/dubbo " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans 
                           HTTP: // the WWW. springframework.org/schema/beans/spring-beans.xsd 
                           http://code.alibabatech.com/schema/dubbo 
                           http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> 

    
    <-! offers party app name -> 
    <Dubbo: the application name = "Dubbo-Provider" /> 
    ! <- using multicast broadcast exposure registry service address ->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <-! Dubbo agreement with exposure to port services 20880 ->
    <Dubbo: Protocol name = "Dubbo" Port = "20880" /> 
    <- - statement needs to be exposed service interface timeout timeout error over time too!> 
    <Dubbo: Service interface = "com.an.service.HelloService "REF =" the helloService "timeout =" 5000 "/> 


</ Beans>

  

   4.5、ProviderRunner

package com.an;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;

public class ProviderRunner {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"dubbo-provider.xml"});
        context.start();
        System.out.println("provider...started...");
        System.in.read();
    }
}

5、consumer

    5.1、pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-test</artifactId>
        <groupId>com.an</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.an</groupId>
            <artifactId>api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

    5.2、log4j.properties

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

    5.3、dubbo-consumer

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xmlns: Dubbo =" http://code.alibabatech.com/schema/dubbo " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans 
                           HTTP: // the WWW. springframework.org/schema/beans/spring-beans.xsd 
                           http://code.alibabatech.com/schema/dubbo 
                           http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> 

    <-! consumption party app name -> 
    <Dubbo: the application name = "Dubbo-Consumer" /> 
    ! <- broadcast using multicast discovery service address registry exposure ->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <! - generated remote service agent, you can use the same demoService and local bean -> 
    <Dubbo: Reference the above mentioned id = "helloService" interface = "com.an.service.HelloService" /> 

</ Beans>

    5.4、ConsumerRunner

package com.an;

import com.an.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerRunner {

    public static void main(String[] args){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer.xml"});
        context.start();
        HelloService helloService =(HelloService) context.getBean("helloService");
        String msg=helloService.sayHello("hello world");
        System.out.println(msg);
    }
}

  

    

 

Guess you like

Origin www.cnblogs.com/anpeiyong/p/10991171.html