简单了解一下dubbo:
1:简单说dubbo是一个由阿里巴巴开发的高性能、透明化的RPC框。
2:RPC又是什么:也就是远程调用。比如服务器A要调用服务器B上的方法,不在一个机器上要怎么调用,这时我们想到的是网络。rpc就干这事,就跟我们调用本地方法一样,不像调用web接口那么麻烦。
通过百度,又了解到dubbo的特点
透明化的远程方法调用: 像调用本地方法一样调用远程方法;只需简单配置,没有任何API侵入
软负载均衡及容错机制: 可在内网替代f5等硬件负载均衡器
服务注册中心自动注册 & 配置管理: 不需要写死服务提供者地址,注册中心基于接口名自动
查询提供者ip.使用类似zookeeper等分布式协调服务作为服务注册中心,可以将绝大部分项
目配置移入zookeepeer集群
服务接口监控与治理: Dubbo-admin与Dubbo-monitor提供了完善的服务接口管理与监控功能
针对不同应用的不同接口,可以进行 多版本,多协议,多注册中心管理。
duubo架构
理论知识很多。本文主要讲搭建一个demo工程。
首先创建一个maven工程用于当作父级工程。把dubbo和spring依赖进来,还有注册中心zookeeper(当然也可以选择其他的)
<properties>
<spring.version>4.3.11.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo注册中心-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
<!--zookeeper客户端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
既然使用了zookeeper,那么在此之前得去把zookeeper下载下来。修改了zookeeper的一些配置之后点击zkServer.cmd运行zk
切记pom文件添加
<packaging>pom</packaging>
下面创建子工程,肯定分为三个子模块分别为服务、内容提供者、消费者
ok。点击父工程 右键-->new-->module 分别创建三个子模块 我分别命名为 myService myProvider、myConsumer
消费者和内容提供者的pom文件要依赖于myService。因为两者都引用了service的代码(service提供了接口,myProvider进行了实现,myConsumer则调用了那个接口)
所以两者都添加以下依赖
<dependencies>
<dependency>
<groupId>dwj</groupId>
<artifactId>myService</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
groupId则是你的项目的groupId别跟我的写一样了 artifactId为service模块的名称
service模块的pom则添加以下配置
<packaging>jar</packaging>
然后开始写第一个程序:
写一个hello world:
1:现在service创建一个服务:
public interface First {
String firstMethod(String str);
}
2:创建在myProvider项目(提供者)创建相应的实现:
public class FirstImpl implements First{
public String firstMethod(String str) {
return "hello: "+str;
}
}
创建配置文件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="hello-provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<bean id="FirstImpl" class="com.serviceImpl.StringOperationImpl"/>
<dubbo:service interface="First" ref="FirstImpl"/>
</beans>
interface为service的类全路径,ref为接口的实现
然后编写main程序去运行服务端,
public class ProviderServer {
public static void main(String[] args){
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("provider.xml");
applicationContext.start();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
没什么问题的话现在服务端已经正常提供服务了,现在要做的,只是让消费者去调用这些服务。
首先在myConsumer工程编写配置文件cunsumer.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="hello-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference interface="First" id="helloWorld"/>
</beans>
配置zk 和接口的bean interface就是那个service的类全路径,id则是bean name
在myConsumer工程编写一个main程序去调用服务
public class ConsumerClient {
public static void main(String[] args) {
ClassPathXmlApplicationContext xaa = new ClassPathXmlApplicationContext("consumer.xml");
First sss = (First) xaa.getBean("helloWorld");
String s= sss.firstMethod("tracy !!!!!!!");
System.out.println(s);
}
}
运行,如果不报错的话,会发现控制台打印了 hello tracy !!!!!
那么hello world程序就这么写完了。还蛮简单的,这种简单程序