🎄Apache Dubbo第一个入门案例
🍭整体概述
第一个入门案例将创建一个Maven项目 父项目中包含4个模块 依次为:
(1)dubbo-pojo模块,负责提供公用的实体类
(2)dubbo-interface模块,负责提供项目中需要使用到的接口 依赖dubbo-pojo模块
(3)dubbo-service模块,负责提供后端服务 依赖dubbo-interface模块
(4)dubbo-web模块,运行web应用
🍭dubbo-pojo模块
模块当中只有一个User实体类
public class User implements Serializable {
private int id;
private String username;
private String password;
...
}
dubbo-pojo模块使用之前必须先用Maven命令进行
install
或者直接点击界面提供的install按钮
🍭dubbo-interface模块
interface模块定义项目中要使用的接口
public interface UserService {
/**
* 测试接口1
*/
String helloWorld();
/**
* 测试接口2 查询用户
*/
User findUserById(int id);
}
dubbo-interface需要引入的依赖如下(只需要引入dubbo-pojo模块)
<dependencies>
<dependency>
<groupId>com.bigbigmeng</groupId>
<artifactId>dubbo-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
🍭dubbo-service模块
🌴配置依赖
dubbo-service模块是真正提供远程可调用服务的模块 它需要引入的依赖如下
<dependencies>
<!-- servlet3.0规范的坐标 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--spring的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--springmvc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<!-- Dubbo的起步依赖 版本2.7之后统一为 org.apache.dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- ZooKeeper客户端实现 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<!--ZooKeeper客户端实现 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<!--依赖公共的接口模块 -> dubbo-interface -> dubbo-pojo -->
<dependency>
<groupId>com.bigbigmeng</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- tomcat插件 启动远程接口的服务 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>9000</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
根据pom.xml中配置的Tomcat插件的配置 9000端口 service模块启动在9000端口
🌴提供的服务
主要提供的service服务 -> 一个
UserServiceImpl
类 实现了dubbo-interface
模块提供的UserService
接口
@Service // 将这个类提供的方法(服务)对外发布 将访问的地址 ip 端口 路径 注册到注册中心中
public class UserServiceImpl implements UserService {
public String helloWorld() {
return "hello BigBigMeng~ Welcome to use Dubbo!";
}
public User findUserById(int id) {
//查询User对象
User user = new User(1,"zhangsan","123");
return user;
}
}
🌴web.xml配置🎯Spring
web.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- spring配置 -> 上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
applicationContext.xml 如下 其中配置了注册中心🎯zookeeper 其IP和端口为
node1:2181
为本地启动的zookeeper 后面测试运行前会说明
<?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://dubbo.apache.org/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--<context:component-scan base-package="com.bigbigmengeng.service" />-->
<!--<dubbo:protocol port="20880" />-->
<!--dubbo的配置-->
<!-- 1 配置项目的名称 唯一 -->
<dubbo:application name="dubbo-service"/>
<!-- 2 🎯配置注册中心的地址 -->
<dubbo:registry address="zookeeper://node1:2181"/>
<!-- 3 配置dubbo包扫描 -->
<dubbo:annotation package="com.bigbigmeng.service.impl" />
<!-- 元数据配置 -->
<dubbo:metadata-report address="zookeeper://node1:2181" />
</beans>
🍭dubbo-web模块
🌴提供的服务
package com.bigbigmeng.controller;
import com.bigbigmeng.pojo.User;
import com.bigbigmeng.service.UserService;
import org.apache.dubbo.config.annotation.Reference; // 🎯远程注入
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
//注入Service
//@Autowired // 以前 我们使用本地注入的方式进行注入
/*
1. 从zookeeper注册中心获取userService的访问url
2. 进行远程调用RPC
3. 将结果封装为一个代理对象。给变量赋值
*/
@Reference // 🎯远程注入
private UserService userService;
@RequestMapping("/helloWorld")
public String helloWorld(){
return userService.helloWorld();
}
/**
* 根据id查询用户信息
* @param id
* @return
*/
@RequestMapping("/findUserById")
public User findUserById(int id){
return userService.findUserById(id);
}
}
🌴web配置Servlet(🎯SpringMVC)
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 🎯Springmvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 通过参数contextConfigLocation加载 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 🎯访问链接的时候要以 .do 结尾才可以生效 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
🌴springmvc.xml配置如下
其中配置了注册中心🎯zookeeper 其IP和端口为
node1:2181
为本地启动的zookeeper 后面测试运行前会说明
<?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://dubbo.apache.org/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.bigbigmeng.controller"/>
<!--dubbo的配置-->
<!-- 1 配置项目的名称 唯一 -->
<dubbo:application name="dubbo-web" >
<dubbo:parameter key="qos.port" value="33333"/>
</dubbo:application>
<!-- 2 🎯配置注册中心的地址 -->
<dubbo:registry address="zookeeper://node1:2181"/>
<!-- 3 配置dubbo包扫描 -->
<dubbo:annotation package="com.bigbigmeng.controller" />
</beans>
⚡启动测试
关于zookeeper
安装zookeeper见 -> Linux软件安装 我在本地CentOS虚拟机上安装 并启动 设置虚拟机主机名为node1
本地启动
启动项目进行测试
启动dubbo-service模块
启动dubbo-web模块
浏览器访问
到此 一个简单的Dubbo的使用Demo就完成啦~ very nice✨