How to integrate Dubbo and SpringBoot

Posted by blankextacy on Fri, 17 Jan 2020 05:41:57 +0100

1, How Dubbo integrates spring boot (1)

 

1) Go straight to the theme, way 1:

In pom.xml, Dubbo starter dependency is introduced. In the application.properties configuration, use @ Service [expose Service] and @ Reference [Reference Service], and select the annotation method of application.properties +.

 

2) Create an ego interface module, which is used by the provider and the consumer

The directory structure of this module is as follows:

1. Entity class UserAddress

package com.sxt.domain;
import java.io.Serializable;

//Entity class must implement serialization
public class UserAddress implements Serializable {

    private Integer id;
    private String address;
    private String userId;

    public UserAddress() {
    }
    public UserAddress(Integer id, String address, String userId) {
        this.id = id;
        this.address = address;
        this.userId = userId;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
}

2. Provider interface UserService

package com.sxt.service;
import com.sxt.domain.UserAddress;
import java.util.List;
//provider interface 
public interface UserService {

    //According to users id Query user address
    public List<UserAddress> getUserAddressByUserId(String userId);
}

3. Consumer interface OrderService

package com.sxt.service;
import com.sxt.domain.UserAddress;
import java.util.List;
//Consumer interface
public interface OrderService{

    //Initialize order
    public List<UserAddress> initOrder(String userId);
}

3) Create boot ego user service provider module

The directory structure of this module is as follows (config is used in the second way, please ignore here first):

4) Modify pom.xml to add dependency

Add some critical dependencies (others are generated automatically):

    <!--Join in ego_interface rely on-->
  <!--This is the dependency of the interface class you want to use created in step 2 above-->
<dependency> <groupId>com.sxt</groupId> <artifactId>ego_interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
<!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <!-- Use zk As a registry, Dubbo Dependency needed --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>

1. Write UserServiceImpl

package com.sxt.service.impl;

import com.sxt.domain.UserAddress;
import com.sxt.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import java.util.ArrayList;
import java.util.List;

//here service yes apache.dubbo Instead of creating and exposing objects
@Service
public class UserServiceImpl implements UserService {

    public static List<UserAddress> address=new ArrayList<>();

    static {
        address.add(new UserAddress(1, "tian'anmen square", "bj"));
        address.add(new UserAddress(2, "Shanghai Disneyland", "sh"));
    }


    @Override
    public List<UserAddress> getUserAddressByUserId(String userId) {
        return address;
    }
}

2. Modify application.properties

#application-name Module name
dubbo.application.name=boot-ego-user-service-provider
#Register specifies the address of the registration center (www.lcbxiuxiu.tech is my alicloud address, please change to your own server address)
dubbo.registry.address=zookeeper://www.lcbxiuxiu.tech:2181
#dubbo protocol specifies that the dubbo protocol exposes the service to port 20880
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

3. Modify startup class and start

@SpringBootApplication
//This note is for automatic opening dubbo
@EnableDubbo
public class BootEgoUserServiceProviderApplication {
  public static void main(String[] args) {
        SpringApplication.run(BootEgoUserServiceProviderApplication.class, args);
    }
}

4. After successful startup, the screen is accessed in the remote dubbo

5) Create the boot ego order service comsumer consumer module

The directory structure of this module is as follows (config is used in the second way, please ignore here first):

 

 

 

1. Modify pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sxt</groupId>
    <artifactId>boot-ego-order-service-comsumer</artifactId>
    <version>1.0</version>
    <name>boot-ego-order-service-comsumer</name>
    <description>springboot Integrate dubbo Consumers</description>

    <properties>
        <java.version>1.8</java.version>
        <dubbo.version>2.7.3</dubbo.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--Join in ego_interface rely on-->
        <dependency>
            <groupId>com.sxt</groupId>
            <artifactId>ego_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!-- Use zk As a registry, Dubbo Dependency needed -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

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

</project>

2. Create OrderServiceImpl

package com.sxt.service.impl;

import com.sxt.domain.UserAddress;
import com.sxt.service.OrderService;
import com.sxt.service.UserService;
import org.springframework.stereotype.Service;
import org.apache.dubbo.config.annotation.Reference;

import java.util.List;

@Service  //Here is spring's help to automatically create objects and inject
public class OrderServiceImpl implements OrderService {

    @Reference //Here is apache.dubbo instead of introducing remote objects
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List<UserAddress> initOrder(String userId) {
        return this.userService.getUserAddressByUserId(userId);
    }

}

3. Modify startup class and start

package com.sxt;

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

@SpringBootApplication
@EnableDubbo
public class BootEgoOrderServiceComsumerApplication {

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

4. Test in test class

package com.sxt;

import com.sxt.domain.UserAddress;
import com.sxt.service.OrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
class BootEgoOrderServiceComsumerApplicationTests {

    @Autowired
    OrderService orderService;

    @Test
        void contextLoads() throws IOException {
        List<UserAddress> userAddresses = orderService.initOrder("sxt");
        for (UserAddress userAddress : userAddresses) {
            System.out.println(userAddress.getId()+" "+userAddress.getAddress());
        }
        //Want to show consumer blocker stop on dubbo home page
        System.in.read();
    }

}

5. Modify application.properties and start the above test class

#application.name
dubbo.application.name=boot-ego-order-service-comsumer
#address
dubbo.registry.address=zookeeper://Own server address: 2181

Summary the first way:

1. The provider only needs to declare in application.properties: module name, registry address, connection rules (what protocol to use, what port to expose). The rest are created and exposed by the @ service(apache.dubbo) annotation in the service implementation class

2. Consumers only need to declare in application.properties: module name, registration center address. For the rest, the @ service(spring) annotation in the service implementation class helps to create and inject objects, and @ Reference(apache.dubbo) helps to introduce remote services. In the test class, @ Autowired can be used to assemble objects and use their methods

Topics: Java Dubbo Apache Spring