首页 > 其他分享 >Dubbo -介绍以及基本使用(xml方式与properties方式)

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

时间:2022-11-30 14:33:37浏览次数:45  
标签:xml Dubbo www http dubbo springframework org properties schema


Dubbo介绍

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

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

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

 Dubbo结构及功能

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

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

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

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

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

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

6、monitor统计rpc的调用频次

项目部署中的dubbo

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

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

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

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

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

Dubbo标签

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

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

 

标签:xml,Dubbo,www,http,dubbo,springframework,org,properties,schema
From: https://blog.51cto.com/u_14906615/5899438

相关文章

  • Dubbo -注解方式和Api方式
    注解方式服务端<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema......
  • [canvas]ncaught TypeError: Cannot read properties of null (reading 'getContext')
    相信你和我一样是直接复制大佬的js代码(笑)————————————主要问题在于:js先加载完了,html才加载,导致js获取不了html的对象解决办法:  ......
  • XML的简单认识
    一、初识XML1.1XML和HTML的对比1.1.1用途方面XML主要用于数据存储、传输、配置文件;HTML主要用于页面展示。(一开始XML想替换HTML,后来失败了就转数据存储应用了。)1.1.......
  • Java学习笔记XML(3)
    XML简介XML即可扩展的标记语言。因此该语言中所有的标签都是没有预先定义的,开发者可以自己随意的指定。目前为止所有的标记的语言都属于开源的语言。由W3C组织进行一个基本......
  • php中的XML DOM(10)
    1.PHPDOM(1)Php中的DOM跟javascript不一样,属性不用另外增加一个节点2.主要类    DOMDocument:文档类    DOMNodeList:节点列表类    DOMNode:节点类   ......
  • XML 与ABAP对象转换
    sap与外部系统通过接口交互时,数据的传递通常有XML,JSON等格式,此处介绍XML与ABAP结构、内表互转的两种常用方法。A.一种是通过类cl_xml_document解析转换XML字符串,该方式......
  • XML展示
       DATA: lr_xml TYPE REF TO cl_xml_document.  DATA: lv_rc TYPE int4.   CREATE OBJECT lr_xml.  CALL METHOD lr_xml->parse_xstring  ......
  • 实践案例:平安健康的 Dubbo3 迁移历程总结
    本篇是ApacheDubbo的实践案例。感兴趣的朋友可以访问官网了解更多详情,或搜索关注官方微信公众号ApacheDubbo跟进最新动态。1背景我们公司从15年开始就使⽤dubb......
  • Spring Boot+Mybatis:实现数据库登录注册与两种properties配置参数读取
    〇、参考资料1、hutool介绍https://blog.csdn.net/abst122/article/details/1240913752、SpringBoot+Mybatis实现登录注册https://www.cnblogs.com/wiki918/p/1622175......
  • springboot整合dubbo3.0
    项目结构父工程只要一个pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XML......