Dubbo -介绍以及基本使用(xml方式与properties方式)

Dubbo介绍

一个分布式、高性能、透明化的RPC服务框架。

提供服务自动注册、自动发现等高效服务治理方案。

其功能主要包括:高性能NIO通讯及多协议集成,服务动态寻址与路由,软负载均衡与容错,依赖分析与降级等。

 Dubbo结构及功能

1、container负责启动、加载、运行provider

2、provider启动时,向registry注册自己的服务

3、cousumer启动时,向registry订阅自己的服务

4、registry提供provider列表给consumer,实时推送变动情况

5、consumer根据provider列表,按负载算法选一台provider调用

6、monitor统计rpc的调用频次

项目部署中的dubbo

1、一台应用服务(application)内,既有对外提供服务(provider),也有依赖外部服务(consumer)

2、 provider涉及:registry/protocol/service/method/provider

3、 consumer涉及:registry/reference/method/consumer

4、每台服务接口的信息,都会反映到monitor。以application的名称标识属于哪个应用。

Dubbo标签

1、标签属性有继承关系,即:下层有设置则使用,未配置则沿用上一级的设置

2、 timeout/retries/ loadbalance消费方未设置,则沿用服务方的设置。

常规配置(xml方式与properties方式)

1、关闭某个服务的启动时检查:(没有提供者时报错) <dubbo:reference check="false" />

2、关闭所有服务的启动时检查:(没有提供者时报错)  写在定义服务消费者一方 <dubbo:consumer check="false" />

3、关闭注册中心启动时检查:(注册订阅失败时报错) <dubbo:registry check="false" />

4、引用缺省是延迟初始化的,改为饥饿加载 <dubbo:reference init="true" />

5、禁用注册 <dubbo:registry register="false" />

6、回声测试:所有服务自动实现EchoService接口, 强转EchoService测试可用性 < dubbo:reference id=“aaa" interface="com.xxx.XxxService" /> EchoService echoService = ( EchoService ) ctx.getBean(“aaa")

XML与Spring集成方式

服务端

<?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:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans.xsd
	      http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
	 http://code.alibabatech.com/schema/dubbo
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <context:component-scan base-package="com.enjoy.dao"/>

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="storeServer"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.46.133:2181"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:protocol name="rmi" port="21880"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.enjoy.service.OrderService" ref="orderService" protocol="dubbo">
        <dubbo:method name="getDetail" cache="lru" />
    </dubbo:service>

    <dubbo:service interface="com.enjoy.service.UserService" ref="userService" protocol="rmi"/>
    <dubbo:service interface="com.enjoy.service.VipUserService" ref="vipUserService" />

     <!--和本地bean一样实现服务 -->
    <bean id="orderService" class="com.enjoy.service.impl.OrderServiceImpl"/>
    <bean id="userService" class="com.enjoy.service.impl.UserServiceImpl"/>
    <bean id="vipUserService" class="com.enjoy.service.impl.VipUserServiceImpl"/>

</beans>

启动spring容器:

public class StoreServer {
    public static void main(String[] args) throws IOException {
        /**
         * dubbo.xml
         * dubbo_annotation.xml
         */
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:dubbo.xml");
        context.start();

        System.out.println("-----dubbo开启-----");

//        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
}

客户端

<?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.xsd        
	 http://code.alibabatech.com/schema/dubbo        
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="enjoyStore"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.46.133:2181"/>

    <dubbo:consumer check="false"/>

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="orderService" interface="com.enjoy.service.OrderService" >
        <dubbo:method name="getDetail" cache="lru" />
    </dubbo:reference>

    <dubbo:reference id="userService" interface="com.enjoy.service.UserService"  />
    <dubbo:reference id="vipUserService" interface="com.enjoy.service.VipUserService"  />


</beans>

<import resource="dubbo.xml"/>

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    version="3.0">  
 
  <display-name>Archetype Created Web Application</display-name>

    <!-- Spring MVC servlet -->
    <servlet>  
        <servlet-name>SpringMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:spring-mvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
        <async-supported>true</async-supported>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVC</servlet-name>  
        <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  
    <welcome-file-list>  
        <welcome-file>/index.jsp</welcome-file>  
    </welcome-file-list>  

</web-app>

properties方式

是指在xml里面没有的配置,会自动去查找properties里面的配置

dubbo_properties.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:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans.xsd
	      http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
	 http://code.alibabatech.com/schema/dubbo
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <context:component-scan base-package="com.enjoy.dao"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.enjoy.service.OrderService" ref="orderService"/>
    <dubbo:service interface="com.enjoy.service.UserService" ref="userService"/>
    <dubbo:service interface="com.enjoy.service.VipUserService" ref="vipUserService"/>

     <!--和本地bean一样实现服务 -->
    <bean id="orderService" class="com.enjoy.service.impl.OrderServiceImpl"/>
    <bean id="userService" class="com.enjoy.service.impl.UserServiceImpl"/>
    <bean id="vipUserService" class="com.enjoy.service.impl.VipUserServiceImpl"/>

</beans>

dubbo.properties

dubbo.application.name=storeServer_properties
dubbo.registry.address=zookeeper://192.168.46.133:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=28080

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值