一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第4天,点击查看活动详情。
一、简介
Apache Dubbo 是一款微服务开发框架,主要功能是RPC通信与微服务治理。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。同时 Dubbo 是高度可扩展的,用户几乎可以在任意功能点去定制自己的实现,以改变框架的默认行为来满足自己的业务需求。
详细文档可以查看dubbo官网:dubbo.apache.org/zh/docs/
dubbo源码地址:github.com/apache/dubb…
dubbo示例源码地址:github.com/apache/dubb…
二、dubbo的简单应用
1、创建三个项目:
provider:服务提供项目
consumer:服务调用项目
service-api:服务的公共接口项目
2、引入依赖包
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.11</version>
</dependency>
3、定义接口和实现
在service-api下:定义公共接口:
public interface HelloService {
String sayHello(String name);
}
在provider定义实现类(provider需要引入service-api的依赖):
public class HelloServiceImpl implements HelloService {
@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();
}
}
4、用 Spring 配置声明暴露服务。xml配置文件默认放在resources/METAINF/spring/ 文件夹下,名字随意。 在provider下配置application.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="service-provider"/>
<!--配置注册中心,没有就用N/A-->
<dubbo:registry address="N/A"/>
<!--协议和端口-->
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="包名.HelloService" ref="helloService"/>
<bean id="helloService" class="包名.HelloServiceImpl"/>
</beans>
在consumer里定义(consumer需要引入service-api的依赖)application.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="service-consumer"/>
<!--配置注册中心-->
<dubbo:registry address="N/A"/>
<!--没配置注册中心的话需要使用服务的url protocol://ip:端口/路径-->
<dubbo:reference interface="包名.HelloService" id="helloService "
url="dubbo://192.168.56.1:20880/包名.HelloService"/>
</beans>
5、启动以及调用服务 启动服务
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/application.xml");
context.start();
System.out.println("dubbo service started");
//让程序一直运行
System.in.read();
}
或者直接使用dubbo的启动方法
public static void main( String[] args ) { Main.main(new String[]{"spring","log4j"}); }
调用服务:
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/application.xml");
context.start();
HelloService helloService = ( HelloService) context.getBean("helloService ");
String hello = helloService .sayHello("world");
System.out.println(hello);
}
服务分组
使用服务分组区分服务接口的不同实现,当一个接口有多种实现时,可以用 group 区分。 服务端:
<dubbo:service group="feedback" interface="com.xxx.IndexService" /> <dubbo:service group="member" interface="com.xxx.IndexService" />
客户端引用:
<dubbo:reference id="feedbackIndexService" group="feedback" interface="com.xxx.IndexService" />
<dubbo:reference id="memberIndexService" group="member" interface="com.xxx.IndexService" />
任意组:
<dubbo:reference id="barService" interface="com.foo.BarService" group="*" />
三、dubbo的注册中心及协议的支持
1、注册中心
作为主流的服务治理组件,当然不能没有注册中心,dubbo最开始使用的是zookeeper来支持注册中心。在新版本中也提供了诸如consul、etcd、nacos、sofa、zookeeper、redis、multicast等组件来实现注册中心。dubbo支持的注册中心(不同版本有所不同):
以zookeeper为注册中心为例:
1、引入依赖包(使用curator):
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
2、在spring的配置文件配置注册中心 provider:
单机配置:
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
或
<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181" />
集群配置:
<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:registry id="rg1" address="zookeeper://127.0.0.1:2181"/>
<dubbo:registry id="rg2" address="zookeeper://10.20.153.10:2181"/>
consume:
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--使用注册中心就不用使用url了,使用了多个配置中心,使用registry注明-->