Dubbo學習筆記

Dubbo學習筆記

Dubbo的設計目的

  1. 動態註冊、發現服務,軟負載均衡和服務降級。
  2. 描述服務依賴關係,描述總體服務架構。
  3. 統計服務負載,按需擴展容量。

簡單實例

該實例參考官方實例,有所修改。java

整個項目結構:git

dubbo-hello
.
├── dubbo-hello-api
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── tc
│   │   │   │           └── dubbo
│   │   │   │               └── hello
│   │   │   │                   └── HelloService.java
│   │   │   └── resources
│   │   └── test
│   │       └── java
│   └── target
│
├── dubbo-hello-consumer
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── tc
│   │   │   │           └── dubbo
│   │   │   │               └── hello
│   │   │   │                   └── consumer
│   │   │   │                       └── Consumer.java
│   │   │   └── resources
│   │   │       ├── META-INF
│   │   │       │   └── spring
│   │   │       │       └── dubbo-hello-consumer.xml
│   │   │       ├── dubbo.properties
│   │   │       └── log4j.properties
│   │   └── test
│   │       └── java
│   └── target
│      
├── dubbo-hello-provider
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── tc
│   │   │   │           └── dubbo
│   │   │   │               └── hello
│   │   │   │                   └── provider
│   │   │   │                       ├── HelloServiceImpl.java
│   │   │   │                       └── Provider.java
│   │   │   └── resources
│   │   │       ├── META-INF
│   │   │       │   └── spring
│   │   │       │       └── dubbo-hello-provider.xml
│   │   │       ├── dubbo.properties
│   │   │       └── log4j.properties
│   │   └── test
│   │       └── java
│   └── target
└── pom.xml

整個項目分爲三個模塊,dubbo-hello-api、dubbo-hello-provider和dubbo-hello-consumer。一下是整個dubbo-hello工程的pom內容:github

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

    <groupId>com.tc</groupId>
    <artifactId>dubbo-hello</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>${project.artifactId}</name>

    <properties>
        <skip_maven_deploy>false</skip_maven_deploy>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <modules>
        <module>dubbo-hello-api</module>
        <module>dubbo-hello-provider</module>
        <module>dubbo-hello-consumer</module>
    </modules>

</project>

公共API定義

通常Provider和Consumer共享的接口應該單獨一個包進行定義,方便雙方的共享。這裏新建了模塊dubbo-hello-api來定義公共接口。其pom文件爲基本配置,無特殊性,簡單定義接口HelloService以下:spring

package com.tc.dubbo.hello;

/**
 * Created by tianchi on 2018/3/14.
 */
public interface HelloService {
    String sayHello(String name);
}

Provider實現

服務提供者模塊dubbo-hello-provider的pom文件內容以下:apache

<?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-hello</artifactId>
        <groupId>com.tc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-hello-provider</artifactId>
    <packaging>jar</packaging>
    <name>${project.artifactId}</name>
    <description>The demo provider module of dubbo project</description>
    <properties>
        <skip_maven_deploy>false</skip_maven_deploy>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.tc</groupId>
            <artifactId>dubbo-hello-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.tc.dubbo.hello.provider.Provider</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

其中聲明瞭對com.tc:dubbo-hello-api接口的依賴,還有對com.alibaba:dubbo的依賴。插件plugin聲明瞭採用加入依賴的打包方式,這樣方便直接運行。api

在文件dubbo-hello-provider.xml中聲明瞭要暴露的服務:架構

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="hello-provider"/>

    <!-- use multicast registry center to export service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>

    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- service implementation, as same as regular local bean -->
    <bean id="helloService" class="com.tc.dubbo.hello.provider.HelloServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.tc.dubbo.hello.HelloService" ref="helloService"/>

</beans>

其中說明了註冊方式爲multicast,也即利用廣播,無註冊中心的方式。同時聲明瞭協議和端口號。app

暴露的服務HelloServiceImpl實現以下:負載均衡

package com.tc.dubbo.hello.provider;

import com.alibaba.dubbo.rpc.RpcContext;
import com.tc.dubbo.hello.HelloService;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by tianchi on 2018/3/14.
 */
public class HelloServiceImpl implements HelloService{
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
}

入口類Provider的實現以下:maven

package com.tc.dubbo.hello.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by tianchi on 2018/3/14.
 */
public class Provider {

    public static void main(String[] args) throws Exception {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-hello-provider.xml"});
        context.start();

        System.in.read(); // press any key to exit
    }

}

Consumer實現

其pom文件和Provider相似:

<?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-hello</artifactId>
        <groupId>com.tc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-hello-consumer</artifactId>
    <packaging>jar</packaging>
    <name>${project.artifactId}</name>
    <description>The demo consumer module of dubbo project</description>

    <dependencies>
        <dependency>
            <groupId>com.tc</groupId>
            <artifactId>dubbo-hello-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.tc.dubbo.hello.consumer.Consumer</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

其dubbo-hello-consumer.xml配置以下,聲明瞭對服務的引用。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="hello-consumer"/>

    <!-- use multicast registry center to discover service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>

    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="helloService" check="false" interface="com.tc.dubbo.hello.HelloService"/>

</beans>

入口類Consumer實現以下:

package com.tc.dubbo.hello.consumer;

import com.tc.dubbo.hello.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by tianchi on 2018/3/14.
 */
public class Consumer {

    public static void main(String[] args) {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-hello-consumer.xml"});
        context.start();
        HelloService helloService = (HelloService) context.getBean("helloService"); // get remote service proxy

        while (true) {
            try {
                Thread.sleep(1000);
                String hello = helloService.sayHello("world"); // call remote method
                System.out.println(hello); // get result

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

    }
}
相關文章
相關標籤/搜索