一、什么是Spring Boot ?
现阶段的 Spring Boot 可谓是太火了,为什么呢?因为使用方便、配置简洁、上手快速,那么它是什么?从官网上我们可以看到,它是 Spring 开源组织下的一个子项目,主要简化了 Spring 繁重的配置,而且 Spring Boot 内嵌了各种 Servlet 容器,如:Tomcat、Jetty 等
官方网站:http://projects.spring.io/spring-boot/GitHub源码:https://github.com/spring-projects/spring-boot
二、Spring Boot 的优势 ?
1、独立运行:不需要在用 tomcat 等容器运行。2、简化配置:不需要在像 Spring mvc 那样配置很多的xml了;3、自动配置:根据包路径自动配置 bean4、应用监控:Spring Boot 提供监控服务
三、项目创建
1、创建提供者
![afdfc84684874a597bd0fd8d3b6439a6.png](https://i-blog.csdnimg.cn/blog_migrate/b3e7847d608a44ced708eb37b1c801f3.png)
![5a1dc4ff961603342e3f0ffb65d5b8ca.png](https://i-blog.csdnimg.cn/blog_migrate/8459fe4d1da77b2ebc882028633b8b0b.png)
![490a107aa0103d1cee08c15a065d4323.png](https://i-blog.csdnimg.cn/blog_migrate/f68e0e3d60f67211f3a38f77e9b42292.jpeg)
![73bdf21c195518711700b3ad64721e6b.png](https://i-blog.csdnimg.cn/blog_migrate/e97d8dd5aba2cb3e78cfea73bd4a1756.png)
后面点击 finish,创建完毕,然后删掉多余包,使得项目结构如下图:
![f1919e2ea5e324240c9000f4b05a6443.png](https://i-blog.csdnimg.cn/blog_migrate/f2648a0379be51c2d7f9d29320be2d92.png)
右击项目,新建一个提供者对外提供服务的模块 qbs-facade
![585e8074aa26cad487224bb67ada5ec5.png](https://i-blog.csdnimg.cn/blog_migrate/a58856e0357e62f3bb9784d4af63c1d8.png)
![62f9dc78c66fa7e6a39d814221178313.png](https://i-blog.csdnimg.cn/blog_migrate/e268f5b41565efad092acf1a1b5b289a.jpeg)
![9e205d00f1cb20dd121334a3c380dc97.png](https://i-blog.csdnimg.cn/blog_migrate/d6175236d5445fd1c79842913441c965.png)
然后再按照该模式创建一个 qbs-web 模块(这里就不介绍了)最终的项目结构如下图所示:
![8adbdce9c8b640706f4fc9bfa9e962f8.png](https://i-blog.csdnimg.cn/blog_migrate/846dfbb83e4a82cd89990045e8b0afcd.png)
![cc1db3888c0334588c4eaaa3cdf9a869.png](https://i-blog.csdnimg.cn/blog_migrate/ff9c4536269f664ee021ccd4b148aa93.png)
修改 主 pom 文件
<?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、编写 facade
![79d58be235c8dbfb1f985f5af0878d14.png](https://i-blog.csdnimg.cn/blog_migrate/746668ca22073a05dfb72c4e9ab0cbc8.png)
SayFacade.java
package com.btd.qbs.facade;
public interface SayFacade {
String say(String context);
}
facade 模块的 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 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、组织api模块,实际接口的实现
![c035201294762eaebcbbdb674a9066b3.png](https://i-blog.csdnimg.cn/blog_migrate/9195fb4fd194960690179f670cd0a6eb.png)
先看 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 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 相关 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 相关 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 相关依赖 end-->
<dependency>
<groupId>com.btd</groupId>
<artifactId>qbs-facade</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
application.properties 文件
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注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 提供者配置
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 "小肥羊对你说:"+context;
}
}
启动文件 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 ,到这里来说,我们的提供者就完成了,启动项目,然后我们看看 dubbo-admin
![3b2bed84f70f17c5f3cfdfdf147275ef.png](https://i-blog.csdnimg.cn/blog_migrate/bd3675974df8e309535772f6a1cf07e9.jpeg)
这里要注意,因为我们的 facade 是对外需要映入的,所以我们打个 jar 包
![890612b9db3193e8461627a5519880c8.png](https://i-blog.csdnimg.cn/blog_migrate/7623f9f86ce9bc28538b5a215af2f675.png)
打完包后,我们会得到一个 jar 文件
![a061432860dafc01a7bd1e5990046a59.png](https://i-blog.csdnimg.cn/blog_migrate/830bf55c1b508924cc1771a2d3cb424a.png)
2、建立消费者
![63b197d701ae0f9c40e268482734b85e.png](https://i-blog.csdnimg.cn/blog_migrate/f8e211b037aa0172e0fa7ac69ee2d5d4.png)
引入我们打的 jar 包
![627676b9461ed9995e7d558c419c1f89.png](https://i-blog.csdnimg.cn/blog_migrate/d955e0c6cb9b002eea4611c522905480.png)
配置文件:
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注册中心地址
dubbo.registry.address=zookeeper://172.25.37.130:2181
# 消费者配置
dubbo.consumer.version=1.0.0
dubbo.consumer.check=false
dubbo.consumer.timeout=8000
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.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 相关 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 相关 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 相关依赖 end-->
<!-- 基础依赖 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>
<!-- 基础依赖 end -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application.java 文件
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 文件
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);
}
}
最后调用结果:http://localhost:11121/qbs/say?context=asde3
![482a5315fdc41b66cbc40bcba9cbc93a.png](https://i-blog.csdnimg.cn/blog_migrate/da7522604ff3868296f78b6df31ed664.png)
![c1480b930d4aa111b25e38cd8ee86509.png](https://i-blog.csdnimg.cn/blog_migrate/a7df12d26fb042235d8dfecb585da727.png)
好了,这是spring boot 集成 dubbo的一整套商业使用的代码事例,按着从上到下的操作,是完全OK的;
有意向的可以关注下我公众号哦!!! 刚开的
![6c935145418b57a5886655d2dc3aa953.png](https://i-blog.csdnimg.cn/blog_migrate/f98e348b9b137aa02b8637ea78b13115.png)
本文纯属原创,转载请申明