dubbo只支持Java
协议支持
Dubbo支持多种协议,如下所示:
Dubbo协议
Hessian协议
HTTP协议
RMI协议
WebService协议
Thrift协议
Memcached协议
Redis协议
一.为什么要用dubbo
1.分布式,像调用本地方法一样调用远程方法。
2.集群容错
3.服务自动发现和注册,可平滑添加或者删除服务提供者。(zookeeper)
zookeeper:它是一个为分布式应用提供一致性服务的软件。
准备工作
安装zookeeper
1.先去官网下载zookeeper,http://www.apache.org/dyn/closer.cgi/zookeeper
2.将下载的zookeeper-3.3.6.tar.gz解压;tar zxvf zookeeper-3.3.6.tar.gz
3.修改zookeeper 配置,在zookeeper目录下创建一个文件夹data用来存放zookeeper相关数据,
[root@VM_0_17_centos zookeeper-3.3.6]# mkdir data
再创建一个文件夹log存放zookeeper日志
[root@VM_0_17_centos zookeeper-3.3.6]# mkdir log
再zookeeper-3.3.6目录下的conf的目录下创建一个zoo.cfg文件,用来配置zookeeper,可以参考zoo_sample.cfg
[root@VM_0_17_centos zookeeper-3.3.6]# vim zoo.cfg
最后启动zookeeper:
[root@VM_0_17_centos bin]# ./zkServer.sh start
安装完毕之后,开始写代码
先写一个服务提供者:代码结构如图,
UserService.java
public interface UserService {
String getUserByPhone(String phone);
}
UserServiceImpl .java
public class UserServiceImpl implements UserService {
public String getUserByPhone(String phone) {
if("11111".equals(phone)) {
return "jiangjz";
}
return "查无此人";
}
}
App.java
public class App
{
public static void main( String[] args ) throws IOException
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml" );
context.start();
System.out.println("服务发布中。。。");
System.in.read();
}
}
<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="provider" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.jiangjz.service.UserService" ref="userService" />
<bean id="userService" class="com.jiangjz.service.impl.UserServiceImpl" />
beans>
服务消费者
UserService.java //这里是重复代码 ,后面有改进版
public interface UserService {
String getUserByPhone(String phone);
}
App.java
public class App
{ //执行此方法可以调用提供者提供的服务
public static void main( String[] args )
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml" );
context.start();
UserService userService = (UserService) context.getBean("userService");
String name = userService.getUserByPhone("2345");
System.out.println(name);
}
}
<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="consumer" />
<dubbo:registry address="zookeeper://192.144.129.114:2181" />
<dubbo:reference interface="com.jiangjz.service.UserService" id="userService" />
beans>
针对上面的重复代码部分进行改进
新建一个maven工程,将上面的重复代码UserService.java 提取出来,代码结构如下图:
UserService.java
public interface UserService {
String getUserByPhone(String phone);
}
然后在删掉消费者和提供者工程里面的UserService.java,再将simple-dubbo-service依赖进来,分别在提供者和消费工程的pom.xml文件中加入如下依赖,就可以了。
<dependency>
<groupId>com.jiangjzgroupId>
<artifactId>simple-dubbo-serviceartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>