What is Spring Boot?

Spring Boot is so hot right now, why? Because it’s easy to use, simple to configure and quick to get started, what is it? As you can see from the official website, it is a sub-project of the Spring open source organization. It mainly simplifies the heavy configuration of Spring, and Spring Boot has various Servlet containers embedded, such as Tomcat and Jetty

The official web site: http://projects.spring.io/spring-boot/GitHub source: https://github.com/spring-projects/spring-boot

Ii. Advantages of Spring Boot?

1. Independent operation: no need to run in a container such as Tomcat. 2. Simplified configuration: You don’t need to configure as much XML as Spring MVC does; Automatic configuration: Automatically configure beanbased on the package path. 4. Application monitoring: Spring Boot provides monitoring services

3. Project creation

1. Create a provider

Click Finish to create the project, and then delete the extra packages so that the project structure looks like this:

Right-click on the project and create a QBS -facade for the provider to provide services externally

Then follow this pattern to create a QBS-Web module (not covered here). The final project structure is shown below:

Modify the master POM file

<? The 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> < dubo.version >2.7.1</ dubo.version > </properties> <build> <plugins> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Write a facade

SayFacade.java

package com.btd.qbs.facade;

public interface SayFacade {
    String say(String context);
}Copy the code

The XML file for the facade module, which only provides the interface externally, so nothing else is required

<? The 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 - the facade < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < name > QBS - the facade < / name > < packaging > jar < / packaging > < project >Copy the code

4. Organize API modules and implement actual interfaces

Start with the pom.xml file

<? The 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" > < the parent > < artifactId > QBS < / artifactId > < groupId > com. BTD < / groupId > < version > 0.0.1 - the SNAPSHOT < / version > < / parent > <modelVersion>4.0.0</modelVersion> <artifactId> QBS-API </artifactId> <packaging>jar</packaging> <dependencies> <! <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 end --> <! Dubbo --> <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> <! End --> <dependency> <groupId>com.btd</groupId> <artifactId>qbs-facade</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> </project>Copy the code

Application. The 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. The name} # zk registry address dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181 # provider configuration Dubbo. The provider. The name = dubbo dubbo. The provider. The protocol = dubbo dubbo. The provider. The version = 1.0.0 dubbo. The provider. A timeout = 30000Copy the code

SayFacadeImpl.java

package com.btd.qbs.service; import com.btd.qbs.facade.SayFacade; import org.apache.dubbo.config.annotation.Service; @service public class implements SayFacade {@override public String say(String context) {return "+context; }}Copy the code

Start the 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); }}Copy the code

OK, at this point, our provider is done, starts the project, and then we look at Dubbo-admin

Note here that because our facade needs to be externally projected, we put a JAR package

After completing the package, we get a JAR file

2. Build customers

Introduce the JAR package we hit

Configuration file:

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 registry address Dubbo. Registry. Address = zookeeper: / / 172.25.37.130:2181 # configuration dubbo consumers. Consumer. Version = 1.0.0 dubbo. Consumer. Check = false dubbo.consumer.timeout=8000Copy the code

Pom. The XML file

<? The 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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the 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> < dubo.version >2.7.1</ dubo.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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- Spring boot end --> <! Dubbo --> <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 dependencies end--> <! Alibaba </groupId> <artifactId>fastjson</artifactId> < version > 1.2.59 < / version > < / dependency > < the dependency > < groupId > org. Projectlombok < / groupId > <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <! End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

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); }}Copy the code

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); }}Copy the code

The last call results: http://localhost:11121/qbs/say? context=asde3

OK, this is a complete set of code examples for commercial use for Spring Boot integration with Dubbo, which is completely OK from top to bottom;

Interested can pay attention to my public number oh!

Original article, reprint please state, thanks !!!!!!