六、Dubbo支持的协议
1、Dubbo协议:
1.1 本质:使用NIO和线程池进行处理
1.2 缺点:大文件传输时可能出现文件传输失败的问题
2、RMI协议:
2.1 本质:JDK提供的协议,远程方法调用协议
2.2 缺点:偶尔链接失败
2.3 优点:JDK原生,不需要进行额外配置(导入jar)
3、Hession协议:
3.1 优点:基于HTTP协议,支持http请求
3.2 缺点:需要额外导入jar,并在短连接时性能低
七、Dubbo中Provider搭建
1、新建Maven Project,里面只有接口(
1.1 为什么这么做
RPC框架,不希望Consumer知道具体的实现,如果实现类和接口在同一个项目中,Consumer依赖这个项目时,就会知道这个类的具体实现
2、新建Maven Project ,写接口的实现类(dubbo-service-impl)
3、在dubbo-service-impl中配置pom.xml
3.1 依赖接口
3.2 依赖dubbo,去掉老版本spring
3.3 依赖新版本spring
3.4 依赖Zookeeper客户端zkClient
| <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <!-- 去掉dubbo自带的老版本 --> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency>
<!-- 访问Zookeeper的客户端jar包 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> |
4、新建实现类,并实现接口方法
5、新建配置文件applicationContext-dubbo.xml,并配置
5.1 <dubbo:application /> 给provider起别名,在monitor或管理工具中区别是哪个provider
5.2 <dubbo:registry /> 配置注册中心
4.2.1 address : 注册中心的IP和端口
4.2.2 protocol : 只用哪种注册中心
5.3 <dubbo:protocol /> 配置协议
5.3.1 name :使用什么协议
5.3.2 port:comsumer invoke provider时的端口号
5.4 <dubbo:service /> 注册接口
5.4.1 ref:引用接口实现类<bean>的id值
| <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 给当前Provider自定义名词 --> <dubbo:application name="dubbo-service"/> <!-- 配置注册中心 --> <dubbo:registry address="192.168.0.103:2181" protocol="zookeeper"></dubbo:registry> <!-- 配置端口 --> <dubbo:protocol name="dubbo" port="21880"></dubbo:protocol> <!-- 注册功能 --> <dubbo:service interface="com.project.DemoService" ref="demoServiceImpl"></dubbo:service> <bean id="demoServiceImpl" class="com.project.impl.DemoServiceImpl"></bean> </beans> |
6、启动容器
6.1 通过spring方式启动
6.1.1 applicationContext -dubbo.xml位置没有要求
| ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml"); ac.start(); System.out.println("启动成功"); System.in.read(); |
6.2 使用dubbo提供的方式启动(推荐使用这种方式)
6.2.1 要求applicationContext-dubbo.xml必须放入类路径下/META-INF/spring/*.xml
7、具体实现看下面
原码:
| package com.project; public interface DemoService { String demo(String name); }
package com.project.impl; import com.project.DemoService; public class DemoServiceImpl implements DemoService{ @Override public String demo(String name) { // TODO Auto-generated method stub return "传递过来的Name:"+ name; } }
package com.project.test; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.dubbo.container.Main; public class Test { public static void main(String[] args) throws IOException { // ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml"); // ac.start(); // System.out.println("启动成功"); // System.in.read();
//官方推荐 //要求配置文件必须放在/METE-INFO/spring/*.xml Main.main(args); } }
applicationContext-dubbo.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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 给当前Provider自定义名词 --> <dubbo:application name="dubbo-service"/> <!-- 配置注册中心 --> <dubbo:registry address="192.168.0.103:2181" protocol="zookeeper"></dubbo:registry> <!-- 配置端口 --> <dubbo:protocol name="dubbo" port="21880"></dubbo:protocol> <!-- 注册功能 --> <dubbo:service interface="com.project.DemoService" ref="demoServiceImpl"></dubbo:service> <bean id="demoServiceImpl" class="com.project.impl.DemoServiceImpl"></bean> </beans>
Pom.xml <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>com.project</groupId> <artifactId>dubbo-service-impl</artifactId> <version>0.0.1-SNAPSHOT</version>
<dependencies> <dependency> <groupId>com.project</groupId> <artifactId>dubbo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency>
<!-- 访问Zookeeper的客户端jar包 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
</dependencies> </project> |