本文以官网Demo入门配置,简单介绍下Dubbo的基本配置使用,本文使用XML配置的方式。
从上图可以看出,Dubbo包含服务端,客户端,注册中心,监控中心等组件。那么在代码中主要为服务端和客户端,通过配置文件配置连接相应的注册中心。
Dubbo调用的过程是,客户端通过接口就可以直接调用到服务端的代码,那么在一般情况下Dubbo的服务端和客户端应该是共用同一套调用接口的,整个入门
演示的工程结构如下:
其中:common是定义的公共接口;consumer是客户端;provider是服务端。
(1)common:只定义客户端需要调用的dubbo服务接口,声明接口方法:
package org.apache.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}
(2)服务端:提供基于common接口定义的服务,供客户端调用:
pom.xml:引入dubbo2.7,zk,common包依赖。注意 <!--使用高版本,2.6之前使用zkClick,2.6之后使用curator,高版本需引用curator否则报类找不到 -->
<?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>springboot-dubbo-provider</groupId>
<artifactId>springboot_dubbo_provider</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<modules>
<module>../springboot_dubbo_common</module>
</modules>
<properties>
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
<dubbo.version>2.7.5</dubbo.version>
</properties>
<dependencies>
<!-- springboot -->
<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>
<!-- api -->
<dependency>
<groupId>springboot-dubbo-common</groupId>
<artifactId>springboot_dubbo_