1. 程式人生 > >Dubbo2.6.5入門——簡單的HelloWorld

Dubbo2.6.5入門——簡單的HelloWorld

hello provide 負載均衡算法 cli 日誌類 ice model rom pri

建立父工程

打開idea,新建一個空的maven工程,作為整個項目的父工程。

<?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.demo</groupId> <artifactId>dubbo-parent</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-api</module> <module>dubbo-service</module
> <module>dubbo-consumer</module> </modules> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>
2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.13.0</version> </dependency> <!--<dependency>--> <!--<groupId>org.apache.curator</groupId>--> <!--<artifactId>curator-client</artifactId>--> <!--<version>2.13.0</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.apache.curator</groupId>--> <!--<artifactId>curator-recipes</artifactId>--> <!--<version>2.13.0</version>--> <!--</dependency>--> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> </dependency> </dependencies> <repositories> <repository> <id>sonatype-nexus-snapshots</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>

然後建立公共模塊

技術分享圖片

..

技術分享圖片

..

技術分享圖片

完成之後在src/main/java目錄下新建一個服務接口

技術分享圖片

內容:

package com.demo.common.service;

public interface DemoService {

    String hello();
}

目錄:

技術分享圖片

建立dubbo服務子模塊

技術分享圖片

..

技術分享圖片

..

技術分享圖片

..

技術分享圖片

這個時候呢,Springboot的父工程不是我們建立的那個,所以要改一下parent,還需要加入公共模塊的依賴

<?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>
    <parent>
        <groupId>com.demo</groupId>
        <artifactId>dubbo-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-service</name>
    <description>dubbo服務</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

下面寫一個簡單的dubbo服務,在與SpringBoot啟動類同級目錄下建立service包,裏面有服務接口的實現

技術分享圖片

..服務接口實現

package com.demo.dubboservice.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.demo.common.service.DemoService;


/**
 *  建議由服務提供方設置超時,在 Provider 上盡量多配置 Consumer 端屬性
 *  timeout 方法調用超時
 *  retries 失敗重試次數,缺省是 2 [2]
 *  loadbalance 負載均衡算法 [3],缺省是隨機 random。還可以有輪詢 roundrobin、最不活躍優先 [4] leastactive 等
 *  actives 消費者端,最大並發調用限制,即當 Consumer 對一個服務的並發調用到上限後,新調用會阻塞直到超時
 */
@Service(registry = "dubboRegistry", timeout = 3000, version = "1.0", retries = 3, loadbalance = "random", actives = 5)
public class DemoServiceImpl implements DemoService {
    @Override
    public String hello() {
        return "動物園裏有大西幾! 小兇許! 小腦斧! 梅發怒! 小福泥! 小海疼! 發福蝶! 大雞居! 大飛囊! 大lai流! 強頸怒! 大蟒鞋! 小白去! 鴕寮! 大帶許! 大窮貓! 大諾坨! 大你魚! 丹鼎貨! 小烏堆!";
    }
}

..然後修改application.properties

spring.application.name=dubbo-service
server.port=9999

# 應用名
dubbo.application.id=service-provider
dubbo.application.name=service-provider

# 掃描dubbo服務(@Service.. @Reference..)
dubbo.scan.basePackages=com.demo.dubboservice.service

# 通信協議
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

# 註冊中心
dubbo.registry.id = dubboRegistry
dubbo.registry.timeout=5000
dubbo.registry.address = 127.0.0.1:2181
dubbo.registry.client = curator
dubbo.registry.protocol=zookeeper

建立服務消費者子模塊

技術分享圖片

..

技術分享圖片

..

技術分享圖片

..

技術分享圖片

同樣,也需要更改parent並加入公共模塊依賴

<?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>
    <parent>
        <groupId>com.demo</groupId>
        <artifactId>dubbo-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-consumer</name>
    <description>dubbo消費者</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在Springboot啟動類的同級目錄下建立,controller

技術分享圖片

Controller

package com.demo.dubboconsumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.demo.common.service.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class DemoController {

    @Reference(version = "1.0")
    private DemoService demoService;

    @RequestMapping("/hello")
    public String hello(){
        return demoService.hello();
    }

}

application.properties

spring.application.name=dubbo-service
server.port=9990

# 應用名
dubbo.application.id=service-consumer
dubbo.application.name=service-consumer

# 掃描dubbo服務(@Service.. @Reference..)
dubbo.scan.basePackages=com.demo.dubboconsumer.controller

# 通信協議
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

# 註冊中心
dubbo.registry.id = dubboRegistry
dubbo.registry.timeout=5000
dubbo.registry.address = 127.0.0.1:2181
dubbo.registry.client = curator
dubbo.registry.protocol=zookeeper

還有一個日誌類,這個沒有也行,我自己寫著玩的

package com.demo.dubboconsumer.utils;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * AOP 日誌
 */
@Aspect
@Component
@Slf4j
public class MonitorAOP {

    @Autowired
    private HttpServletRequest request;

    @Pointcut("execution(* com.demo.dubboconsumer.controller..*.*(..))")
    public void aspect(){}

    @Around("aspect()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        log.info(joinPoint + "|Time-Consuming:" + (end-start) + "|" + request.getRequestURI());
        return result;
    }

    @AfterThrowing(value = "aspect()", throwing = "e")
    public void afterException(JoinPoint joinPoint, Exception e){

    }
}

首先啟動服務提供者

技術分享圖片

然後啟動消費者

技術分享圖片

然後呢,我前面寫了一個管控臺的,不清楚請看這裏,現在看 http://localhost:8080/#/。

技術分享圖片

點擊Detail

技術分享圖片

可以看到消費者信息。

下面我們訪問一下接口 http://localhost:9990/hello

技術分享圖片

訪問的話我寫的AOP日誌攔截會輸出信息

技術分享圖片

Dubbo2.6.5入門——簡單的HelloWorld