1.dubbo简介
起源阿里系内部使用分布式服务应用管理。经过大量生产环境考量http://dubbo.io
dubbo原生(2017.5) --> 2018.2.15入驻apache孵化器
dubbox 支持RESTful通讯
2.dubbo的provider编写
# pom.xml添加dubbo引用
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
# 编写dubbo接口及实现子类
package com.lpf.dubbo;
public interface HelloService {
public String hello(String name);
}
package com.lpf.dubbo;
public class HelloServiceImpl implements HelloService {
public String hello(String name) {
return "hello " + name;
}
}
# 编写spring配置文件格式的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-service" />
<!-- 服务网络注册 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />-->
<dubbo:registry address="zookeeper://192.168.0.102:2181" />
<!-- provider和consumer之间通讯使用端口号 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 服务接口声明 -->
<dubbo:service interface="com.lpf.dubbo.HelloService" ref="helloServiceImpl" />
<!-- 服务接口实现的bean -->
<bean id="helloServiceImpl" class="com.lpf.dubbo.HelloServiceImpl" />
</beans>
# 编写main启动类,启动provider服务并生成jar包
package com.lpf.dubbo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provier {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("provider.xml");
ctx.start();
System.in.read(); // 任意键退出
}
}
3.dubbo的consumer编写
# pom.xml添加provider.jar引用
<dependency>
<groupId>com.lpf.dubbo</groupId>
<artifactId>hello_provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
# 编写spring配置文件格式的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">
<!-- 声明consumer的引用名称 -->
<dubbo:application name="hello-service-consumer" />
<!-- 服务网络注册 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />-->
<dubbo:registry address="zookeeper://192.168.0.102:2181" />
<!-- 声明服务及引用 -->
<dubbo:reference id="helloService" interface="com.lpf.dubbo.HelloService"/>
</beans>
# 编写main启动类,调用provider服务
package com.lpf.dubbo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("consumer.xml");
ctx.start();
HelloService service = (HelloService) ctx.getBean("helloService");
System.out.println(service.hello("mike"));
}
}
4.zookeeper安装及配置
1)简介
原属于hadoop技术框架中一个子项,功能完成状态管理
原理:内部DOM结构,存储多个节点,不同节点保存不同值。
由于zookeeper独立运行在系统中,可以实现全局共享资源管理。
2)安装配置。
http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
# linux系统解压zookeeper文件,并解压后的文件夹改名
tar zxvf zookeeper-3.4.11.tar.gz
mv zookeeper-3.4.11 zookeeper
#进入zookeeper编辑配置文件
cd zookeeper
cd conf
cp zoo_simple.cfg zoo.cfg
vi zoo.cfg
# 心跳时间检测间隔值
tickTime=2000
# 数据存盘目录位置
dataDir=/var/lib/zookeeper
# 客户端通讯端口
clientPort=2181
编辑后存盘退出
cd ..
启动zookeeper服务
bin/zkServer.sh start
查询系统进程中zookeeper
ps -aux | grep zookeeper
客户端访问(需要java环境)
bin/zkCli.sh -server 127.0.0.1:2181
查询节点
ls /
创建节点
create /test hello
查看根节点
ls /
获取节点
get /test
退出客户端
quit
5.dubbo服务注册使用zookeeper实现
#pom.xml导入
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
#第三步consumer中xml配置文件
<dubbo:registry address="zookeeper://192.168.10.102:2181" />