一文了解带有Dubbo的Spring Cloud Alibaba

通过优锐课核心java学习笔记中,我们可以看到,码了很多专业的相关知识, 分享给大家参考学习。

看一下如何在阿里巴巴的Spring Cloud实现中使用这个流行的RPC框架。

Spring Cloud Alibaba

Spring Cloud Alibaba是Alibaba Cloud的Spring Cloud版本。 它由几个阿里巴巴的开源项目Nacos,Sentinel和RocketMQ以及几个阿里云原生商业产品组成,以增强用户在阿里云上的体验。 Spring Cloud Alibaba的新版本还将提供Dubbo作为RPC选择。

Dubbo是一个经过严格实践的RPC框架。 在另一篇文章中,我演示了如何将其与注释一起使用。 该示例使用的是Spring Boot。 Dubbo与Spring Boot紧密集成。 将Dubbo放在Spring Cloud Alibaba上似乎很自然。

在本文中,我们将使用一个简单的echo示例来说明在Spring Cloud Alibaba上使用Dubbo的步骤。

定义Dubbo接口

首先定义一个接口:

public interface EchoService {

    String echo(String message);

}

 

该接口将向远程客户端公开。 这里的提示是将此接口打包到第二或第三方工件(jar)中,以便该jar可用于spring-cloud-dubbo-sample-api。

实施Dubbo服务

Maven中创建一个项目

让我们用artifactIdId spring-cloud-dubbo-server-sample创建一个Maven项目。 然后,将依赖项添加到pom.xml中。

<dependencies>

    <!-- Sample API -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-dubbo-sample-api</artifactId>

        <version>${project.version}</version>

    </dependency>

    <!-- Spring Boot dependencies -->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-actuator</artifactId>

    </dependency>

    <!-- Dubbo Spring Cloud Starter -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-dubbo</artifactId>

    </dependency>

    <!-- Spring Cloud Nacos Service Discovery -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

    </dependency>

</dependencies>

 

在上面的pom.xml中:

· spring-cloud-dubbo-sample-api:这是工件ID

· spring-boot-actuator:这是Spring Boot生产就绪的工件

· spring-cloud-starter-dubbo:这是Dubbo Spring Cloud Starter工件

· spring-cloud-starter-alibaba-nacos-discovery:这是Nacos Spring Cloud服务注册表工件

在继续之前,我们需要在定义中添加一个版本:

<dependencyManagement>

    <dependencies>

        <!-- Spring Cloud Alibaba dependencies -->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-alibaba-dependencies</artifactId>

            <version>0.9.0.RELEASE</version>

            <type>pom</type>

            <scope>import</scope>

        </dependency>

    </dependencies>

  </dependencyManagement>

 

服务代码实施

这是实现的Java代码:

@org.apache.dubbo.config.annotation.Service

class EchoServiceImpl implements EchoService {

    @Override

    public String echo(String message) {

        return "[echo] Hello, " + message;

    }

}

 

@ org.apache.dubbo.config.annotation.Service注释将其声明为Dubbo服务。

配置Dubbo服务

建议的公开服务的方法是使用@DubboComponentScanannotation。

该配置涉及两个部分:Dubbo和Spring Cloud。

dubbo:

  scan:

    base-packages: org.springframework.cloud.alibaba.dubbo.bootstrap

  protocol:

    name: dubbo

    # -1 means self-defined

    port: -1

  registry:

    address: spring-cloud://localhost

spring:

  application:

    name: spring-cloud-alibaba-dubbo-server

  main:

    allow-bean-definition-overriding: true

  cloud:

    nacos:

      # Nacos

      discovery:

        server-addr: 127.0.0.1:8848

 

Spring Boot应用程序类

这与其他Spring Boot应用程序类相同:

@EnableDiscoveryClient

@EnableAutoConfiguration

public class DubboSpringCloudServerBootstrap {

    public static void main(String[] args) {

        SpringApplication.run(DubboSpringCloudServerBootstrap.class);

    }

}

 

这里唯一值得一提的是,我们应该在此之前启动Nacos服务,以便Nacos可以发现该服务。

实施Dubbo客户

创建spring-cloud-dubbo-client-sample Maven项目

与服务端配置类似,我们需要指定依赖项:

<dependencyManagement>

    <dependencies>

        <!-- Spring Cloud Alibaba dependencies -->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-alibaba-dependencies</artifactId>

            <version>0.9.0.RELEASE</version>

            <type>pom</type>

            <scope>import</scope>

        </dependency>

    </dependencies>

</dependencyManagement>

<dependencies>

    <!-- Sample API -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-dubbo-sample-api</artifactId>

        <version>${project.version}</version>

    </dependency>

    <!-- Spring Boot dependencies -->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-actuator</artifactId>

    </dependency>

    <!-- Dubbo Spring Cloud Starter -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-dubbo</artifactId>

    </dependency>

    <!-- Spring Cloud Nacos Service Discovery -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

    </dependency>

</dependencies>

 

与服务端的主要区别在于客户端将是一个使用spring-boot-starter-web的Web Servlet应用程序。

配置客户端

与服务端类似,配置分为两个部分:Dubbo和Spring。

dubbo:

  registry:

    address: spring-cloud://localhost

  cloud:

    subscribed-services: spring-cloud-alibaba-dubbo-server

spring:

  application:

    # Dubbo

    name: spring-cloud-alibaba-dubbo-client

  main:

    # Spring Boot 2.1

    allow-bean-definition-overriding: true

  cloud:

    nacos:

      # Nacos

      discovery:

        server-addr: 127.0.0.1:8848

 

在这里,我们通过将dubbo.cloud.subscribed-services绑定到spring-cloud-dubbo-server-sample来指定要使用的服务。

· dubbo.cloud.subscribed-services:要订阅多个服务,请使用“,”作为分隔符。 

由于它是一个Web应用程序,因此默认端口为8080。 可以通过修改server.port属性来更改。

Spring Boot应用程序类和实现Java代码

分为两个步骤:

@EnableDiscoveryClient

@EnableAutoConfiguration

@RestController

public class DubboSpringCloudClientBootstrap {

    @Reference

    private EchoService echoService;

    @GetMapping("/echo")

    public String echo(String message) {

        return echoService.echo(message);

    }

    public static void main(String[] args) {

        SpringApplication.run(DubboSpringCloudClientBootstrap.class);

    }

}

 

由于这是一个Web客户端,因此我们可以使用curl进行尝试。 如果我们跑

curl http://127.0.0.1:8080/echo?message=yourmessage

 

或者我们可以运行Java客户端来获得相同的结果。

We should see the result [echo] Hello, yourmessage

 

结论

Spring Cloud阿里巴巴现在有两个发行版,其中包括Dubbo。 它们是与Spring Cloud Finchley兼容的0.2.2版本和与Spring Cloud Greenwich兼容的0.9.0版本。 无论是尝试适应Spring Cloud阿里巴巴的Dubbo用户,还是相反,经验都应该是无缝的。

 

> 喜欢这篇文章的可以点个赞,欢迎大家留言评论,记得关注我,每天持续更新技术干货、职场趣事、海量面试资料等等

 > 如果你对java技术很感兴趣也可以交流学习,共同学习进步。 

> 不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代


文章写道这里,欢迎完善交流。最后奉上近期整理出来的一套完整的java架构思维导图,分享给大家对照知识点参考学习。有更多JVM、Mysql、Tomcat、Spring Boot、Spring Cloud、Zookeeper、Kafka、RabbitMQ、RockerMQ、Redis、ELK、Git等Java干货


微信图片_20200227100004.png微信图片_20200225115155.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章