dubbo介绍

3 篇文章 0 订阅
1 篇文章 0 订阅

dubbo介绍

介绍

一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:

  1. 面向接口的远程方法调用
  2. 智能容错和负载均衡
  3. 服务自动注册和发现

架构

  • 节点角色说明
    |节点 |角色说明 |
    | ---- | ---- |
    |Provider |暴露服务的服务提供方 |
    |Consumer |调用远程服务的服务消费方 |
    |Registry |服务注册与发现的注册中心 |
    |Monitor |统计服务的调用次数和调用时间的监控中心 |
    |Container |服务运行容器
  • 调用关系说明
  1. 服务容器负责启动,加载,运行服务提供者。
  2. 服务提供者在启动时,向注册中心注册自己提供的服务。
  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

快速启动

  • pom.xml要引入spring以及dubbo的依赖
        <!--spring容器依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!--springMVC依赖,包含了context容器,aop代理,bean对象,core核心,expression表达式语言-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!--dubbo依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.8</version>
        </dependency>

服务提供者

  1. 定义服务接口 DemoService.java
	package service;

	import entity.User;

	public interface UserService {
		User queryUserById(Integer id);
	}
  1. 在服务提供方实现接口 DemoServiceImpl.java
	package service.impl;

	import entity.User;
	import service.UserService;

	public class UserServiceImpl implements UserService {
		/*
		* 应该注入持久层框架返回一个user对象,在此只是模拟
		* 就不做mybatis到数据库中查数据了,直接new出一个对象返回
		* */
		@Override
		public User queryUserById(Integer id) {
			User user = new User();
			user.setUserName("孙烨");
			user.setId(1);
			user.setAge(18);
			return user;
		}
	}
  1. 用 Spring 配置声明暴露服务 dubbo-userService-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"
       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的配置文件,实际上也是一个spring配置文件-->
    <!--服务提供者声明名称,提供方应用信息,用于计算依赖关系,name需要保证唯一性-->
    <dubbo:application name="dubbo-userService-provider"/>
    <!--访问服务协议的名称额端口号,dubbo官方推荐使用dubbo协议,默认端口使用20880-->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口 register连接方式,N/A表示直连不通过注册中心 -->
    <dubbo:service interface="service.UserService" ref="userService" register="N/A"/>
    <!-- 和本地bean一样实现服务 -->
    <bean id="userService" class="service.impl.UserServiceImpl"/>
</beans>
  1. web.xml中配置监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--引入spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:dubbo-userService-provider.xml</param-value>
    </context-param>
    <!--
        在web应用启动的,ContextLoaderListener读取contextConfigLocation中定义的xml文件
        自动装配ApplicationContext的配置信息并产生WebApplicationContext对象
        然后将这个对象放置在ServletContext的属性里,这样我们就可以在servlet里得到WebApplicationContext对象。
    -->
    <!--设置监听器,创建application对象并放入全局作用域-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

服务消费者

  1. 消费者应当获取服务者所提供的类和接口,直接在pom.xml引入服务端jar包
    <dependencies>
       <!--spring容器依赖-->
   	...
       <!--springMVC依赖-->
   	...
       <!--dubbo依赖-->
   	...
   	<!--服务端依赖-->
       <dependency>
           <groupId>org.example</groupId>
           <artifactId>001_link_serviceProvider</artifactId>
           <version>1.0-SNAPSHOT</version>
       </dependency>
   </dependencies>
  1. 用 Spring 配置声明暴露服务 dubbo-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://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的配置文件,实际上也是一个spring配置文件-->
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-consumer"/>
    <!--
        生成远程服务代理,可以和本地bean一样使用demoService
        id:远程代理服务的名称
        interface: 远程接口的全限定类名
        url:服务接口地址
        registry:设置连接方式 N/A表示直接连接,不通过注册中西
     -->
    <dubbo:reference id="UserService" interface="service.UserService" url="dubbo://localhost:20880" registry="N/A"/>
</beans>	
  1. 声明spring配置文件,applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
		   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

		<!--组件扫描器-->
		<context:component-scan base-package="controller"/>
		<!--注解驱动-->
		<mvc:annotation-driven/>
		<!--
			视图解析
			使用redirect关键字显示重定向,绕过视图解析器
			modelAndView.setViewName("redirect:/WEB-INF/jsp/info.jsp")
		-->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/jsp/"/>
			<property name="suffix" value=".jsp"/>
		</bean>
	</beans>	

最终我发现,动力节点的这套课程中,服务消费者就不能通过自动装箱实现对UserService的获取,所以本天学习到此结束,转看黑马教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙烨_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值