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仓库

<?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>com.studyDubbo</groupId>
  <artifactId>dubbo-interface</artifactId>
  <version>1.0-SNAPSHOT</version>
<!--  <packaging>war</packaging>-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

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

提供者

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

<?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>org.example</groupId>
  <artifactId>dubbo-provider</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.1</version>
    </dependency>
    <!--Dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>
    <!--引入interface工程-->
    <dependency>
      <groupId>com.studyDubbo</groupId>
      <artifactId>dubbo-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--zookeeper依赖-->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.1.0</version>
    </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

<?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服务提供者的名称,保证唯一性-->
    <dubbo:application name="dubbo-service" />
    <!--声明dubbo使用的协议名称和端口号-->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用zookeeper注册中心 指定注册中心的地址和端口号-->
    <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>contextConfigLocation</param-name>
    <param-value>classpath:dubbo-provider.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-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
这里没什么说的

<?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="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同时配置一个接口的多个实现类

<?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服务消费者名称-->
    <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>contextConfigLocation</param-name>
    <param-value>classpath:dubbo-consumer.xml,classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <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:applicationContext.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

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>org.study.interface</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.1</version>
    </dependency>
    <!--Dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>
    <!--引入interface工程-->
    <dependency>
      <groupId>com.studyDubbo</groupId>
      <artifactId>dubbo-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--zookeeper依赖-->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.1.0</version>
    </dependency>
    <!--resource注解-->
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.3.2</version>
    </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>Title</title>
</head>
<body>
<div>
    id:${user1.id}
    name:${user1.name}
</div>
</body>
</html>

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