dubbo学习笔记(一)——dubbo的作用及简单应用

一、简介

Apache Dubbo 是一款微服务开发框架,主要功能是RPC通信与微服务治理。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。同时 Dubbo 是高度可扩展的,用户几乎可以在任意功能点去定制自己的实现,以改变框架的默认行为来满足自己的业务需求。

详细文档可以查看dubbo官网:https://dubbo.apache.org/zh/docs/
dubbo源码地址:https://github.com/apache/dubbo
dubbo示例源码地址:https://github.com/apache/dubbo-samples

二、dubbo的简单应用

1、创建三个项目:
provider:服务提供项目
consumer:服务调用项目
service-api:服务的公共接口项目
2、引入依赖包

	<dependency>
        <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注明-->
<dubbo:reference interface="包名.HelloService" id="helloService" registry="rg1"/>

详情参考dubbo官方文档注册中心配置:https://dubbo.apache.org/zh/docs/references/registry/

2、远程通信协议

Dubbo 对于 RPC 通信协议的支持,不仅仅是原生的 Dubbo 协议,它还围绕着 rmi、hessian、http、webservice、thrift、rest 有了多协议的支持,使得其他 rpc 框架的应用程序可以快速的切入到 dubbo生态中。 同时,对于多协议的支持,使得不同应用场景的服务,可以选择合适的协议来发布服务,并不一定要使用 dubbo 提供的长连接方式。
dubbo支持的协议(不同版本会有所不同):
在这里插入图片描述
以webService为例:
1、引入依赖:webService基于http,需要web容器的支持,所以引入jetty。

   		<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-simple</artifactId>
        <version>3.3.2</version>
      </dependency>
      <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.3.2</version>
      </dependency>
      <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.4.19.v20190610</version>
      </dependency>
      <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.4.19.v20190610</version>
      </dependency>

2、配置spring的配置文件:
provider:

<dubbo:protocol name="webservice" port="8080" server="jetty"/>
<dubbo:reference interface="包名.HelloService" id="helloService" registry="rg1"  protocol="webservice"/>

启动服务之后 ,可以使用 :
http://localhost:8080/包名.HelloService?wsdl 来获得Webservice的wsdl描述文档。

这里需要注意zookeeper新版本引入了AdminServer功能,此功能也使用了jetty,可能会造成8080端口冲突。
解决方法查看我的博客:https://blog.csdn.net/qq_34609889/article/details/123971669

Dubbo 允许配置多协议,在不同服务上支持不同协议或者同一服务上同时支持多种协议。不同服务在性能上适用不同协议进行传输,比如大数据用短连接协议,小数据大并发用长连接协议

<?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="world"  />
    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
    <!-- 多协议配置 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:protocol name="rmi" port="1099" />
    <!-- 使用dubbo协议暴露服务 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
    <!-- 使用rmi协议暴露服务 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" /> 
</beans>
<?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="world"  />
    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
    <!-- 多协议配置 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:protocol name="hessian" port="8080" />
    <!-- 使用多个协议暴露服务 -->
    <dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" protocol="dubbo,hessian" />
</beans>
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值