Zookeeper服务节点的监控及维护(节点注册,删除,故障维护等),Dubbo远程RPC调用服务。
Dubbo可以单独作为服务被消费者调用,分布式都是多服务的,所以会用到类似Zookeeper服务节点监控管理框架。
项目功能简介:
Zookeeper开启2个zkServer分别监听2081,2082端口,即分别为Provider和Provider1提供服务注册中心。
Provider,Provider1分别开启20880,20881端口启动Dubbo服务。
Consumer调用Provider和Provider1提供的服务。
Provider,Provider1服务提供者,Consumer服务消费者,API服务接口
核心代码:
dubbo-api
public interface DemoService {
String sayHello(String name);
}
public interface DemoService1 {
String sayHello(String name);
}
dubbo-provider
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
public class Provider {
public static void main(String[] args) throws Exception {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
context.start();
System.in.read(); // press any key to exit
}
}
<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="demo-provider"/>
<dubbo:registry address="zookeeper://192.168.1.106:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
beans>
dubbo-provider1
public class DemoServiceImpl1 implements DemoService1 {
@Override
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider111111111: " + RpcContext.getContext().getLocalAddress();
}
}
public class Provider {
public static void main(String[] args) throws Exception {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider1.xml"});
context.start();
System.in.read(); // press any key to exit
}
}
<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="demo-provider1"/>
<dubbo:registry address="zookeeper://192.168.1.106:2182"/>
<dubbo:protocol name="dubbo" port="20881"/>
<bean id="demoService1" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl1"/>
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService1" ref="demoService1"/>
beans>
dubbo-consumer
public class Consumer {
public static void main(String[] args) {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
DemoService1 demoService1 = (DemoService1) context.getBean("demoService1");
while (true) {
try {
Thread.sleep(1000);
String hello1 = demoService1.sayHello("world"); // call remote method
String hello = demoService.sayHello("world");
System.out.println(hello); // get result
System.out.println(hello1); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
<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="demo-consumer"/>
<dubbo:registry id="demo" address="zookeeper://192.168.1.106:2181"/>
<dubbo:registry id="demo1" address="zookeeper://192.168.1.106:2182"/>
<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService" registry="demo"/>
<dubbo:reference id="demoService1" check="false" interface="com.alibaba.dubbo.demo.DemoService1" registry="demo1"/>
beans>
pom
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>dubbogroupId>
<artifactId>dubbo-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>dubbo-provider1groupId>
<artifactId>dubbo-provider1artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>Mavenname>
<url>http://maven.apache.org/url>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<springframework.version>4.1.5.RELEASEspringframework.version>
<slf4j.version>1.7.5slf4j.version>
<slf4j-log4j12.version>1.7.5slf4j-log4j12.version>
<log4j.version>1.2.17log4j.version>
properties>
<dependencies>
<dependency>
<groupId>dubbo-apigroupId>
<artifactId>dubbo-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j-log4j12.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.3version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.5version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>2.5.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.1version>
<scope>compilescope>
<exclusions>
<exclusion>
<artifactId>springartifactId>
<groupId>org.springframeworkgroupId>
exclusion>
exclusions>
dependency>
dependencies>
project>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>dubbogroupId>
<artifactId>dubbo-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>dubbogroupId>
<artifactId>dubbo-consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>Mavenname>
<url>http://maven.apache.org/url>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<springframework.version>4.1.5.RELEASEspringframework.version>
properties>
<dependencies>
<dependency>
<groupId>dubbo-apigroupId>
<artifactId>dubbo-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.3version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.5version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>2.5.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.1version>
<scope>compilescope>
<exclusions>
<exclusion>
<artifactId>springartifactId>
<groupId>org.springframeworkgroupId>
exclusion>
exclusions>
dependency>
dependencies>
project>
动态调用dubbo服务代码:
public class Consumer2 {
public static void main(String[] args) {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer2.xml"});
String url = "dubbo://192.168.1.106:20880/com.alibaba.dubbo.demo.DemoService";//更改不同的Dubbo服务暴露的ip地址&端口
ReferenceBean referenceBean = new ReferenceBean();
referenceBean.setApplicationContext(context);
referenceBean.setInterface(com.alibaba.dubbo.demo.DemoService.class);
referenceBean.setUrl(url);
while (true) {
try {
Thread.sleep(1000);
referenceBean.afterPropertiesSet();
DemoService demoService = referenceBean.get();
System.out.println(demoService.sayHello("Tester"));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
xml version="1.0" encoding="UTF-8"?>
"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">
"demo-consumer2"/>
项目源码:https://github.com/lichengshengko/demo-dubbo
Windows下Zookeeper伪集群设置参考:https://blog.csdn.net/ceasadan/article/details/52343734(需安装JDK并配置JAVA路径)
Dubbo官方项目下载地址:https://github.com/apache/incubator-dubbo
说明:
RpcContext为Dubbo官方封装好的库文件,主要服务提供者配置信息的获取。
分布式架构设计的难点是服务节点的动态操作及事务的处理。
单点故障处理,上述Provider1节点故障了,服务中心如何快速注册替代服务。
分布式事务,Consumer调用多个服务,但实际业务中有顺序的,如何协调。若其中一个服务超时如何回滚等。
本文章的项目为一个静态节点的小例子,主要用于学习与参考。