Dubbo分布式框架入门实验

Dubbo框架

今天做了下阿里做的分布式框架Dubbo的实验
我理解的这个框架主要是将controller层和service层分开部署
分为消费者,注册服务器,提供者
面向接口
提供者提供接口的实现方法,并将其注册到zookeeper,消费者只需要引入接口的依赖,可以从zookeeper上远程获取实现类的方法
以下为简单的实验

建立interface工程

这是User类

package com.study.model;
import java.io.Serializable;
public class User implements Serializable {
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

这是UserService接口

package com.study.service;
import com.study.model.User;
public interface UserService {
    public User queryById(int id);
}

pom.xml文件
build标签中编译插件是为了将interface工程打成jag包并放入本地Maven仓库


<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.0modelVersion>
  <groupId>com.studyDubbogroupId>
  <artifactId>dubbo-interfaceartifactId>
  <version>1.0-SNAPSHOTversion>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
  properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <configuration>
          <source>1.8source>
          <target>1.8target>
          <encoding>UTF-8encoding>
        configuration>
      plugin>
    plugins>
  build>
project>

打包成jar包,package->install,查看本地库,成功安装jar包

提供者

pom.xml
提供者中引入了上面的interface模块,以模拟不同的服务器


<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.0modelVersion>
  <groupId>org.examplegroupId>
  <artifactId>dubbo-providerartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>
  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
  properties>
  <dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>5.3.1version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.3.1version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
      <version>2.6.2version>
    dependency>
    
    <dependency>
      <groupId>com.studyDubbogroupId>
      <artifactId>dubbo-interfaceartifactId>
      <version>1.0-SNAPSHOTversion>
    dependency>
    
    <dependency>
      <groupId>org.apache.curatorgroupId>
      <artifactId>curator-frameworkartifactId>
      <version>4.1.0version>
    dependency>
  dependencies>
  project>

UserServiceImpl类,实现了interface包中的UserService接口

此外,UserService可以有很多个实现类,只需要在dubbo配置文件中为每个service接口指定版本,消费者就可以根据版本号调用一个接口的不同的实现类

package com.study.service;
import com.study.model.User;
public class UserServiceImpl implements UserService{
    @Override
    public User queryById(int i) {
        User user=new User();
        user.setId(i);
        user.setName("user_"+i);
        return user;
    }
}

dubbo配置 dubbo-provider.xml


<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="dubbo-service" />
    
    <dubbo:protocol name="dubbo" port="20880" />
    
    <dubbo:registry address="zookeeper://localhost:2181" />
    
    <dubbo:service interface="com.study.service.UserService" ref="userServiceImpl" />
    
    <bean id="userServiceImpl" class="com.study.service.UserServiceImpl"/>
beans>

web.xml配置,只需设置监听器

DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:dubbo-provider.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
web-app>

消费者

UserController类
这里用Resource自动装配(所以引入了javax.annotation-api),用autowired会失败,具体原因还有待深究

package com.study.controller;
import com.study.model.User;
import com.study.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/userDetail")
    public String userDetail(Model model,Integer id){
        User user1 = userService.queryById(id);
        model.addAttribute("user1",user1);
        return "userDetail";
    }
}

applicationContext.xml
这里没什么说的


<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="com.study.controller"/>
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    bean>
beans>

dubbo配置文件dubbo-consumer.xml
可以根据dubbo:reference里的version同时配置一个接口的多个实现类


<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="dubbo-consumer"/>
    
    <dubbo:registry address="zookeeper://localhost:2181"/>
    
    <dubbo:reference id="userService" interface="com.study.service.UserService"/>
beans>

web.xml
这里无意间发现有个注意的点
“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
“http://java.sun.com/dtd/web-app_2_3.dtd” >
Servlet2.3/jsp1.2不支持el表达式,然后导致jsp里面没有读出el表达式
解决方案,换个高点的servlet版本即可
或者在jsp页面声明时<%@ page contentType=“text/html;charset=UTF-8” language=“java”
isELIgnored=“false” %>
加入isELIgnored=“false”

DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:dubbo-consumer.xml,classpath:applicationContext.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:applicationContext.xmlparam-value>
    init-param>
  servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

pom.xml


<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.0modelVersion>
  <groupId>org.study.interfacegroupId>
  <artifactId>dubbo-consumerartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>
  <dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>5.3.1version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.3.1version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
      <version>2.6.2version>
    dependency>
    
    <dependency>
      <groupId>com.studyDubbogroupId>
      <artifactId>dubbo-interfaceartifactId>
      <version>1.0-SNAPSHOTversion>
    dependency>
    
    <dependency>
      <groupId>org.apache.curatorgroupId>
      <artifactId>curator-frameworkartifactId>
      <version>4.1.0version>
    dependency>
    
    <dependency>
      <groupId>javax.annotationgroupId>
      <artifactId>javax.annotation-apiartifactId>
      <version>1.3.2version>
    dependency>
  dependencies>
project>

userDetail.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<div>
    id:${user1.id}
    name:${user1.name}
div>
body>
html>

最后进行测试。先启动zookeeper注册中心,再启动提供者,最后启动消费者,由于我是在一台电脑上面部署运行的,所以提供者和消费者的tomcat服务器端口号记得设置成不同的。最后浏览器访问消费者的url,成功显示了user1的信息。

你可能感兴趣的:(后端学习笔记,dubbo,分布式,java)