SSM项目dubbo配置

dubbo架构

http

节点 角色名称
Provider 暴露服务的服务提供方 服务方
Consumer 调用远程服务的服务消费方 调用方
Registry 服务注册与发现的注册中心
Monitor 统计服务的调用次数和调用时间的监控中心
Container 服务运行容器

服务调用过程

  1. 服务提供者在启动时,向注册中心注册自己提供的服务。
  2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送通知消费者。
  4. 服务消费者,从注册中心摘取服务提供者列表,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

ssm配置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.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

      <!--  发布服务的名称  -->
      <dubbo:application name="dubbo_provide"/>

      <!--  注册中心 zookeeper:-->
      <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

      <!--单个指定-->
      <!-- service:  注册上去服务
          interface: 发布服务的接口
          ref: spring容器的bean对象
          将来通过这个interface调用服务时,就来调用spring容器中的对象的方法
      -->
      <dubbo:service interface="com.foxconn.service.UserService" ref="userService"/>

      <!--  服务的执行者  -->
      <bean id="userService" class="com.foxconn.service.impl.UserServiceImpl"/>

<!--
      批量扫描,发布服务 与上二选一
      注册容器使用的注解需使用此com.alibaba.dubbo.config.annotation.Service
      @Service(interfaceClass = CheckGroupService.class) 
            当注册的类使用了aop时(事务等增强) 类会被包装为proxy需在 注册容器的注解上指定interfaceClass自己的接口类型来标识

      <dubbo:annotation package="com.itheima.service.impl"/>
-->

      <!--
        超时全局设置 10分钟
        check=false 不检查服务提供方,开发阶段建议设置为false
        check=true 启动时检查服务提供方,如果服务提供方没有启动则报错
      -->
      <dubbo:consumer timeout="600000" check="false"/>


</beans>

注意:

  • 消费者与提供者应用名称不能相同

  • 如果有多个服务提供者,名称不能相同,通信端口也不能相同

  • 只有服务提供者才会配置服务发布的协议,默认是dubbo协议,端口号是20880

服务调用者配置

<?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.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!--  发布的名称  -->
    <dubbo:application name="dubbo_consumer"/>
<!--  注册中心  -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--  服务订阅扫包
      在controller的服务注入使用com.alibaba.dubbo.config.annotation.Reference 的@Reference 
    <dubbo:annotation package="com.itheima"/>  -->

    <dubbo:reference interface="com.foxconn.service.UserService" id="userService"/>

<!--  启动时是否检查服务提供者是否存在,true: 则会检查【上线时】,没有则报错。false不检查
 retries: 失败后的重试次数
 -->
    <dubbo:consumer check="false" timeout="2000" retries="2"/>

</beans>

version与group(选配)

version一般用于业务的升级, 用2位数代表着版本, 通常是在服务提示者@Service(version="1.0"), 消费者调用时要指定版本号 @Reference(version="1.0"),就可以调用
Group 区分业务分组, 使用与version 类似,只是定义不一样而已(范围group>version),发布与调用时都要指定group的值。@Service(group="group"), @Reference(group="group")
http
http

posted @ 2021-01-25 19:24  笨蛋树上笨蛋果  阅读(218)  评论(0编辑  收藏  举报