Dubbo2.6.5入门——简单的HelloWorld

Posted 小LUA

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo2.6.5入门——简单的HelloWorld相关的知识,希望对你有一定的参考价值。

需要本地起一个zookeeper

下载:https://www.apache.org/dyn/closer.cgi/zookeeper/

镜像:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

 

下载3.4.X的

下载完成解压,进入conf目录,复制一份配置文件并重命名即可:

 

 在bin目录有个启动的

 

 双击启动

建立父工程

打开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的主要内容,如果未能解决你的问题,请参考以下文章

rxjs入门指南

在 Spring 框架中使用 Dubbo

饮冰三年-人工智能-Python-11之HelloWor

可进抖音的C++学习路线公开啦

CMake 简单使用

使用 mingw 遵循简单的 c++ DLL 教程时出错