Dubbo基本使用:直连方式
一、简介
Dubbo 官网
Dubbo 示例仓库地址
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务间调用,可以和Spring框架无缝集成。
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
二、Dubbo 直连示例
2.1 创建接口工程模块
接口工程只包含一系列的业务接口即可。示例中创建一个DemoService接口作为服务调用的接口。
2.2 创建服务提供者工程模块
引入dubbo依赖与接口工程的依赖:
<dependency>
<groupId>org.dubbo</groupId>
<artifactId>api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.7</version>
</dependency>
实现测试接口 DemoService :
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name + "!";
}
@Override
public void testVoid() {
System.out.println("provider testVoid method!");
}
}
创建Spring配置文件配置接口实现 :
<?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.xsd
http://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">
<!-- 指定dubbo应用的名字 -->
<dubbo:application name="base-demo-provide"/>
<!-- 不使用注册中心 -->
<dubbo:registry address="N/A" />
<!-- 指定协议 与 端口号 -->
<dubbo:protocol name="dubbo" port="20881" />
<!-- 将实现类交给Spring管理 -->
<bean id="demoService" class="org.dubbo.base.impl.DemoServiceImpl" />
<!-- 服务暴露 -->
<dubbo:service interface="org.dubbo.base.api.DemoService" ref="demoService" />
</beans>
dubbo依赖中默认集成了Spring 依赖,无需重复添加Spring依赖。
创建服务提供者启动类:
package org.dubbo.base;
import org.apache.dubbo.container.Main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
System.in.read();
}
}
2.3 创建服务消费者工程模块
依赖与服务提供者相同
消费者端spring配置文件:
<?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.xsd
http://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">
<!-- 指定服务名称 -->
<dubbo:application name="base-demo-consumer"/>
<!-- 不使用注册中心 -->
<dubbo:registry address="N/A" />
<!-- 指定协议与端口 -1 表示系统使用随机未在用的端口 -->
<dubbo:protocol name="dubbo" port="-1" />
<!-- 直连式连接服务提供者 -->
<dubbo:reference interface="org.dubbo.base.api.DemoService" id="cDemoService" url="dubbo://169.254.156.154:20881" />
</beans>
消费者启动测试类:
package org.dubbo.base;
import org.dubbo.base.api.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ConsumerMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/application.xml");
DemoService demoService = context.getBean(DemoService.class);
demoService.testVoid();
System.in.read();
}
}