Dubbo official website: dubbo.apache.org/zh-cn/


preface

I will use a small demo to show how to integrate with Spring Boot;

If you haven’t used Spring Boot or Dubbo before, I recommend you familiarize yourself with it, because this article doesn’t cover how to install it.

This demo is a small demo to get user order information

The demo is currently on my Github: github.com/aaaTao66/du…

The development environment

  • win10
  • idea 2019
  • zookeeper
  • dubbo
  • jdk1.8

Create a project

  • First, we will create a Maven project not Spring Boot. This project is called XXX-Interface.

I’m going to call it gmall-interface

According to Dubbo’s official documentation of service optimization practices:

It is recommended to put service interfaces, service models, service exceptions, and so on in the API package, because service models and exceptions are part of the API, and this is also consistent with the principles of subcontracting: Reuse publish Equivalence (REP), common Reuse (CRP).

That’s why we created gmall-Interface, which is to put Java beans and interfaces in this project and make every microservice project depend on it.

  • We then create two Spring Boot projects, one provider and one consumer.

Provider project name: boot-user-service-provider; (User, can not tick anything)

Consumer project name: boot-order-service-consumer; (Order, remember to check web when initializing)

Write a program

Project: gmall – interface

  • Start by creating a JavaBean:
package com.carson.gmall.bean;


import java.io.Serializable;

public class UserAddress implements Serializable {

    private Integer id;
    private String userAddress; // User address
    private String userId; / / user id
    private String consignee; / / consignee
    private String phoneNum; // Phone number
    private String isDefault; // Default address yes/no
   
    // omit methods such as get set toString
Copy the code

Remember to implement serialization

For example, if our client A wants to call A method on client B, server A needs to establish A connection with server B, and server A needs to pass in parameters when calling A method, and this parameter needs to be passed across the network, we need to serialize.

Then server B finds that an external server wants to call my method and sends me parameters. Since it is serialized, server B deserializes the parameters.

And then after the method call, we have A return value, which is basically the same thing as before, serialize it, and then A deserializes it, so we can get the result back.

  • Establish interface

OrderService:

import com.carson.gmall.bean.UserAddress;

import java.util.List;

/** * User service interface ** /
public interface UserService {

    /* Returns all harvest addresses by user ID * */
    List<UserAddress> getUserAddressList(String userId);
}
Copy the code

UserService:

import com.carson.gmall.bean.UserAddress;

import java.util.List;

/** * User service interface ** /
public interface UserService {

    /* Returns all harvest addresses by user ID * */
    List<UserAddress> getUserAddressList(String userId);
}
Copy the code
  • Pom. The XML file
<?xml version="1.0" encoding="UTF-8"? >

<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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.carson.gmall</groupId>
  <artifactId>gmall-interface</artifactId>
  <version>1.0 the SNAPSHOT</version>

<dependencies>
    
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.8</version>
  </dependency>
    
  <dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.8.0</version>
  </dependency>
    
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
  </dependency>
    
</dependencies>
</project>

Copy the code

Provider project :boot-user-service-provider

  • pom.xml

Dubbo’s 0.2.0 release is for Spring Boot 2.x.

If Spring Boot is 1.x, dubbo should use 0.1.0.

Associate the interface with gmall-interface.<dependency>
            <groupId>com.carson.gmall</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0 the SNAPSHOT</version>
        </dependency>Dubbo rely on<dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version> 
        </dependency>
Copy the code

Then there is the Spring Boot configuration file:

  • application.yml
dubbo:
  application:
    name: boot-order-service-consumer
  registry:
    address: Zookeeper: / / 127.0.0.1:2181
  monitor:
    protocol: registry
server:
  port: 8081
Copy the code

Provider. XML: provider. XML: provider.

Specific website: dubbo.apache.org/zh-cn/docs/…

However, there is no service interface declared to be exposed in my configuration, which will be explained later.

  • The implementation class of UserService

import com.alibaba.dubbo.config.annotation.Service;
import com.carson.gmall.bean.UserAddress;
import com.carson.gmall.bootuserserviceprovider.service.UserService;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Service // Expose service
@Component
public class UserServiceImpl implements UserService {

    public List<UserAddress> getUserAddressList(String userId) {

        UserAddress address1 = new UserAddress(1."Hengshui, Hebei Province"."1"."carson"."12345678911"."Y");
        UserAddress address2 = new UserAddress(1.Dezhou City, Shandong Province."2"."eason"."4562144"."Y");

        returnArrays.asList(address1,address2); }}Copy the code

Note my @service annotation, which is not Spring’s but Dubbo’s:

import com.alibaba.dubbo.config.annotation.Service;

This annotation means: declare the service interface to be exposed;

In this method we can see that I set two harvest addresses and return them as a list.

  • Spring Boot the main class

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@EnableDubbo // Enable annotation-based dubbo
@SpringBootApplication
public class BootUserServiceProviderApplication {

    public static void main(String[] args) { SpringApplication.run(BootUserServiceProviderApplication.class, args); }}Copy the code

Consumer project :boot-order-service-consumer

The POM file is the same as the provider.

  • application.yml
dubbo:
  application:
    name: boot-order-service-consumer
  registry:
    address: Zookeeper: / / 127.0.0.1:2181
  monitor:
    protocol: registry
server:
  port: 8081
Copy the code

For details, you can refer to the quick start of Duboo official document, which is just the above website:

Dubbo.apache.org/zh-cn/docs/…

  • OrderServiceImpl
package com.carson.gmall.bootorderserviceconsumer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.carson.gmall.bean.UserAddress;
import com.carson.gmall.bootuserserviceprovider.service.OrderService;
import com.carson.gmall.bootuserserviceprovider.service.UserService;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/** * 1. Register the service provider to the registry (leaky service) * 1) import dubbo dependencies * 2) Configure the service provider ** 2. Let the service consumer go to the registry to subscribe to the service provider's service address * */
@Service
public class OrderServiceImpl implements OrderService {

    @Reference
    UserService userService;

    public List<UserAddress> initOrder(String userId) {
        System.out.println("User id."+userId);
        // 1. Query the shipping address of the user
        List<UserAddress> userAddressList = userService.getUserAddressList(userId);
        returnuserAddressList; }}Copy the code

Note @reference: remotely calling UserService will go to the registry to discover it

  • OrderController
@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ResponseBody
    @RequestMapping("/initOrder")
    public List<UserAddress> initOrder(@RequestParam("uid") String userId) {
        returnorderService.initOrder(userId); }}Copy the code

ResponseBody: Returns the result as a JSON response

  • Start the class
@EnableDubbo // Enable annotation-based dubbo
@SpringBootApplication
public class BootOrderServiceConsumerApplication {
    public static void main(String[] args) { SpringApplication.run(BootOrderServiceConsumerApplication.class, args); }}Copy the code

Like the provider


Start ZooKeeper and Dubbo and these two projects, which you can see in the Dubbo console, already have consumers and providers

Let’s try to access port 8081 with parameters:

Successfully obtained the order information;