一、服务端依赖
<?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>org.example</groupId><artifactId>dubboxml1</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><artifactId>spring-boot-maven-plugin</artifactId><groupId>org.springframework.boot</groupId></plugin></plugins></build><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><dubbo.version>3.1.8</dubbo.version><spring-boot.version>2.7.10</spring-boot.version></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.10</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.7.10</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><type>pom</type></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-zookeeper</artifactId></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-apache-dubbo-adapter</artifactId><version>1.8.7</version></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.7</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-metrics-api</artifactId></dependency><dependency><groupId>org.apache.skywalking</groupId><artifactId>apm-toolkit-micrometer-1.10</artifactId><version>8.14.0</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.1</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2021.0.1.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
二、服务端接口定义和实现
package cn.edu.tju.service;public interface GreetingsService {String sayHi(String name);
}
package cn.edu.tju.service;public class GreetingsServiceImpl implements GreetingsService {@Overridepublic String sayHi(String name) {return "hi, " + name;}
}
三、服务端配置文件(resources目录下dubbo-demo-provider.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder/><!-- 定义应用名 --><dubbo:application name="demo-provider"/><!-- 定义注册中心地址 --><dubbo:registry address="zookeeper://xx.xx.xx.xx:2181"/><!-- 定义实现类对应的 bean --><bean id="greetingsService" class="cn.edu.tju.service.GreetingsServiceImpl"/><!-- 定义服务信息,引用上面的 bean --><dubbo:service interface="cn.edu.tju.service.GreetingsService" ref="greetingsService"/></beans>
其中配置了服务本身和注册中心地址
四、服务端主类:
package cn.edu.tju;import java.util.concurrent.CountDownLatch;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Application {public static void main(String[] args) throws InterruptedException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-demo-provider.xml");context.start();// 挂起主线程,防止退出new CountDownLatch(1).await();}
}
其中使用了CountDownLatch,使服务端一直运行。
五、客户端依赖,类似于服务器端
六、客户端的服务接口定义,同服务端
package cn.edu.tju.service;public interface GreetingsService {String sayHi(String name);
}
七、客户端配置文件:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder/><!-- 定义应用名 --><dubbo:application name="demo-provider"/><!-- 定义注册中心地址 --><dubbo:registry address="zookeeper://xx.xx.xx.xx:2181"/><!-- 定义订阅信息,Dubbo 会在 Spring Context 中创建对应的 bean --><dubbo:reference id="greetingsService" interface="cn.edu.tju.service.GreetingsService"/></beans>
其中配置了注册中心的地址和使用的服务。
八、客户端主类:
package cn.edu.tju;import java.io.IOException;import cn.edu.tju.service.GreetingsService;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Application {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-demo-consumer.xml");context.start();GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");String message = greetingsService.sayHi("dubbo");System.out.println("Receive result ======> " + message);System.in.read();System.exit(0);}}
九、分别运行服务端和客户端,在客户端看到运行结果: