亲宝软件园·资讯

展开

Spring Boot Dubbo 构建分布式服务的方法

人气:0

概述:

 

节点角色说明

节点 角色说明
Provider 暴露服务的服务提供方
Consumer 调用远程服务的服务消费方
Registry 服务注册与发现的注册中心
Monitor 统计服务的调用次数和调用时间的监控中心
Container 服务运行的容器

调用关系说明

 项目构建

开发环境主要涉及以下方面:

具体代码可以查看 github 的 dubbo 模块: https://github.com/UniqueDong/springboot-study

Dubbo API

定义服务接口,打成 jar 包让消费者依赖,服务者实现接口。该工程只有接口定义以及 model 对象。@Data 属于lombok 开源库提供的特性,方便开发。

model 对象定义:

@Data
public class User implements Serializable {
  private Long id;
  private String username;
}

provider 接口定义:

public interface UserProvider {
  List<User> listUser();
}

Provider 服务提供者

pom依赖:

引入spring-boot-starter,dubbo-api 接口就是我们上面提到的 接口定义 jar ,dubbo-spring-boot-starter ,dubbo-dependencies-zookeeper。

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <artifactId>spring-boot-starter-logging</artifactId>
          <groupId>org.springframework.boot</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>zero.springboot.study</groupId>
      <artifactId>dubbo-api</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <!--dubbo start-->
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>2.7.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.7.1</version>
    </dependency>

    <!-- Zookeeper dependencies -->
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-dependencies-zookeeper</artifactId>
      <version>2.7.1</version>
      <type>pom</type>
      <exclusions>
        <exclusion>
          <artifactId>log4j</artifactId>
          <groupId>log4j</groupId>
        </exclusion>
        <exclusion>
          <artifactId>slf4j-log4j12</artifactId>
          <groupId>org.slf4j</groupId>
        </exclusion>
      </exclusions>
    </dependency>

    <!--dubbo end-->
    
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.57</version>
    </dependency>
  </dependencies>

配置文件 yaml 定义:

spring:
 application:
  name: dubbo-provider
#自定义配置
embedded:
 zookeeper:
  # zookeeper 服务连接端口
  port: 2181

# dubbo 配置
dubbo:
 # 注册中心配置 
 registry:
  id: dubbo-provider
  address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
  group: local
 application:
  name: dubbo-provider
  id: dubbo-provider
  logger: slf4j
  qosEnable: true
  qosPort: 22224
  qosAcceptForeignIp: false
 # dubbo 协议配置
 protocol:
  # -1 表示使用随机未被占用的端口
  port: -1
  name: dubbo
 scan:
  # dubbo 服务提供者实现类所在包
  base-packages: com.zero.provider.impl

实现 api 定义的接口

注意 @Service 是 Dubbo 的,不要导入了 Spring 的。

import com.google.common.collect.Lists;
import com.zero.api.model.User;
import com.zero.api.provider.UserProvider;
import org.apache.dubbo.config.annotation.Service;

import java.util.List;

@Service(interfaceClass = UserProvider.class)
public class UserProviderImpl implements UserProvider {
  @Override
  public List<User> listUser() {
    User user = new User();
    user.setId(1L);
    user.setUsername("青龙");
    return Lists.newArrayList(user);
  }
}

Consumer

Pom 定义:

我们要依赖 spring-boot-starter-web 提供http rest接口给前端调用。同时内部通过 Dubbo 实现 RPC调用服务提供者。

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>zero.springboot.study</groupId>
      <artifactId>dubbo-api</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <!--dubbo start-->
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>2.7.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.7.1</version>
    </dependency>

    <!-- Zookeeper dependencies -->
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-dependencies-zookeeper</artifactId>
      <version>2.7.1</version>
      <type>pom</type>
      <exclusions>
        <exclusion>
          <artifactId>log4j</artifactId>
          <groupId>log4j</groupId>
        </exclusion>
        <exclusion>
          <artifactId>slf4j-log4j12</artifactId>
          <groupId>org.slf4j</groupId>
        </exclusion>
      </exclusions>
    </dependency>

    <!--dubbo end-->

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

  </dependencies>

yaml 定义:

server:
 # web 日更年期端口
 port: 9005
spring:
 application:
  name: dubbo-comsumer
#自定义配置
embedded:
 zookeeper:
  port: 2181
# dubbo 配置
dubbo:
 registry:
  id: dubbo-comsumer
  address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
  group: local
 application:
  name: dubbo-comsumer
  id: dubbo-comsumer
  logger: slf4j
  qosEnable: false
  qosPort: 22223
  qosAcceptForeignIp: false
 protocol:
  port: -1
  name: dubbo
 # 是否检查服务提供者有效 
 consumer:
  check: false

服务消费者调用服务生产者

import com.zero.api.model.User;
import com.zero.api.provider.UserProvider;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

  @Reference
  private UserProvider userProvider;

  public List<User> listUser() {
    return userProvider.listUser();
  }
}

我们通过一个RESTfull接口,提供给前端调用。

@RestController
@RequestMapping("/users")
public class UserController {
  @Autowired
  private UserService userService;

  @GetMapping
  public Object listUser() {
    List<User> list = userService.listUser();
    return list;
  }
}

总结

各种具体协议、注册中心、多注册中心、超时等配置可以查看官方文档 http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论