dubbo 示例(dubbo 二)

详细内容参考dubbo官网:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

启动时检查

  • Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check=“true”。

  • 可以通过 check=“false” 关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。

  • 另外,如果你的 Spring 容器是懒加载的,或者通过 API 编程延迟引用服务,请关闭 check,否则服务临时不可用时,会抛出异常,拿到 null 引用,如果 check=“false”,总是会返回引用,当服务恢复时,能自动连上。

通过 spring 配置文件

关闭某个服务的启动时检查 (没有提供者时报错):

<dubbo:reference interface="com.foo.BarService" check="false" />

关闭所有服务的启动时检查 (没有提供者时报错):

<dubbo:consumer check="false" />

关闭注册中心启动时检查 (注册订阅失败时报错):

<dubbo:registry check="false" />

多协议支持

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="order-provider-app" owner="order-test" />  
    <!-- 使用zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://192.168.1.103:2181?client=zkclient" client="zkclient"/>
    <!-- rpc协议 -->
    <dubbo:protocol name="dubbo" port="20880" />  
    <!-- 使用hessian协议暴露服务 -->
    <dubbo:protocol name="hessian" port="8090" server="jetty"/>
    
    <!-- 对外暴露接口 -->
    <dubbo:service interface="org.dubbo.order.api.IOrderService" ref="orderService" />  
    <!-- 实例化对象 -->
    <bean id="orderService" class="org.dubbo.order.provider.OrderServiceImpl" />
    <!-- 对外暴露接口 -->
    <dubbo:service interface="org.dubbo.order.api.IOrderQueryService" ref="orderQueryService" protocol="hessian"/>  
    <!-- 实例化对象 -->
    <bean id="orderQueryService" class="org.dubbo.order.provider.OrderQueryServiceImpl" />
</beans>

消费协议指明hessian

<?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="order-consumer-app" owner="client-test" />  
    <!-- 使用zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://192.168.1.103:2181?client=zkclient" client="zkclient"/>
    <!-- rpc协议 -->
    <dubbo:protocol name="dubbo" port="20880" />  
    <!-- 远程对象代理 -->
   	<dubbo:reference id="orderService" interface="org.dubbo.order.api.IOrderService"/>
    <!-- 远程对象代理 -->
   	<dubbo:reference id="orderQueryService" interface="org.dubbo.order.api.IOrderQueryService" protocol="hessian"/>
</beans>

多注册中心

Dubbo 支持同一服务向多注册中心同时注册,或者不同服务分别注册到不同的注册中心上去,甚至可以同时引用注册在不同注册中心上的同名服务。

多注册中心注册

同时注册到多个注册中心,这里同时注册到hangzhouRegistry,qingdaoRegistry注册中心

<?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="hangzhouRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />
    <!-- 向多个注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" />
</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="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    <!-- 向中文站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
    <!-- 向国际站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
</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="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    <!-- 引用中文站服务 -->
    <dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="chinaRegistry" />
    <!-- 引用国际站站服务 -->
    <dubbo:reference id="intlHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="intlRegistry" />
</beans>

多版本

当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。

可以按照以下的步骤进行版本迁移:

  1. 在低压力时间段,先升级一半提供者为新版本
  2. 再将所有消费者升级为新版本
  3. 然后将剩下的一半提供者升级为新版本

老版本服务提供者配置:

<dubbo:service interface="com.foo.BarService" version="1.0.0" />

新版本服务提供者配置:

<dubbo:service interface="com.foo.BarService" version="2.0.0" />

老版本服务消费者配置:

<dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" />

新版本服务消费者配置:

<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />

如果不需要区分版本,可以按照以下的方式配置 [1]:

<dubbo:reference id="barService" interface="com.foo.BarService" version="*" />

异步调用

在 consumer.xml 中配置

	<dubbo:reference id="orderQueryService" interface="org.dubbo.order.api.IOrderQueryService" protocol="dubbo">
   		<dubbo:method name="getFooAsync" async="true" return="true" />
   	</dubbo:reference>

在 provider.xml 中配置

 <dubbo:service interface="org.dubbo.order.api.IOrderQueryService" ref="orderQueryService" protocol="dubbo">
    	<dubbo:method name="getFooAsync" async="true" return="true" />
    </dubbo:service>  

调用main

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("order-consumer.xml");
        // 订单调用
    	IOrderQueryService orderQueryService = applicationContext.getBean(IOrderQueryService.class);
    	Foo foo = orderQueryService.getFooAsync(121212);
    	System.out.println(foo); // null
    	Future<Foo> future = RpcContext.getContext().getFuture();
    	Foo foo2 = future.get();
    	System.out.println(foo2);

只订阅

为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如果一个正在开发中的服务提供者注册,可能会影响消费者不能正常运行。

可以让服务提供者开发方,只订阅服务(开发的服务可能依赖其它服务),而不注册正在开发的服务,通过直连测试正在开发的服务。

在这里插入图片描述
禁用注册配置

<dubbo:registry address="10.20.153.10:9090" register="false" />

只注册

不订阅服务
禁用订阅配置

<dubbo:registry id="hzRegistry" address="10.20.153.10:9090" />
<dubbo:registry id="qdRegistry" address="10.20.141.150:9090" subscribe="false" />

负载均衡

在集群负载均衡时,Dubbo 提供了多种均衡策略,缺省为 random 随机调用。

负载均衡策略
Random LoadBalance
  • 随机,按权重设置随机概率。
  • 在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。
RoundRobin LoadBalance
  • 轮询,按公约后的权重设置轮询比率。
  • 存在慢的提供者累积请求的问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。
LeastActive LoadBalance
  • 最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
  • 使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。
ConsistentHash LoadBalance
  • 一致性 Hash,相同参数的请求总是发到同一提供者。
  • 当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。
  • 算法参见:http://en.wikipedia.org/wiki/Consistent_hashing
  • 缺省只对第一个参数 Hash,如果要修改,请配置 <dubbo:parameter key=“hash.arguments” value=“0,1” />
  • 缺省用 160 份虚拟节点,如果要修改,请配置 <dubbo:parameter key=“hash.nodes” value=“320” />
配置
服务端服务级别
<dubbo:service interface="..." loadbalance="roundrobin" />
客户端服务级别
<dubbo:reference interface="..." loadbalance="roundrobin" />
服务端方法级别
<dubbo:service interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:service>
客户端方法级别
<dubbo:reference interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:reference>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Dubbo是一个基于Spring Cloud构建的企业级分布式服务框架,它简化了Dubbo的集成和配置。Dubbo本身是一个高性能、高可用的RPC(Remote Procedure Call)框架,而Spring Cloud Dubbo则提供了一种更优雅的方式来使用Dubbo。 下面是一个简单的Spring Cloud Dubbo示例: 1. 首先,你需要在项目中引入Spring Cloud Alibaba的依赖,因为Dubbo已被包含在其模块中。在`pom.xml`文件中添加如下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-dubbo</artifactId> </dependency> ``` 2. 定义服务提供者接口(Provider Interface): ```java @Service public interface HelloService { String sayHello(String name); } ``` 3. 创建服务提供者的实现类: ```java @Component public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name; } } ``` 4. 在消费者端,创建一个依赖注入HelloService的服务调用组件: ```java @RestController public class ConsumerController { private final HelloService helloService; @Autowired public ConsumerController(HelloService helloService) { this.helloService = helloService; } @GetMapping("/hello") public String consumeHello(@RequestParam("name") String name) { return helloService.sayHello(name); } } ``` 5. 最后,在应用启动配置中启用Dubbo: ```yaml spring: cloud: dubbo: application: name: consumer-service # 消费者服务名 registry: address: zookeeper://localhost:2181 # 注册中心地址 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值