一、注册中心zookeeper搭建,首先下载zookeeper包
下载压缩包以后接下来步骤:解压 ==> 进入解压后文件的conf目录 ==> zoo_sample.cfg文件改名为zoo.cfg ==> 修改zoo.cfg文件,dataDir=xxx修改为dataDir=../data并保存 ==> 新建一个和conf同级的文件夹data ==> 在zookeeper-3.4.11 bin目录下打开cmd,执行zkServer.cmd(mac或linux使用sh zkServer.sh start命令),这样zookeeper就启动了(端口默认2181,要修改端口则需修改zoo.cfg文件)。测试:执行zkCli.cmd,输入命令:get / 得到zookeeper(默认就有的节点)。输入命令:create -e /mqb 123456(创建mqb节点,值为123456)。再次执行 get / 得到结果zookeeper,mqb。执行 get /mqb 得到结果123456。
下载链接:https://archive.apache.org/dist/zookeeper/zookeeper-3.4.11/
二、下载dobbo管理控制台
下载链接:https://github.com/apache/dubbo-admin/tree/master
下载压缩包以后接下来步骤:解压后进入dubbo-admin ,修改resources下application.properties配置文件中的zookeeper注册中心地址,这里默认本地==> 打开cmd ==> mvn clean package ==> 启动命令:java -jar xxx.jar启动target目录下的jar包(一定要在jar包的目录执行命令,这里jar包在target目录下,所以切换到target目录) ==> 浏览器输入localhost:7001访问 ==> 打开页面后输入账号密码,如下
登录成功过后界面如下
三、三方工具代码(可重复使用的service或bean放在此包下,作为依赖引入到生产者dubbo-provider和消费者dubbo-consumer)
H
3.1 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
3.2 HelloService
package com.example.dubboapi.service;
public interface HelloService {
String sayHello(String name);
}
四、生产者或服务提供者
4.1 项目结构:红色框框重点关注一下
4.2 pom.xml(引入dubbo依赖三方工具包依赖)。
<?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>com.demo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!--三方工具-->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
其中dubbo依赖使用的是springboot的依赖,也可用如下两个依赖(dubbo依赖和操作zookeeper客户端依赖,注意版本对应,dubbo2.6之前对应zkclient,2.6之后对应curator)替代,替代以后日志打印有点不太一样,若出现如下日志属于正常现象
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
4.3 provider.xml(resources目录下),简记四要素:application、registry、protocol、service
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://localhost:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.example.dubboapi.service.HelloService" ref="helloService" />
<!-- 和本地bean一样实现服务 -->
<bean id="helloService" class="com.example.dubboprovider.rpc.HelloServiceImpl" />
</beans>
4.4 HelloServiceImpl
package com.example.dubboprovider.rpc;
import com.example.dubboapi.service.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello "+name;
}
}
4.5 Provider
其中ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml")中的“provider.xml”是因为provider.xml在resources目录下。
package com.example.dubboprovider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read();
}
}
五、消费者
项目结构:红色框框重点了解一下。
5.1 pom.xml同生产者
5.2 consumer.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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
<dubbo:registry address="zookeeper://localhost:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="helloService" interface="com.example.dubboapi.service.HelloService" />
</beans>
5.3 Consumer
package com.example.dubboconsumer;
import com.example.dubboapi.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
context.start();
HelloService helloService = (HelloService) context.getBean("helloService"); // 获取远程服务代理
String hello = helloService.sayHello("world"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
System.in.read();
}
}
六、结果
也可查看dubbo管理控制台