spring boot integrated dubbo enterprise full

1, What is Spring Boot?

Spring Boot at this stage is too hot. Why? Because it is easy to use, simple to configure and fast to start, what is it? From the official website, we can see that it is a subproject of spring open source organization, which mainly simplifies the heavy configuration of spring, and Spring Boot embeds various Servlet containers, such as Tomcat, Jetty, etc

Official website: http://projects.spring.io/spring-boot/ GitHub source code: https://github.com/spring-projects/spring-boot

2, What are the advantages of Spring Boot?

1. Stand alone: you don't need to be running with containers like tomcat. 2. Simplified configuration: you don't need to configure a lot of xml like Spring mvc; 3. Auto configuration: automatically configure bean s according to package path 4. Application monitoring: Spring Boot provides monitoring services

3, Project creation

1. Create provider

After that, click Finish to finish the creation, and then delete the redundant packages, so that the project structure is as follows:

Right click the project, and create a new QBs facade that provides external services for the provider

Then create a QBs web module according to the pattern (not covered here). The final project structure is shown in the following figure:

Modify the main pom 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <groupId>com.btd</groupId>
    <artifactId>qbs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>qbs</name>

    <modules>
        <module>qbs-facade</module>
        <module>qbs-api</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/>
    </parent>

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

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

</project>

3. Write a facade

SayFacade.java

package com.btd.qbs.facade;

public interface SayFacade {
    String say(String context);
}

The xml file of the facade module only provides an external interface, so it doesn't need anything else

<?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.btd</groupId>
    <artifactId>qbs-facade</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>qbs-facade</name>
    <packaging>jar</packaging>

</project>

4. Organization api module, implementation of actual interface

First look at the pom.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">
    <parent>
        <artifactId>qbs</artifactId>
        <groupId>com.btd</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>qbs-api</artifactId>
    <packaging>jar</packaging>


    <dependencies>
        <!-- spring boot Relevant start  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>
        </dependency>
        <!-- spring boot Relevant end  -->

        <!--dubbo Relevant-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
        </dependency>
        <!-- dubbo Dependent dependence end-->

        <dependency>
            <groupId>com.btd</groupId>
            <artifactId>qbs-facade</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

application.properties file

spring.application.name=qbs-provider

server.port=11222

dubbo.application.id=${spring.application.name}
dubbo.application.name=${spring.application.name}
dubbo.protocol.port = 28820
dubbo.protocol.name=${spring.application.name}

# zk registration center address
dubbo.registry.address=zookeeper://127.0.0.1:2181

# Provider configuration
dubbo.provider.name=dubbo
dubbo.provider.protocol=dubbo
dubbo.provider.version=1.0.0
dubbo.provider.timeout=30000

SayFacadeImpl.java

package com.btd.qbs.service;

import com.btd.qbs.facade.SayFacade;
import org.apache.dubbo.config.annotation.Service;

@Service
public class SyaFacadeImpl implements SayFacade {

    @Override
    public String say(String context) {
        return "Little fat sheep said to you:"+context;
    }
}

Startup file Application.java

package com.btd.qbs;

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

@EnableDubbo
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

OK, so far, our provider is finished. Start the project. Then let's take a look at Dubbo admin

Note here that since our facade s need to be mapped in, we need to make a jar package

After the package is completed, we will get a jar file

2. Building consumers

Introduce our jar package

Profile:

spring.application.name=qbs-consumer

server.port=11121
dubbo.application.id=${spring.application.name}
dubbo.application.name=${spring.application.name}
dubbo.protocol.port=28820
dubbo.protocol.name=dubbo

# zk registration center address
dubbo.registry.address=zookeeper://172.25.37.130:2181

# Consumer configuration
dubbo.consumer.version=1.0.0
dubbo.consumer.check=false
dubbo.consumer.timeout=8000

pom.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 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.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.btd.abs</groupId>
    <artifactId>qbs-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>qbs-consumer</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <!-- spring boot Relevant start  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>
        </dependency>
        <!-- spring boot Relevant end  -->

        <!--dubbo Relevant-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
        </dependency>
        <!-- dubbo Dependent dependence end-->

        <!-- Basic dependence start -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.59</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Basic dependence end -->

    </dependencies>

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

Application.java file

package com.btd.abs;

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

@EnableDubbo
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
public class Application {

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

DbsController.java file

package com.btd.abs.controller;

import com.btd.qbs.facade.SayFacade;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/qbs")
public class DbsController {

    @Reference
    private SayFacade sayFacade;

    @GetMapping("/say")
    @ResponseBody
    public String say(String context) {
         return sayFacade.say(context);
    }
}

Last call result: http://localhost:11121/qbs/say?context=asde3

OK, this is a complete set of commercial code examples of integrating dubbo with spring boot. It is completely OK according to the top-down operation; If you are interested, remember to pay attention to my public number!

Original blog, reprint please state reprint address, thank you!!!!!!

Keywords: Programming Dubbo Spring Apache Maven

Added by darknuke on Wed, 15 Jan 2020 13:49:27 +0200