一、什么是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、创建提供者
后面点击 finish,创建完毕,然后删掉多余包,使得项目结构如下图:
右击项目,新建一个提供者对外提供服务的模块 qbs-facade
然后再按照该模式创建一个 qbs-web 模块(这里就不介绍了)最终的项目结构如下图所示:
修改 主 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
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模块,实际接口的实现
先看 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-providerserver.port=11222dubbo.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 {@Overridepublic 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
这里要注意,因为我们的 facade 是对外需要映入的,所以我们打个 jar 包
打完包后,我们会得到一个 jar 文件
2、建立消费者
引入我们打的 jar 包
配置文件:
spring.application.name=qbs-consumerserver.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 {@Referenceprivate SayFacade sayFacade;@GetMapping("/say")@ResponseBodypublic String say(String context) {return sayFacade.say(context);}
}
最后调用结果:http://localhost:11121/qbs/say?context=asde3
好了,这是spring boot 集成 dubbo的一整套商业使用的代码事例,按着从上到下的操作,是完全OK的;
有意向的可以关注下我公众号哦!!! 刚开的
本文纯属原创,转载请申明