Dubbo实战(二) - 环境搭建
Dubbo 采用全 Spring 配置方式,透明化接入应用,对应用没有任何 API 侵入,只需用 Spring 加载 Dubbo 的配置即可,Dubbo 基于 Spring 的 Schema 扩展 进行加载。
如果不想使用 Spring 配置,可以通过 API 的方式 进行调用。
1 Provider
1.1 定义服务接口
- DemoService.java (该接口需单独打包,在服务提供方和消费方共享)
package org.apache.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}
1.2 在服务提供方实现接口
- DemoServiceImpl.java (对服务消费方隐藏实现)
package org.apache.dubbo.demo.provider;
import org.apache.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
1.3 用 Spring 配置声明暴露服务
- provider.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="hello-world-app" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl" />
</beans>
1.4 加载 Spring 配置
- Provider.java:
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/provider.xml"});
context.start();
System.in.read(); // 按任意键退出
}
}
2 Consumer
2.1 通过 Spring 配置引用远程服务
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" /> <!-- 生成远程服务***,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService" /> </beans>

2.2 加载Spring配置,并调用远程服务
Consumer.java (也可以使用 IoC 注入)
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务***
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}
}3 SpringBoot搭建
3.1 生成者
启动类

服务类

接口

配置

3.2 消费者
- 启动类
- 服务类
- 配置
3.3 文件结构

其实上面的操作都是直连提供者的具体实现
4 直连提供者
4.0 概念
消费端知道服务提供者的地址,直接进行连接
限制了分布式的易扩展性
一般只在测试环境中使用,绕过注册中心,只测试指定的服务提供者,这时候可能需要
- 点对点直连方式
以服务接口为单位,忽略注册中心的提供者列表,A 接口配置点对点,不影响 B 接口从注册中心获取列表
4.1 通过 XML 配置
如果是线上需求需要点对点,可在 <dubbo:reference> 中配置 url 指向提供者,将绕过注册中心,多个地址用分号隔开,配置如下(≥1.0.6 版本)
<dubbo:reference id="xxxService" interface="com.alibaba.xxx.XxxService" url="dubbo://localhost:20890" />
4.2 通过 -D 参数指定
在 JVM 启动参数中加入-D参数映射服务地址
key 为服务名,value 为服务提供者 url,此配置优先级最高,1.0.15 及以上版本支持
如
java -Dcom.alibaba.xxx.XxxService=dubbo://localhost:20890
4.3 通过文件映射
如果服务比较多,也可以用文件映射,用 -Ddubbo.resolve.file 指定映射文件路径,此配置优先级高于 dubbo:reference 中的配置
1.0.15 及以上版本支持,2.0 以上版本自动加载 ${user.home}/dubbo-resolve.properties文件,不需要配置
如
java -Ddubbo.resolve.file=xxx.properties
然后在映射文件 xxx.properties 中加入配置,其中 key 为服务名,value 为服务提供者 URL:
com.alibaba.xxx.XxxService=dubbo://localhost:20890
注意 为了避免复杂化线上环境,不要在线上使用这个功能,只应在测试阶段使用。
5 启动ZooKeeper
6 SpringBoot配置注册中心
修改配置文件,现在有注册中心了,不再是直连模式!
6.1 再配置Provider
添加zookeeper客户端依赖

添加zookeeper配置

6.2 再配置Consumer
添加zookeeper客户端依赖

添加zookeeper配置

修改消费服务类

参考
查看11道真题和解析