基于Springboot+Dubbo+Nacos 注解方式实现微服务调用

基于Springboot+Dubbo+Nacos 注解方式实现微服务调用

  • 一、项目结构
  • 二、spring-boot-dubbo-base工程
  • 三、spring-boot-dubbo-consumer工程
  • 四、spring-boot-dubbo-provider工程
  • 五、顺序启动ProviderApplication、ConsumerApplication发现服务已经注册到nacos

一、项目结构

基于Springboot+Dubbo+Nacos 注解方式实现微服务调用_第1张图片
源码下载
启动nacos服务
基于Springboot+Dubbo+Nacos 注解方式实现微服务调用_第2张图片

二、spring-boot-dubbo-base工程

1、pom.xml

<dependencies>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
    dependencies>

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

2、ProviderDTO(RPC接口DTO)

/**
 * RPC接口DTO
 * 注意这里要实现序列化接口
 * @author zhang
 */
@Data
@Builder
public class ProviderDTO implements Serializable {
    /**
     * ID
     */
    private Integer id;
    /**
     * 名字
     */
    private String name;
    /**
     * 序号
     */
    private Integer number;
}

3、IProviderService(RPC接口)

/**
 * RPC接口
 * @author zhang
 */
public interface IProviderService {
    /**
     * 查询接口
     * @return List
     */
    List<ProviderDTO> queryList();
}

4、ResultVO(返回结果类)

/**
 * 返回结果类
 * 这里采用构建者模式构建
 * 优点:1.私有化构造器访问范围小 2.参数可灵活设置便于管理
 * @author zhang
 */
@Getter
public class ResultVO<T> implements Serializable {

    /**
     * 返回码
     */
    private Integer code;
    /**
     * 返回信息
     */
    private String message;
    /**
     * 返回数据
     */
    private T data;

    /** 私有化构造器  **/
    private ResultVO() {}
    private ResultVO(ResultVO<T> resultVO) {
        this.code = resultVO.code;
        this.message = resultVO.message;
        this.data = resultVO.data;
    }

    /**
     * Build
     */
    public static class Builder<T>{
        private ResultVO<T> resultVO;
        public Builder() {
            resultVO = new ResultVO<>();
        }
        public Builder code(Integer code){
            resultVO.code = code;
            return this;
        }
        public Builder message(String message){
            resultVO.message = message;
            return this;
        }
        public Builder data(T data){
            resultVO.data = data;
            return this;
        }
        public ResultVO<T> build(){
            return new ResultVO<>(resultVO);
        }
    }
}

三、spring-boot-dubbo-consumer工程

1、pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>com.sansgroupId>
            <artifactId>spring-boot-dubbo-baseartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>2.7.0version>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubboartifactId>
            <version>2.7.0version>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-registry-nacosartifactId>
            <version>2.7.1version>
        dependency>
        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-clientartifactId>
            <version>1.0.0version>
        dependency>
    dependencies>

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

2、application.yml

server:
  port: 8862
dubbo:
  # 配置服务信息
  application:
    name: dubbo-consumer
    # 禁用QOS同一台机器可能会有端口冲突现象
    qos-enable: false
    qos-accept-foreign-ip: false
  # 配置注册中心
  registry:
    address: nacos://127.0.0.1:8848
  # 设置超时时间
  consumer:
    timeout: 4000
spring:
  main:
    # 解决Bean重复定义问题
    allow-bean-definition-overriding: true

3、ConsumerApplication(消费主启动类)

/**
 * 开启dubbo的注解支持
 * @author zhang
 */
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

4、ConsumerController(消费测试接口)

/**
 * 消费测试接口
 * @author zhang
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    /**
     * Dubbo远程调用注解
     * */
    @Reference
    private IProviderService providerService;

    @GetMapping ("/list")
    public ResultVO getList(){
        // 远程调用
        List<ProviderDTO> providerDTOList = providerService.queryList();
        return new ResultVO.Builder().code(200).message("success").data(providerDTOList).build();
    }
}

四、spring-boot-dubbo-provider工程

1、pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>com.sansgroupId>
            <artifactId>spring-boot-dubbo-baseartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>2.7.0version>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubboartifactId>
            <version>2.7.0version>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-registry-nacosartifactId>
            <version>2.7.1version>
        dependency>
        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-clientartifactId>
            <version>1.0.0version>
        dependency>
    dependencies>

2、application.yml

server:
  port: 8861
dubbo:
  # 配置服务信息
  application:
    name: dubbo-provider
    # 禁用QOS同一台机器可能会有端口冲突现象
    qos-enable: false
    qos-accept-foreign-ip: false
  # 配置注册中心
  registry:
    address: nacos://127.0.0.1:8848
  # 设置协议-协议由提供方指定消费方被动接受
  protocol:
    name: dubbo
    port: 20880
spring:
  main:
    # 解决Bean重复定义问题
    allow-bean-definition-overriding: true

3、ProviderServiceImpl(生产者Dubbo接口实现)

/**
 * 生产者Dubbo接口实现
 * @author zhang
 */
@Service
public class ProviderServiceImpl implements IProviderService {
    @Override
    public List<ProviderDTO> queryList() {
        // 初始化数据,模拟从数据库获取数据,组装数据
        List<ProviderDTO> list = new ArrayList<>();
        list.add(ProviderDTO.builder().id(1).name("张三丰").number(100).build());
        list.add(ProviderDTO.builder().id(2).name("张翠山").number(101).build());
        list.add(ProviderDTO.builder().id(3).name("张无忌").number(102).build());
        return list;
    }
}

4、ProviderApplication(生产主启动类)

/**
 * 开启Dubbo的注解支持
 * @author zhang
 */
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

五、顺序启动ProviderApplication、ConsumerApplication发现服务已经注册到nacos

基于Springboot+Dubbo+Nacos 注解方式实现微服务调用_第3张图片
利用postman测试
基于Springboot+Dubbo+Nacos 注解方式实现微服务调用_第4张图片

你可能感兴趣的:(Dubbo,spring,boot,dubbo,nacos)