Popular Dubbo tutorial (2): Integration of Dubbo and Spring Boot

ready

First of all, we need to build the zookeeper environment of Dubbo's registration center and the dubbo-admin platform of Dubbo. Since this is not the focus of this article, I will skip it here. Readers who need to know the details can refer to the relevant information.

Build the project

We create a Spring Boot project called dubbo-test, and then create three new modules in the project, as follows:

  1. api: interface layer, store the interface called by the provider and the consumer
  2. producer: provider, used to provide interface implementation to consumers
  3. consumer: Consumer, call the interface implementation provided by the provider remotely

The project directory is as follows:
Insert picture description here

API module implementation

We define an interface in the api module, which is used to modify the name.

package edu.szu.api.service;

/**
 * 修改名称的接口
 */
public interface NameService {
    String updateName(String name);
}

producer module implementation

First of all, we have to introduce the relevant dependency dubbo-spring-boot-starter and the api module implemented above in the producer module. The pom file is implemented as follows:

<?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.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>edu.szu</groupId>
    <artifactId>producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>producer</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>
        <!-- 引入api模块 -->
        <dependency>
            <groupId>edu.szu</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 引入dubbo环境 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

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

</project>

Next we need to write the implementation of the NameService interface defined by the api module. It should be noted that we need to add @ com.alibaba.dubbo.config.annotation.Service annotation on the implementation class.

package edu.szu.producer.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import edu.szu.api.service.NameService;
import org.springframework.stereotype.Component;

@Component
@Service
public class NameServiceImpl implements NameService {

    @Override
    public String updateName(String name) {
        return "修改后的名称:" + name;
    }
}

Then add the @EnableDubbo annotation on the startup class to start the Dubbo service

package edu.szu.producer;

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

@SpringBootApplication
@EnableDubbo
public class ProducerApplication {

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

}

Finally, modify the configuration file. In fact, the comments of the configuration file are already very detailed, so I will not introduce more specific meanings of each configuration here.

server.port=8080
#提供方应用信息,用于计算依赖关系
dubbo.application.name=producer
#注册中心地址
dubbo.registry.address=127.0.0.1:2181
#使用zookeeper作为注册中心
dubbo.registry.protocol=zookeeper
#使用dubbo协议
dubbo.protocol.name=dubbo
#使用dubbo协议在20880端口暴露服务
dubbo.protocol.port=20880

consumer module implementation

First introduce the relevant dependency dubbo-spring-boot-starter and api module, the pom file is as follows:

<?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.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>edu.szu</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.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-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 引入api模块 -->
        <dependency>
            <groupId>edu.szu</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 引入dubbo环境 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

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

</project>

Then we define the relevant interface implemented in the consumer

package edu.szu.consumer.service;

public interface ChangeService {
    String change(String name);
}

Then provide an implementation of the interface. Here we call the remote service nameService. It should be noted that we need to add a @Reference annotation to the called remote service interface.

package edu.szu.consumer.serviceImpl;

import com.alibaba.dubbo.config.annotation.Reference;
import edu.szu.api.service.NameService;
import edu.szu.consumer.service.ChangeService;
import org.springframework.stereotype.Component;

@Component
public class ChangeServiceImpl implements ChangeService {

    @Reference
    NameService nameService;

    @Override
    public String change(String name) {
        return nameService.updateName(name);
    }
}

Then we simply implement the control layer

package edu.szu.consumer.controller;

import edu.szu.consumer.service.ChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChangeController {

    @Autowired
    private ChangeService changeService;

    @RequestMapping("/change/{name}")
    public String change(@PathVariable("name") String name) {
        return changeService.change(name);
    }

}

Then add the @EnableDubbo annotation on the startup class to start the Dubbo service

package edu.szu.consumer;

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

@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {

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

}

Finally modify the configuration file

server.port=8081
#提供方应用信息,用于计算依赖关系
dubbo.application.name=consumer
#注册中心地址
dubbo.registry.address=127.0.0.1:2181
#使用zookeeper作为注册中心
dubbo.registry.protocol=zookeeper

test

We open the zookeeper of Dubbo's registration center, and then start the producer and consumer respectively, enter http: // localhost: 8081 / change / HelloDubbo on the browser, and find that the display is as follows: it
Insert picture description here
shows that the consumer has successfully called the service provided by the provider remotely .

Then we start dubbo-admin, which displays as follows:
Insert picture description here
dubbo-admin has successfully monitored our providers and consumers, and at the same time said that our Dubbo and Spring Boot integration was successful.

Published 151 original articles · praised 317 · 40,000+ views

Guess you like

Origin blog.csdn.net/Geffin/article/details/105663545