Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
安装环境为Centos系统 IP:192.168.40.129,与Windows操作基本一样
yum install java-1.8.0-openjdk.x86_64 -y
#下载zookeeper
cd /usr/local/
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
#解压
tar -zxvf zookeeper-3.4.14.tar.gz
#配置
cd ./zookeeper-3.4.14
mkdir data
vim conf/zoo_sample.cfg
#修改 dataDir=/usr/local/zookeeper-3.4.14/data
mv conf/zoo_sample.cfg conf/zoo.cfg
#启动
/usr/local/zookeeper-3.4.14/bin/zkServer.sh start
开发环境为IDEA ,注意修改成正确的Maven配置
dubbo-demo
主体结构,其中producer和consumer也可以分开写,但在本例为方便而中写到了一起。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>cn.ttext.demo.dubbo</groupId>
<artifactId>dubbo-demo</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>dubbo-demo-api</module>
<module>duboo-demo-producer</module>
<module>duboo-demo-consumer</module>
</modules>
<properties>
<spring.version>4.3.3.RELEASE</spring.version>
<dubbo.version>2.6.7</dubbo.version>
<netty.version>4.1.43.Final</netty.version>
<curator.version>4.2.0</curator.version>
<zookeeper.version>3.4.14</zookeeper.version>
</properties>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<artifactId>zookeeper</artifactId>
<groupId>org.apache.zookeeper</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>zookeeper</artifactId>
<groupId>org.apache.zookeeper</groupId>
<version>${zookeeper.version}</version>
</dependency>
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</project>
dubbo-demo-api
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>cn.ttext.demo.dubbo</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.0.0</version>
<artifactId>dubbo-demo-api</artifactId>
</project>
IDemoService
package cn.ttext.demo.dubbo.api;
public interface IDemoService {
String say(String val);
}
dubbo-demo-producer
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>cn.ttext.demo.dubbo</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.0.0</version>
<artifactId>dubbo-demo-producer</artifactId>
<dependencies>
<dependency>
<groupId>cn.ttext.demo.dubbo</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
spring-dubbo-producer.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-demo-producer"/>
<!-- 注册中心地址 -->
<dubbo:registry address="zookeeper://192.168.40.129:2181"/>
<!-- 服务端口 -->
<dubbo:protocol name="dubbo" port="20880"/>
<bean id="demoService" class="cn.ttext.demo.dubbo.api.impl.DemoServiceImpl"/>
<dubbo:service interface="cn.ttext.demo.dubbo.api.IDemoService" ref="demoService"/>
</beans>
DemoServiceImpl
package cn.ttext.demo.dubbo.api.impl;
import cn.ttext.demo.dubbo.api.IDemoService;
import com.alibaba.dubbo.rpc.RpcContext;
public class DemoServiceImpl implements IDemoService {
public String say(String val) {
return RpcContext.getContext().getLocalAddress() + ":" + val;
}
}
Main
package cn.ttext.demo.dubbo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
new ClassPathXmlApplicationContext(new String[]{"spring-dubbo-producer.xml"}).start();
System.out.println("服务已启动");
System.in.read();
}
}
dubbo-demo-consumer
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>cn.ttext.demo.dubbo</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.0.0</version>
<artifactId>duboo-demo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>cn.ttext.demo.dubbo</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
spring-dubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-demo-producer"/>
<dubbo:registry address="zookeeper://192.168.40.129:2181"/>
<dubbo:reference id="demoService" check="false" interface="cn.ttext.demo.dubbo.api.IDemoService"/>
</beans>
Main
package cn.ttext.demo.dubbo;
import cn.ttext.demo.dubbo.api.IDemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext(new String[]{"spring-dubbo-consumer.xml"});
classPathXmlApplicationContext.start();
IDemoService iDemoService = (IDemoService) classPathXmlApplicationContext.getBean("demoService");
while (true) {
Thread.sleep(1000);
System.out.println(iDemoService.say("Hello!!!"));
}
}
}