目录

一、软件架构的演进过程【了解】二、Dubbo概述【了解】1、Dubbo简介2、Dubb架构

三、服务注册中心Zookeeper1、 安装2、 启动&停止

四、Dubbo快速入门1、服务提供方开发2、服务消费方开发

一、软件架构的演进过程【了解】

1、单体架构

一个包,包含了应用所有功能的应用程序,我们通常称之为单体应用。 简单理解:类比前端,把所有代码写到一个文件里。

2、垂直架构

按照业务进行切割,形成小的单体项目。 简单理解:前端的组件化,但是不同组件仍有重复代码。

3、SOA架构

将重复功能或模块抽取成组件的形式,对外提供服务,在项目与服务之间使用ESB(企业服务总线)的形式作为通信的桥梁。 简单理解:前端的组件化,并且把重复代码【粗略】抽离出来。

4、微服务架构

和 SOA 架构类似, 微服务架构强调的一个重点是“业务需要彻底的组件化和服务化”。 将系统服务层完全独立出来,抽取为一个一个的微服务。 抽取的粒度更细,遵循单一原则。 简单理解:前端的组件化,并且把重复代码【颗粒度更细】抽离出来。

二、Dubbo概述【了解】

1、Dubbo简介

Apache Dubbo是一款高性能的Java RPC框架。

什么是RPC?

RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。

Dubbo提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

2、Dubb架构

图例说明:

虚线都是异步访问,实线都是同步访问 蓝色虚线:在启动时完成的功能 红色虚线(实线)都是程序运行过程中执行的功能

调用关系说明:

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

三、服务注册中心Zookeeper

1、 安装

下载地址:http://archive.apache.org/dist/zookeeper/

2、 启动&停止

需要注意:

Zookeeper在Windows下启动只需要运行zkServer.cmd双击即可(需保证运行环境中正确安装了Java运行环境)

但是在有的时候会出现双击闪退的情况。针对闪退,可按照以下步骤进行解决: 1 、编辑zkServer.cmd文件末尾添加pause 。这样运行出错就不会退出,会提示错误信息,方便找到原因。例如原因如下:

2、重新双击运行zkServer.cmd 错误原因 ,找不到zoo.cfg配置文件

3、查看zkEnv.cmd 4 、增加zoo.cfg文件,如下 5、 zoo.cfg配置文件介绍

修改dataDir属性值,在上级目录下增加data目录 6、重新双击zkServer.cmd启动

四、Dubbo快速入门

Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。本小节就是要创建两个应用,一个作为服务的提供方,一个作为服务的消费方。通过Dubbo来实现服务消费方远程调用服务提供方的方法。

1、服务提供方开发

开发步骤:

先创建一个dubbodemo的工程。在文件夹下创建提供者和消费者项目。

1、创建maven工程(打包方式为war)dubbodemo_provider,在pom.xml文件中导入如下坐标

war

UTF-8

1.8

1.8

5.0.5.RELEASE

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-aspects

${spring.version}

org.springframework

spring-jms

${spring.version}

org.springframework

spring-context-support

${spring.version}

com.alibaba

dubbo

2.6.0

org.apache.zookeeper

zookeeper

3.4.7

com.github.sgroschupf

zkclient

0.1

javassist

javassist

3.12.1.GA

com.alibaba

fastjson

1.2.47

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

1.8

1.8

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

8081

/

2、配置web.xml文件

创建 webapp/WEB-INF/web.xml

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_3_1.xsd"

version="3.1">

contextConfigLocation

classpath:applicationContext*.xml

org.springframework.web.context.ContextLoaderListener

3、创建服务接口

package com.atguigu.service;

public interface HelloService {

public String sayHello(String name);

}

4、创建服务实现类

package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.atguigu.service.HelloService;

@Service

public class HelloServiceImpl implements HelloService {

@Override

public String sayHello(String name) {

return "hello " + name;

}

}

注意:服务实现类上使用的 Service 注解是Dubbo提供的,用于对外发布服务

5、在src/main/resources下创建 applicationContext-service.xml

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

启动服务

2、服务消费方开发

开发步骤:

1、创建 maven 工程(打包方式为war)dubbodemo_consumer,pom.xml 配置和上面服务提供者相同,只需要将 Tomcat 插件的端口号改为8082即可

war

UTF-8

1.8

1.8

5.0.5.RELEASE

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-aspects

${spring.version}

org.springframework

spring-jms

${spring.version}

org.springframework

spring-context-support

${spring.version}

com.alibaba

dubbo

2.6.0

org.apache.zookeeper

zookeeper

3.4.7

com.github.sgroschupf

zkclient

0.1

javassist

javassist

3.12.1.GA

com.alibaba

fastjson

1.2.47

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

1.8

1.8

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

8082

/

2、配置webapp/WEB-INF/web.xml文件

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_3_1.xsd"

version="3.1">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext-web.xml

1

springmvc

/

3、将服务提供者工程中的HelloService接口复制到当前工程

package com.atguigu.service;

public interface HelloService {

public String sayHello(String name);

}

4、编写Controller

package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.atguigu.service.HelloService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

@RequestMapping("/demo")

public class HelloController {

@Reference

private HelloService helloService;

@RequestMapping("/hello")

@ResponseBody

public String getName(String name){

//远程调用

String result = helloService.sayHello(name);

System.out.println(result);

return result;

}

}

注意:Controller中注入HelloService使用的是Dubbo提供的@Reference注解

5、在src/main/resources下创建applicationContext-web.xml

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

6、启动服务

7、运行测试

在浏览器输入http://localhost:8082/demo/hello?name=Jack,查看浏览器输出结果

*思考一*:上面的Dubbo入门案例中我们是将HelloService接口从服务提供者工程(dubbodemo_provider)复制到服务消费者工程(dubbodemo_consumer)中,这种做法是否合适?还有没有更好的方式?

答:这种做法显然是不好的,同一个接口被复制了两份,不利于后期维护。更好的方式是单独创建一个maven工程,将此接口创建在这个maven工程中。需要依赖此接口的工程只需要在自己工程的pom.xml文件中引入maven坐标即可。

*思考二*:在服务消费者工程(dubbodemo_consumer)中只是引用了HelloService接口,并没有提供实现类,Dubbo是如何做到远程调用的?

答:Dubbo底层是基于代理技术为HelloService接口创建代理对象,远程调用是通过此代理对象完成的。

*思考三*:上面的Dubbo入门案例中我们使用Zookeeper作为服务注册中心,服务提供者需要将自己的服务信息注册到Zookeeper,服务消费者需要从Zookeeper订阅自己所需要的服务,此时Zookeeper服务就变得非常重要了,那如何防止Zookeeper单点故障呢?

答:Zookeeper其实是支持集群模式的,可以配置Zookeeper集群来达到Zookeeper服务的高可用,防止出现单点故障。

参考链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: