【转】 SpringCloudAlibaba之远程调用Dubbo实战

【转】 SpringCloudAlibaba之远程调用Dubbo实战

SpringCloudAlibaba微服务实战教程系列  

注意:以下实战是Dubbo以Nacos为注册中心的使用方式

一、Dubbo提取api模块

package city.albert.email;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/10/9  12:22 PM
 */
public interface EmailCodeService {

    /**
     *
     * @param email
     * @param code
     * @return
     */
    Boolean email(String email, String code);
}
View Code

模块pom的依赖如下:

        <dependency>
            <groupId>city.albert</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

二、服务提供者

  1、引入pom文件依赖

  注意:我的版本号是同父pom统一管理,一定注意版本兼容的问题

        <!--接口依赖包-->
        <dependency>
            <groupId>city.albert</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--dubbo依赖包-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-apache-dubbo-adapter</artifactId>
        </dependency>

  2、配置配置文件application.yml或者bootstrap.yml

spring:
  cloud:
    nacos:
    #指定注册中心需要引入spring-cloud-starter-alibaba-nacos-discovery依赖哦!本章不是注册nacos重点不做解释
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: service-email8082
  main:
    allow-bean-definition-overriding: true
dubbo:
  #dubbo扫描dubbo注解报名
  scan:
    base-packages: city.albert
  protocol:
    # dubbo 协议
    name: dubbo
    # dubbo 协议端⼝( -1 表示⾃增端⼝,从 20880 开始)
    port: -1
  cloud:
  # 订阅服务提供⽅的应⽤列表,订阅多个服务提供者使⽤ "," 连接
    subscribed-services: service-code8081
  registry:
    #注册到springcloud服务
    address: spring-cloud://localhost

  3、实现接口

  使用org.apache.dubbo.config.annotation包下的@Service注解声明dubbo接口实现类

package city.albert;

import city.albert.email.EmailCodeService;
import org.apache.dubbo.config.annotation.Service;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/10/9  12:23 PM
 */
@Service
public class EmailCodeServiceImpl implements EmailCodeService {

    @Override
    public Boolean email(String email, String code) {
        System.out.println("发送邮件");
     return true;
    }
}

三、服务消费者

  1、引入pom文件依赖

  注意:我的版本号是同父pom统一管理,一定注意版本兼容的问题

        <!--接口依赖包-->
        <dependency>
            <groupId>city.albert</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--dubbo依赖包-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-apache-dubbo-adapter</artifactId>
        </dependency> 

  2、配置配置文件application.yml或者bootstrap.yml

spring:
  cloud:
    nacos:
    #指定注册中心需要引入spring-cloud-starter-alibaba-nacos-discovery依赖哦!本章不是注册nacos重点不做解
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: service-code8081
  main:
    allow-bean-definition-overriding: true
dubbo:
  #dubbo扫描dubbo注解报名
  scan:
    base-packages: city.albert
  protocol:
    # dubbo 协议
    name: dubbo
    # dubbo 协议端⼝( -1 表示⾃增端⼝,从 20880 开始)
    port: -1
  cloud:
  # 订阅服务提供⽅的应⽤列表,订阅多个服务提供者使⽤ "," 连接
    subscribed-services: service-email8082
  registry:
    #注册到springcloud服务
    address: spring-cloud://localhost

  3、实现接口

  使用org.apache.dubbo.config.annotation包下的@Reference注入调用dubbo接口实现类

    @Reference
    EmailCodeService emailClient;

四、启动两个项目

  观察nacos注册中心是否存在服务,存在则进行调用测试即可

 

更多:Dubbo与Zookeeper

posted @ 2020-10-15 02:07  CLASSIC~W  阅读(363)  评论(0编辑  收藏  举报