原创

Spring Boot集成dubbo快速入门Demo

1.什么是dubbo?

Apache Dubbo 是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。同时 Dubbo 是高度可扩展的,用户几乎可以在任意功能点去定制自己的实现,以改变框架的默认行为来满足自己的业务需求。

2.zookeeper环境搭建

Dubbo使用zookeeper作为注册中心,所以需要安装zookeeper。
version: '3'
services:
  zookeeper:
    image: zookeeper:3.7.0
    container_name: zookeeper
    restart: unless-stopped
    volumes:
      - "./zookeeper/data:/data"
      - "./zookeeper/datalog:/datalog"
    ports:
      - "2181:2181"

  # webui
  zookeeper-webui:
    image: tobilg/zookeeper-webui
    container_name: zookeeper-webui
    restart: unless-stopped
    environment:
      ZK_DEFAULT_NODE: zookeeper:2181
    depends_on:
      - zookeeper
    links:
      - zookeeper
    ports:
      - "8089:8080"
启动
docker-compose -f docker-compose-zookeeper.yml -p zookeeper up -d
可视化界面访问地址:[http://ip地址:8089] ,输入 [{宿主主机ip}:2181/]进入 桌面可视化工具PrettyZoo: https://github.com/vran-dev/PrettyZoo

3.代码工程

实验目的:实现dubbo远程调用 18  

dubbo-samples-spring-boot-interface

api层,暴露给consumer和provider引用
<!--api  -->
<dependency>
    <groupId>com.et</groupId>
    <artifactId>dubbo-samples-spring-boot-interface</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
package com.et.api.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
    private static final long serialVersionUID = -4294369157631461921L;
    Long userId;
    String userName;
    String userInfo;
}

package com.et.api;

import com.et.api.entity.User;

public interface UserService {
    String getUserInfo();

    User getUserById(String userId);
}

dubbo-samples-spring-boot-provider

服务提供层,把服务注册到zookeeper上
package com.et.provider.service;

import com.et.api.UserService;
import com.et.api.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

/**
 * 注意,@service注解,引入的包是org.apache.dubbo.config.annotation.Service
 */
@Service
@Component
@Slf4j
public class UserServiceImpl implements UserService {

    @Override
    public String getUserInfo() {
        log.info("this is a test");
        return "userTest";
    }

    @Override
    public User getUserById(String userId) {
        log.info("invoke getUserById method");
        User user = new User();
        user.setUserId(Long.valueOf(userId));
        user.setUserInfo("test");
        user.setUserName("lin");
        return user;
    }

}
package com.et.provider;

import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * DubboComponentScan service package
 */
@SpringBootApplication
@DubboComponentScan("com.et.provider.service")
public class DubboProviderApplication {

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

}
dubbo:
  application:
    # 应用名称
    name: dubbo-provider
  protocol:
    # 协议名称
    name: dubbo
    # 协议端口
    port: 20880
  registry:
    # 注册中心地址
    address: zookeeper://127.0.0.1:2181

server:
  # 修改端口号,避免端口冲突
  port: 8081

dubbo-samples-spring-boot-consumer

消费应用,调用provider服务
package com.et.consumer.controller;


import com.et.api.UserService;
import com.et.api.entity.User;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class ConsumerController {
    /**
     *  @Reference invoker dubbo-provider
     */
    @Reference
    private UserService userService;


    @GetMapping("/info")
    public String getUserById() {
        return userService.getUserInfo();
    }


    @GetMapping("/{id}")
    public User getUserById(@PathVariable String id) {
        return userService.getUserById(id);
    }

}

4.测试

启动provider和consumer应用
  • 访问http://127.0.0.1:8080/user/info
  • 访问http://127.0.0.1:8080/user/1

5.引用参考

 
正文到此结束
Loading...