Spring Cloud Alibaba实战(4) - 整合dubbo实现rpc调用

Spring Cloud版本是Greenwich.RELEASE
Springboot版本是2.1.9.RELEASE
Spring Cloud Alibaba版本是2.1.0.RELEASE

现在,我们也可以使用dubbo作为rpc框架,来实现服务提供者和服务消费者。

01 和平时使用dubbo一样,创建一个接口jar包

1)创建一个maven应用,编写一个接口

public interface AppService {
    String getPort();
}

2)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">
    <parent>
        <artifactId>spring-cloud-alibabaartifactId>
        <groupId>com.jamiegroupId>
        <version>1.0.0version>
    parent>
    <modelVersion>4.0.0modelVersion>
    <packaging>jarpackaging>

    <artifactId>dubbo-apiartifactId>
project>

3)将项目install到本地仓库
我这里直接使用IDE的功能,也可以使用mvn命令,都是可以的,只要能达到目的即可。
当然,如果有远程仓库,也可以deploy到远程仓库中。
Spring Cloud Alibaba实战(4) - 整合dubbo实现rpc调用_第1张图片

02 编写provider应用

1)创建一个springboot应用,在pom.xml中导入依赖

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    <java.version>1.8java.version>
properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    dependency>

    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-dubboartifactId>
    dependency>

    <dependency>
        <groupId>com.jamiegroupId>
        <artifactId>dubbo-apiartifactId>
        <version>1.0.0version>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>Greenwich.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>2.1.0.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

2)在启动类配置服务注册发现的注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class NacosProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }

}

3)修改配置文件application.properties

spring.application.name=nacos-provider
server.port=8001
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

# dubbo的相关配置
dubbo.scan.base-packages=com.jamie.nacos.provider.service
dubbo.protocol.name=dubbo
# -1表示端口随机
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost

4)编写接口实现类
这里一定要注意,在类上的注解是dubbo的Service注解,不是spring的Service注解。

import com.jamie.server.AppService;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author [email protected]
 * @date 2019-10-10
 **/
@org.apache.dubbo.config.annotation.Service
public class AppServiceImpl implements AppService {

    @Value("${server.port}")
    private String port;

    @Override
    public String getPort() {
        return "provider port: " + port;
    }
}

03 编写consumer应用

1)在pom.xml文件导入依赖

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    <java.version>1.8java.version>
properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    dependency>

    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-dubboartifactId>
    dependency>

    <dependency>
        <groupId>com.jamiegroupId>
        <artifactId>dubbo-apiartifactId>
        <version>1.0.0version>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>Greenwich.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>2.1.0.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

2)在启动类配置服务注册发现的注解

@EnableDiscoveryClient
@SpringBootApplication
public class NacosConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }
}

3)修改application.properties配置文件

spring.application.name=nacos-consumer

server.port=9000

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.main.allow-bean-definition-overriding=true

dubbo.registry.address=spring-cloud://localhost
dubbo.consumer.check=false

4)调用provider的服务接口

import com.jamie.server.AppService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author [email protected]
 * @date 2019-10-08
 **/
@RestController
public class ConsumerController {

    @Reference
    private AppService appService;

    @GetMapping("/dubbo")
    public String dubbo() {
        return appService.getPort();
    }
}

04 测试

1)在测试时,我启动了两个provider应用,端口分别为8000和8001。一个consumer应用,端口为9000。
2)启动成功后,我们可以在nacos页面看到注册到nacos的应用
Spring Cloud Alibaba实战(4) - 整合dubbo实现rpc调用_第2张图片
3)测试远程调用
Spring Cloud Alibaba实战(4) - 整合dubbo实现rpc调用_第3张图片
Spring Cloud Alibaba实战(4) - 整合dubbo实现rpc调用_第4张图片
我们可以发现,nacos帮我们自动实现了负载均衡。

源码地址:码云

你可能感兴趣的:(springcloud)