LuoliangDSGA’s blog blog address: Luoliangdsga.github. IO welcome to reprint, reprint please indicate the author and source, thank you!

SpringBoot integrates Dubo2.5.10, using the latest official Spring-boot-starter

start

Dubbo has entered the Apache incubator, and the official Spring-boot-Starter 0.1.0 has been released to simplify the configuration of Dubbo applications, including Autoconfigure, Externalized-configuration (externalized configuration) and actuator(production preparation). For details, see the official Github Dubbo-spring-boot-starter.

The preparatory work

You need to install JDK1.8, Maven, and Zookeeper in advance.

Initialize the Maven project

For clarity, use a modular Maven project. Pom files are as follows:

<?xml version="1.0" encoding="UTF-8"? >
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.boot.dubbo</groupId>
    <artifactId>boot-dubbo</artifactId>
    <version>1.0.0 - the SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>boot-dubbo</name>
    <description>Dubbo project for Spring Boot</description>

    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
        <module>dubbo-api</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <dubbo-spring-boot-starter.version>0.1.0 from</dubbo-spring-boot-starter.version>
        <springboot.version>1.5.8. RELEASE</springboot.version>
        <fastjson-version>1.2.31</fastjson-version>
        <zk-client.version>0.2</zk-client.version>
        <dockerfile-maven.version>1.4.3</dockerfile-maven.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo-spring-boot-starter.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
                <version>${springboot.version}</version>
            </dependency>

            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zk-client.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>


Copy the code

It is mainly divided into three modules: API, Provider and Consumer

Creating a producer

With spring-boot-starter, dubbo is easy to configure. Instead of having to configure a bunch of XML files, dubbo can be configured right out of the box with a few simple configurations.

  1. Configure the producer’s POM.xml first
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>org.boot.dubbo</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <parent>
        <artifactId>boot-dubbo</artifactId>
        <groupId>org.boot.dubbo</groupId>
        <version>1.0.0 - the SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <name>dubbo-provider</name>
    <description>Dubbo project for Spring Boot:Provider</description>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

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

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>zookeeper</artifactId>
                    <groupId>org.apache.zookeeper</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.boot.dubbo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>${parent.version}</version>
        </dependency>
    </dependencies>

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

Copy the code
  1. Then configure SpringBoot and Dubbo with properties as follows:
   spring.application.name=springboot-dubbo-provider
   server.port=9090
   # dubbo configuration
   dubbo.application.id=springboot-dubbo-provider
   dubbo.application.name=springboot-dubbo-provider
   dubbo.application.owner=luoliang
   # Protocol configuration
   dubbo.protocol.id=dubbo
   dubbo.protocol.name=dubbo
   # change the default port 20880 to 12345
   dubbo.protocol.port=12345
   Service registration configuration
   dubbo.registry.id=my-registry
   dubbo.registry.address=zookeeper://localhost:2181
   # configure dubbo package scan for dubbo @service, @reference annotation
   dubbo.scan.base-packages=org.boot.dubbo.provider.service
   # Dubbo Health monitoring
   endpoints.dubbo.enabled=true
   management.health.dubbo.status.defaults=memory
   management.health.dubbo.status.extras=load,threadpool
   management.port=9091
Copy the code
  1. After the above two steps, Dubbo is now integrated, and it is time to roll out the service code directly, using annotations to expose the service interface
  • I’m going to write an interface in my API
public interface HelloService {
   String sayHello(String name);
}
Copy the code
  • Implement the interface, plus its own business logic
@Service(version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}")
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "Hello, " + name + " (from Spring Boot)"; }}Copy the code

Notice that the service annotations are com. Alibaba. Dubbo. Config. The annotation. Service

Create consumers

  1. Configure the consumer’s POM.xml
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>org.boot.dubbo</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <parent>
        <artifactId>boot-dubbo</artifactId>
        <groupId>org.boot.dubbo</groupId>
        <version>1.0.0 - the SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <name>dubbo-consumer</name>
    <description>Dubbo project for Spring Boot:Consumer</description>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

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

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.boot.dubbo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>${parent.version}</version>
        </dependency>
    </dependencies>

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

Copy the code
  1. Application.properties is configured as follows:
spring.application.name=springboot-dubbo-consumer
server.port=8081
# dubbo configuration
dubbo.application.id=springboot-dubbo-consumer
dubbo.application.name=springboot-dubbo-consumer
dubbo.application.owner=luoliang
Service registration configuration
dubbo.registry.id=my-registry
dubbo.registry.address=zookeeper://localhost:2181
management.port=8082
Copy the code
  1. Write a service to consume dubbo’s services as follows:
@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Reference(version = "1.0.0",
            application = "${dubbo.application.id}")
    private HelloService helloService;

    @Override
    public String sayHello(String name) {
        returnhelloService.sayHello(name); }}Copy the code
Inject this service into the MVC controllerCopy the code
@RestController
@RequestMapping("/user")
public class DefaultController {
    @Resource
    private ConsumerService consumerService;

    @RequestMapping("/sayHello")
    public String register(String name) {
        returnconsumerService.sayHello(name); }}Copy the code
By now, the basic structure of the whole project has been set up and consumers can consume the services provided by the provider.

To test now, start the provider and consumer respectively, open a browser, type http://localhost:8081/user/sayHello? name=dubbo

As you can see, the results returned are as expected, indicating that the project has been successfully integrated

Need source code please go to my Github, if you can smoothly star better! Boot-dubbo, next blog, learn how to use the Docker container SpringBoot+Dubbo application.

First time in nuggets, you big guy light spray ~

reference

  • https://github.com/apache/incubator-dubbo-spring-boot-project