JavaEE学习日志(一百一十一): 分布式项目基本构建(Dubbo和Zookeeper)

Dubbo、Zookeeper

  • SOA架构
  • Dubbo
  • Zookeeper
  • dubbo入门
    • 项目准备
    • 服务提供者web.xml
    • 服务提供者dubbo配置文件
    • 服务消费者web.xml
    • 服务消费者dubbo配置文件
    • 测试

SOA架构

Tomcat极限: 单台Tomcat极限基本是可以同时抗住500个并发.

传统行业项目: 并发低, 数据量小, 不需要高可用, 例如: oa, erp, crm项目, 或者政府项目, 银行项目等.
互联网项目: 高并发, 大数据量, 高可用 例如: 京东, 百度, 淘宝, 天猫, 饿了么, 美团, 滴滴, ofo, 钉钉

soa架构也叫分布式架构,由互联网项目使用
JavaEE学习日志(一百一十一): 分布式项目基本构建(Dubbo和Zookeeper)_第1张图片

Dubbo

dubbo作用
跨项目调用方法, 例如从A项目中的controller调用B项目中的service方法

dubbo同类型的技术都有哪些
rpc协议实现框架: dubbo, dubbox, springCloud

  • 优点: 传输效率快
  • 缺点: controller和service两个项目必须都是Java语言实现

webService技术: 代表框架有cxf
底层使用的协议叫做soap协议, 其实传输的就是xml数据

  • 优点: 可以跨语言跨平台, controller可以是Java语言实现, service可以是.net或者C语言等语言实现
  • 缺点: 传输效率低

dubbo用法

  • service实现类上写@Service注解, 注意这个@Service注解不是spring包的是阿里的dubbo包下的
  • controller注入的时候使用@Refrence注解来进行注入, 也是阿里dubbo包下面的.
  • 如果controller和service之间传输pojo实体类, 那么实体类必须实现Java的序列化接口

dubbo调用流程
JavaEE学习日志(一百一十一): 分布式项目基本构建(Dubbo和Zookeeper)_第2张图片

Zookeeper

在linux下安装zookeeper
JavaEE学习日志(一百一十一): 分布式项目基本构建(Dubbo和Zookeeper)_第3张图片
安装完成
在这里插入图片描述
将目录改成zookeeper
JavaEE学习日志(一百一十一): 分布式项目基本构建(Dubbo和Zookeeper)_第4张图片
查看zoo.cfg配置文件
JavaEE学习日志(一百一十一): 分布式项目基本构建(Dubbo和Zookeeper)_第5张图片
JavaEE学习日志(一百一十一): 分布式项目基本构建(Dubbo和Zookeeper)_第6张图片
启动zookeeper

zookeeper命令:
启动: ./zkServer.sh start
关闭: ./zkServer.sh stop
查看状态: ./zkServer.sh status
JavaEE学习日志(一百一十一): 分布式项目基本构建(Dubbo和Zookeeper)_第7张图片

dubbo入门

项目准备

首先创建两个项目
在这里插入图片描述
引入依赖

<dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jmsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
      <version>2.8.4version>
    dependency>
    <dependency>
      <groupId>org.apache.zookeepergroupId>
      <artifactId>zookeeperartifactId>
      <version>3.4.6version>
    dependency>
    <dependency>
      <groupId>com.github.sgroschupfgroupId>
      <artifactId>zkclientartifactId>
      <version>0.1version>
    dependency>
    <dependency>
      <groupId>javassistgroupId>
      <artifactId>javassistartifactId>
      <version>3.11.0.GAversion>
    dependency>
  dependencies>

再配置两个tomcat,名字与两个项目相同

服务提供者web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext*.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
web-app>

服务提供者dubbo配置文件

创建applicationContext-dao.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.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">
    
    <dubbo:application name="dubboxdemo-service"/>
    
    <dubbo:registry address="zookeeper://192.168.200.128:2181"/>
    
    <dubbo:annotation package="cn.itcast.service" />
beans>

服务消费者web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
  
  <filter>
    <filter-name>CharacterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
    <init-param>
      <param-name>forceEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:applicationContext-web.xmlparam-value>
    init-param>
  servlet>
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.dourl-pattern>
  servlet-mapping>
web-app>

服务消费者dubbo配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.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">

    
    <mvc:annotation-driven >
        
        <mvc:message-converters register-defaults="false">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            bean>
        mvc:message-converters>
    mvc:annotation-driven>


    
    <dubbo:application name="dubboxdemo-web" />
    
    <dubbo:registry address="zookeeper://192.168.200.128:2181"/>
    
    <dubbo:annotation package="cn.itcast.controller" />
beans>

测试

TestController

package cn.itcast.controller;

import cn.itcast.service.TestService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController注解作用:相当于在类上加上@Controller注解,并且在类的所以方法上@ResponseBody注解
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Reference
    private TestService testService;

    @RequestMapping("/getName")
    public String getName(){
        String name = testService.getName();
        return name;
    }
}

TestService(接口省略)

package cn.itcast.service.impl;

import cn.itcast.service.TestService;
import com.alibaba.dubbo.config.annotation.Service;

@Service
public class TestServiceImpl implements TestService {

    @Override
    public String getName() {
        return "zhangsan";
    }
}

你可能感兴趣的:(JavaEE学习日志)