springboot dubbo 整合简单的示例(用dubbo的xml配置方法)
1 工具
eclipse mar jkd1.8 maven3.5.3 springboot2.1.0 dubbo2.0.0(dubbo用阿里官方版本)
2 创建项目
(1) 父项目--主要是添加公用的依赖pom.xml
Maven项目,只有pom文件
<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>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath /> </parent>
<groupId>com.springboot</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
<modules> <module>../example</module> <module>../dubboconsumer</module> <module>../dubbotest</module> </modules>
<dependencies> <!--dubbo-springBoot依赖 --> <dependency><!--dubbo的官方版本 用这个 --> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- <dependency>dubbo其他版本 <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </dependency> --> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <type>pom</type> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
</dependencies>
</project>
|
(2) 创建公用的接口项目,供服务提供者和服务消费者调用
Pom文件不用配置,只有一个接口,服务提供者和服务消费者依赖该项目,实现该接口
package com.springboot.dubboAPI; public interface IBookService { String readBooks(); } |
(3) 服务提供者
<?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>com.springboot</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
<groupId>com.srpingboot</groupId> <artifactId>example</artifactId> <packaging>war</packaging>
<properties> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion><!--排除springboot自动加载的tomcat --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope><!--设置在打包时不包含tomcat的包 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
不用application.properties文件
添加dubbo-provider.xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用配置,不要与提供方相同 --> <dubbo:application name="springboot-dubbo-provider"/>
<!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.181.128:2181" timeout="60000" />
<!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。--> <dubbo:consumer check="false" />
<!-- 使用注解方式暴露接口,会自动扫描package下所有包中dubbo相关的注解,这样就不用在xml中再针对每个服务接口配置dubbo:service interface--> <dubbo:annotation package="com.srpingboot.example"/> <!-- <dubbo:service interface="com.springboot.dubboAPI.IBookService" ref="bookServiceImpl" /> --> </beans> |
在该配置文件中配置zookeeper的服务地址(zookeeper用于完成服务对外的统一管理),以及我们需要暴露的dubbo服务接口。暴露接口的方式有两种:一是通过注解方式,二是通过xml配置的方式。这里我采用的是注解的方式,然后在类上打@service注解,这样就不用在xml中再针对每个服务接口配置dubbo:service interface。如果需要采用xml配置的方式,只需把最后一行的代码替换成以下代码即可。
<dubbo:service interface="com.springboot.dubboAPI.IBookService" ref="bookServiceImpl" />
这里的配置标签是dubbo:service 和消费者的标签不一样
实现类
package com.srpingboot.example;
import com.alibaba.dubbo.config.annotation.Service; import com.springboot.dubboAPI.IBookService;
@org.springframework.stereotype.Service @Service public class BookServiceImpl implements IBookService {
@Override public String readBooks() { return "I like read book"; }
}
|
注意该类有两个service注解,一个是alibaba的,一个是spring的,大家要注意区分。
启动类
package com.srpingboot.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
//@EnableDubboConfiguration//用xml配置文件了,就不用这个注解了 @SpringBootApplication @ImportResource(value = {"classpath:dubbo-provider.xml"})//引入xml配置文件 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
|
(4) 服务消费者
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent> <groupId>com.springboot</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
<artifactId>dubboconsumer</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion><!--排除springboot自动加载的tomcat --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope><!--设置在打包时不包含tomcat的包 --> </dependency> </dependencies> <build> <finalName>dubboconsumer</finalName> </build> </project> |
application.properties文件
## 避免和提供者项目端口冲突 server.port=8082 |
dubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用配置,不要与提供方相同 --> <dubbo:application name="springboot-dubbo-consumer"/>
<!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.181.128:2181" timeout="60000" />
<!-- 使用注解方式创建远程服务代理--> <dubbo:annotation package="com.srpingboot.example"/> <!--声明服务引用,与服务声明接口类型一致--> <!-- <dubbo:reference id="iBookService" interface="com.springboot.dubboAPI.IBookService" /> 这里的配置标签是dubbo:reference和提供者的标签不一样 标签里用的是id 提供者用的是ref--> </beans> |
controller
package com.srpingboot.example;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference; import com.srpingboot.example.IBookService;
@RestController public class DubboController {
@Reference private IBookService bookService; //通过xml配置文件暴露接口时,接口的调用使用@Autowired注解,不用@Reference,相当于通过spring将这个接口对象注入了 // @Autowired
@GetMapping("test") public String testDubbo() { return bookService.readBooks(); } } |
在调用consumer模块定义的service的时候,必须要加入@Reference注解。
启动类
package com.srpingboot.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
//@EnableDubboConfiguration @SpringBootApplication @ImportResource("classpath:dubbo-consumer.xml") public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
|
启动测试
先启动zookeeper,再启动provider,然后启动consumer
访问consumer : http://localhost:8082/test
注意:
关于接口的暴露有两种方式:
1.在提供者和消费者的xml配置文件里配置
<dubbo:service interface="com.practice.springboot.dubbo.provider.SayHelloImpl" ref="SayHelloImpl"/>
2.通过注解
先在xml配置文件里配置下边这个
<!-- 使用注解方式暴露接口,会自动扫描package下所有包中dubbo相关的注解,这样就不用在xml中再针对每个服务接口配置dubbo:service interface-->
<dubbo:annotation package="com.srpingboot.example"/>
然后在接口实现类上添加注解@Service(这个是dubbo的注解,和spring的名称一样),两个@Service都要有
import com.alibaba.dubbo.config.annotation.Service;
import com.springboot.dubboAPI.IBookService;
@org.springframework.stereotype.Service//这个是spring的
@Service//这个是dubbo的
public class BookServiceImpl implements IBookService {
@Override
public String readBooks() {
return "I like read book";
}
}