Dubbo

作者: 走在冷风中吧 | 来源:发表于2020-02-13 13:15 被阅读0次

架构技术

RPC: Remote Procedure Call 远程过程调用
解决进程之间的通信方式, Dubbo 是一种RPC的框架

Dubbo 的三大核心能力:

  1. 面向接口的远程方法调用
  2. 智能容错和负载均衡
  3. 服务自动注册和发现(zookeeper是其中一种注册中心)
image.png
  1. 服务器容器负责启动,加载,运行服务提供者
  2. 服务提供者在启动时,向注册中心注册自己提供的服务
  3. 服务消费者在启动时,向注册中心订阅自己需要的服务
  4. 注册服务中心返回服务提供者地址列表给消费者, 如有变更通过长连接消息通知消费者
  5. 服务消费者通过软负载均衡算法(通过代码实现的负载均衡算法)去调用服务提供者, 如果调用失败则进行调用下一个提供者
  6. 服务提供者和服务消费者,在内存中累计调用的时间和次数, 定时每分钟发送一次统计数据到监控中心

配置:

provider.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"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--    配置spring包扫描-->
    <context:component-scan base-package="com.lily.dubbo"/>

<!--    配置 dubbo 的应用名称-->
    <dubbo:application name="dubbo_provide"/>

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

<!--    配置通信协议, 每个服务提供者的 port 不应该一样-->
    <dubbo:protocol name="dubbo" port="8888"/>

    <!--配置dubbo注解包扫描-->
    <dubbo:annotation package="com.lily.dubbo.service.impl"/>

<!--    加入dubbo扫包之后不再需要手动注册了-->
<!--    把service等提供服务的注册到注册中心  1. 先构建对象 2.把业务对象注册到zookeeper-->
<!--    <bean class="com.lily.dubbo.service.impl.UserServiceImpl" name="userService"/>-->
<!--    <dubbo:service interface="com.lily.dubbo.service.UserService" ref="userService"/>-->

</beans>

service: 使用注解向注册中心进行注册

package com.lily.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.lily.dubbo.service.UserService;

//这个service要使用dubbo的service, 这样才能将它注册到注册中心
@Service
public class UserServiceImpl implements UserService {

    public boolean login(String username, String pwd) {
        System.out.println("UserServiceImpl.login"+ "username="+username + ",pwd="+pwd);
        if ("admin".equals(username) && "123".equals(pwd)){
            return true;
        }
        return false;
    }

}

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

    <context:component-scan base-package="com.lily.dubbo.controller"/>
<!--    配置dubbo的应用名称-->
    <dubbo:application name="dubbo-consumer"/>
<!--    配置订阅的位置-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

<!--    配置订阅到的引用类型-->
    <dubbo:reference interface="com.lily.dubbo.service.UserService" id="userService"/>

    <!-- 扫描的方式暴露接口 -->
    <dubbo:annotation package="com.lily.dubbo.controller" />

<!--    将userService对象注入到controller中-->
<!--    <bean class="com.lily.dubbo.controller.UserController">-->
<!--        <property name="userService" ref="userService"/>-->
<!--    </bean>-->

</beans>

controller: 使用注解方式获得对象索引

package com.lily.dubbo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lily.dubbo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {


//注意reference也需要使用dubbo包的
    @Reference
    private UserService userService;
    
    @RequestMapping("/login")
    @ResponseBody
    public String login(String userName, String pwd) {
        System.out.println("UserController.login username = "+userName+", pwd ="+pwd);
        boolean login = userService.login(userName, pwd);
        if (login)
            return "success";
        else
            return "failed";
    }

}

相关文章

网友评论

      本文标题:Dubbo

      本文链接:https://www.haomeiwen.com/subject/kgvcfhtx.html