2.Hello World及注册中心的选择使用
2018-10-27 本文已影响0人
小manong
一、spring整合Dubbo使用
- 在dubbo开发中可以使用编程式的开发也可以结合spring等框架进行开发,由于编程式开发代码侵入性强,在实际生成中很少时候,因此直接和spring配合使用作为案例。
1.创建spring项目(这里使用springBoot)
最终项目结构
- 父工程中的pom.xml文件
<modules>
<module>api</module>
<module>server</module>
<module>consumer</module>
</modules>
<properties>
<!-- 使用UTF-8编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 使用jdk1.8版本 -->
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热启动:每自修改后, 程序自动启动spring Application上下文。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--dubbo文件-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>
2.api模块
- api模块主要提供服务的一些接口及基础信息,这里只是提供一个测试接口
public interface HelloService {
/**
* 服务方法
*/
public String say();
}
3.provider服务创建模块
(1)添加依赖,依赖于api模块及spring、dubbo等框架,这里父工程提供了
<dependencies>
<dependency>
<artifactId>api</artifactId>
<groupId>com.qiu</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
(2)创建接口实现类
public class HelloServiceImpl implements HelloService {
@Override
public String say() {
System.out.println("服务执行,是在服务端=======================");
return "服务执行的结果,hello world";
}
}
(3)创建dubbo配置文件provider.xml,这里注册中心使用multicast广播注册中心
<?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-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="consumerService" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.qiu.service.HelloService" ref="helloService" />
<!-- 和本地bean一样实现服务 -->
<bean id="helloService" class="com.qiu.service.impl.HelloServiceImpl" />
</beans>
(4)创建测试方法
public class AppTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
HelloService helloService = (HelloService) context.getBean("helloService");
System.out.println("服务启动成功===========");
//是程序循环等待中,模拟不断运行的生成环境
try {
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.consumer服务消费模块
(1)和服务创建模块一样依赖于api模块及spring、dubbo等框架
<dependencies>
<dependency>
<artifactId>api</artifactId>
<groupId>com.qiu</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
(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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--配置一个服务名 -->
<dubbo:application name="clientServer"/>
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 引用服务 ,id是为了获取服务用-->
<dubbo:reference interface="com.qiu.service.HelloService" id="helloService" ></dubbo:reference>
</beans>
(3)进行服务消费
public class AppSpringTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:consumer.xml");
HelloService helloService=(HelloService) context.getBean("helloService");
System.out.println("服务消费=========");
String result = helloService.say();
System.out.println(result);
}
}
4.进行测试
-
第一步开启服务创建模块应用
(1)服务创建模块开启时候
-
第二部开启服务消费模块应用
(2)服务消费模块返回了结果
(3)同时服务创建模块结构通知调用后进行相应的逻辑处理,并返回结果
二、使用zookeeper作为注册中心(官方推荐)
1.Zookeeper简介
- 官网:https://zookeeper.apache.org/
-
注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbo服务的注册中心,工业强度较高,可用于生产环境。Dubbo官方推荐使用 zookeeper 注册中心
zookeeper作为注册中心使用及说明
-
支持的功能:
zk作为注册中心支持的功能
2.window安装zookeeper
(1)进入zookeeper官网:http://mirror.bit.edu.cn/apache/zookeeper/下载安装包并解压到D盘目录下面。
(2)修改一些常用的配置
- 修改conf文件夹里的zoo_sample.cfg文件,文件名改成zoo.cfg
- 修改zoo.cfg文件的以下配置:
# 存在内存中的数据的快照位置
dataDir=D:/zookeeper-3.4.5/data
#存储错误日志的位置
dataLogDir=D:/zookeeper-3.4.5/log
# zookeeper的端口号
clientPort=2181
(3)启动zookeeper,看看是否可以用
- 双击bin目录下的zkServer.cmd
3.dubbo中使用zookeeper为注册中心
(1)引入依赖(服务模块和客户端模块)
- 引入依赖,其中客户端有多种选择比如还有curator
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
(2)修改配置文件注册中心为zookeeper
- zookeeper单机模式
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
- zookeeper集群模式
<dubbo:registry address="zookeeper://10.20.153.10:2181?
backup=10.20.153.11:2181,10.20.153.12:2181" />
或者
<dubbo:registry protocol="zookeeper"
address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />
三、dubbo官方推荐配置配置文件
- 具体请参考官方文档http://dubbo.apache.org/zh-cn/
1
2
四、相关问题解决部分
1.多接口解决
- 上面只是一个接口的注册,如果涉及多个接口,不可能在xml配置文件中配置所有的接口,如果那样的话,xml文件会显得很雍炯并且容易出现错误。那有没有解决办法呢?