dubbo项目搭建

建立Dubbo-client

建立一个maven-archetype-quickstart工程
pom.xml



<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">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.xxxgroupId>
  <artifactId>dubbo-clientartifactId>
  <version>1.0-SNAPSHOTversion>

  <name>dubbo-clientname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>com.taotaogroupId>
      <artifactId>first-apiartifactId>
      <version>1.0-SNAPSHOTversion>
    dependency>
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
      <version>2.6.4version>
    dependency>
    <dependency>
      <groupId>org.apache.zookeepergroupId>
      <artifactId>zookeeperartifactId>
      <version>3.4.12version>
    dependency>
    <dependency>
      <groupId>org.apache.curatorgroupId>
      <artifactId>curator-frameworkartifactId>
      <version>4.0.1version>
    dependency>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>
  dependencies>

project>

App.java

public static void main( String[] args )
    {
        ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("dubbo-client.xml");

        ISumService sumService = (ISumService) context.getBean("sumService");
        System.out.println("3+4="+sumService.sum(3,4));
    }

dubbo-client.xml



    <dubbo:application name="dubbo-client" owner="lyj"/>

    <dubbo:registry address="zookeeper://192.168.67.128:2181"/>

    <dubbo:protocol name="dubbo" port="20880"/>

    <dubbo:reference id="sumService" interface="com.taotao.ISumService"/>
beans>


server端

建立两个子模块 api和provider
pom.xml



<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">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.taotaogroupId>
  <artifactId>do-dubboserverartifactId>
  <packaging>pompackaging>
  <version>1.0-SNAPSHOTversion>
  <modules>
    <module>first-apimodule>
    <module>first-providermodule>
  modules>

  <name>do-dubboservername>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
      <version>2.6.4version>
    dependency>
    <dependency>
      <groupId>org.apache.zookeepergroupId>
      <artifactId>zookeeperartifactId>
      <version>3.4.12version>
    dependency>
    <dependency>
      <groupId>org.apache.curatorgroupId>
      <artifactId>curator-frameworkartifactId>
      <version>4.0.1version>
    dependency>

    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>
  dependencies>

project>

api 的接口

public interface ISumService {
    Integer sum(Integer a,Integer b);
}

provider

public class SumService implements ISumService {
    @Override
    public Integer sum(Integer a, Integer b) {
        return a+b;
    }
}
public class App 
{
    public static void main( String[] args ) throws IOException {
        ClassPathXmlApplicationContext context=
                new ClassPathXmlApplicationContext("dubbo-server.xml");
        context.start();

        System.in.read();

    }
}

dubbo-server.xml



    <dubbo:application name="dubbo-provider" owner="lyj"/>

    <dubbo:registry address="zookeeper://192.168.67.128:2181"/>

    <dubbo:protocol name="dubbo" port="20880"/>

    <dubbo:service interface="com.taotao.ISumService" ref="sumService"/>
    <bean name="sumService" class="com.taotao.service.SumService"/>

beans>

你可能感兴趣的:(dubbo)