Apache Dubbo 学习 一


前言

由于现在技术知识还不够,所以先从JAVA知识体系来扩展,今天先学习dubbo。个人感觉就是解决业务层与实现逻辑层分离的问题,并且可以分布式管理与版本迭代更新与灰量发布。

一、Apache Dubbo是什么?

Apache Dubbo 是一款高性能、轻量级的开源服务框架,提供了六大核心能力:面向接口代理的高性能RPC调用,智能容错和负载均衡,服务自动注册和发现,高度可扩展能力,运行期流量调度,可视化的服务治理与运维。下面是官方给的出的一张图,便于理解。
在这里插入图片描述

名词解析

名词解释
Provider服务提供方
Consumer服务消费方
Registry服务注册与发现的注册中心
Monitor服务监控中心
Container服务运行容器
init异步初始化
async异步
sync同步

关系说明

节点作用
Provider启动时,向注册中心注册自己提供的服务;接受调用时,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用;并且定时发心跳给监控中心,进行数据分析与异常排查。
Consumer启动时,在注册中心获取自己所需的服务;在处理业务时,根据注册中心给的地址调用提供者的服务;并且定时发心跳给监控中心,进行数据分析与异常排查。
Registry提供者初始化时将服务注册到注册中心,消费者初始化时到注册中心来获取服务列表,根据列表中的地址进行调用。
Monitor监控提供者与消费者的状态,已经数据收集分析。

二、使用步骤

直连模式

直连模式即不使用注册中心,直接调用的形式。

服务提供者项目

创建一个maven webapp项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

提供者项目目录结构

在这里插入图片描述

创建项目基本目录

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

设置目录类型

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

提供者依赖

pox.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>dubbo</groupId>
  <artifactId>1-link-provider</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <!--dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.4</version>
    </dependency>
    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
  </dependencies>
</project>
创建服务接口与实现类

目录结构如下图所示:
在这里插入图片描述
SomeService

package dubbo.service;

public interface SomeService {
    String hello(String msg);
}

SomeServiceImpl

package dubbo.service.impl;


import dubbo.service.SomeService;

public class SomeServiceImpl implements SomeService {
    public String hello(String msg) {
        return "hello " + msg;
    }
}
dubbo配置文件

在这里插入图片描述
dubbo-link-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://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="1-link-provider" />

    <!--指定协议与端口
        name:协议名称
        port:端口号
    -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--暴露服务
        interface:服务类名
        ref:spring接口中的标识名称
        register:使用直连方式,不适用注册中心,值:N/A
    -->
    <dubbo:service interface="dubbo.service.SomeService" ref="someServiceImpl" registry="N/A"/>

    <!--加载接口实现类-->
    <bean id="someServiceImpl" class="dubbo.service.impl.SomeServiceImpl" />
</beans>
spring配置文件

在这里插入图片描述
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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:dubbo-link-provider.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

在这里提供者就写完了。下来进行消费者项目实现。

直连war包生成

因为是直连的没有注册中心,所有消费者调用提供者的服务,需要通过war来调用。所以需要执行一个mvn命令来打包服务提供者的服务列表。在IDEA中双击 ctrl打开mvn控制台,在输入mvn 1-link-provider install并回车即可,1-link-provider是我的服务提供者项目名称,请自行调整。
在这里插入图片描述

服务消费者项目

前面创建文件夹就不重复写了,就写不一样的地方。

消费者项目目录结构

在这里插入图片描述

消费者依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>dubbo</groupId>
  <artifactId>2-link-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <!--dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.4</version>
    </dependency>
    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>dubbo</groupId>
      <artifactId>1-link-provider</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>
控制器

SomeController

package dubbo.web;

import dubbo.service.SomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SomeController {

    @Autowired
    private SomeService someService;

    @RequestMapping(value = "/hello")
    public String hello(Model model){
        //调用远程接口服务
        String hello = someService.hello("dubbo");
        model.addAttribute("hello",hello);
        return "hello";
    }
}
dubbo配置文件

dubbo-link-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"
       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="2-link-consumer"/>

    <!--引用远程接口
        id:远程接口服务的代理名称
        interface:全类名
        url:远程接口服务的url地址
        registry:直连方式,不使用注册中心 值:N/A
    -->
    <dubbo:reference id="someService"
                     interface="dubbo.service.SomeService"
                     url="dubbo://localhost:20880"
                     registry="N/A"/>
</beans>
spring配置文件

springmvc.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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描组件-->
    <context:component-scan base-package="dubbo.web"/>

    <!--注解驱动-->
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

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">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:dubbo-link-consumer.xml,classpath:springmvc.xml</param-value>
	</context-param>

	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:dubbo-link-consumer.xml,classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

配置tomcat

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
tomcat的端口记得修改,一个tomcat只能运行一个端口,不然会报端口占用。
在这里插入图片描述
消费者新增时,选择war修改一下,端口修改一下即可。
配置完之后,先运行提供者的tomcat,再运行消费者的tomcat即可。

提供者运行结果查看

在这里插入图片描述

消费者运行结果查看

在这里插入图片描述

启动失败了怎么看日志

pox.xml文件中,加上下面的依赖重新启动就可以了。

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.25</version>
  <scope>compile</scope>
</dependency>

总结

初学的时候,还是有点痛苦。但是学会了就会有点小开心,嘿嘿,加油!今天就总结到这了,注册中心与版本迭代的后面再更新~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值