Dubbo初体验
- Apache Dubbo 是一款高性能、轻量级的开源服务框架。
- 提供了六大核心能力:面向接口代理的高性能RPC调用,智能容错和负载均衡,服务自动注册和发现,高度可扩展能力,运行期流量调度,可视化的服务治理与运维。
一、Dubbo如何实现远程通信?
服务消费者去注册中心订阅到服务提供者的信息。然后通过dubbo进行远程调用。
二、demo
1. 创建maven项目,命名为my-dubbo-demo
2. 创建my-dubbo-demo的module,类型为maven
- 命名my-service-interfaces
- 创建接口DeviceService
3. 创建my-dubbo-demo的module,类型为maven
- 命名device-service-provider
- 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>my-dubbo-demo</artifactId>
<groupId>com.hao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>device-service-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<!--dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<!--zk-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
<dependency>
<groupId>com.hao</groupId>
<artifactId>my-service-interfaces</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
-
编写具体的提供服务的实现类
-
编写bean配置文件,将dubbo和spring ioc整合,把服务提供到dubbo中.provider.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="device-service"/>
<!--注册中心的信息:服务要注册到这个注册中心上-->
<dubbo:registry address="zookeeper://192.168.100.105:2181"/>
<!--配置当前这个服务在dubbo容器中的端口号,每个dubbo容器内部的服务的端口号必须是不一样的-->
<dubbo:protocol name="dubbo" port="20881"/>
<!--暴露出服务,指明该服务具体的实现bean是deviceService-->
<dubbo:service interface="com.hao.service.api.device.DeviceService" ref="deviceService"/>
<!--指明服务的实现类,将服务提供者的bean注入到ioc容器中-->
<bean id="deviceService" class="com.hao.device.service.impl.DeviceServiceImpl"/>
</beans>
<!--注册服务-->
- 启动ioc容器,关联bean配置文件,provider.java
package com.hao.device.service.impl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provider {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
context.start();
System.out.println("服务已注册!");
try {
// 按任意键退出
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. 创建my-dubbo-demo的module,类型为maven
- 命名device-service-consumer
- 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>my-dubbo-demo</artifactId>
<groupId>com.hao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>device-service-consumer</artifactId>
<dependencies>
<dependency>
<artifactId>my-service-interfaces</artifactId>
<groupId>com.hao</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<!--zk-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
</dependencies>
</project>
- 编写bean配置文件,将dubbo和spring ioc整合,把服务提供到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="device-consumer"/>
<!--注册中心的信息:服务要注册到这个注册中心上-->
<dubbo:registry address="zookeeper://192.168.100.105:2181"/>
<!--要订阅的服务,生成一个远程的服务代理对象-->
<dubbo:reference interface="com.hao.service.api.device.DeviceService" id="deviceService"/>
</beans>
<!--注册服务-->
- 启动ioc容器,关联bean配置文件 Consumer.java
package com.hao.device.consumer;
import com.hao.service.api.device.DeviceService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
context.start();
DeviceService deviceService = (DeviceService) context.getBean("deviceService");
String result = deviceService.getDeviceNameByID(1001);
System.out.println(result);
}
}
三、服务代理过程
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/65656.html