Dubbo
【黑马程序员Dubbo快速入门,Java分布式框架dubbo教程】
3 Dubbo 快速入门
文章目录
- Dubbo
- 3 Dubbo 快速入门
- 3.2 Dubbo 快速入门
- 3.2.1 Spring和 SpringMvc 整合
3.2 Dubbo 快速入门
3.2.1 Spring和 SpringMvc 整合
依然拿着 架构图来比着搞
【实现步骤】
①创建服务提供者Provider模块
②创建服务消费者Consumer模块
③在服务提供者模块编写 UserServiceImpl 提供服务
④在服务消费者中的 UserController 远程调用UserServiceImpl 提供的服务
⑤分别启动两个服务,测试
【直接非常 快速的开干】
先来一个空的项目
直接创建
现在的IDEA 版本 默认 会给我们搞成 一个模块 出来
我们不要
直接干掉
OK, 干净 了
现在再来创建 模块
OK,直接创建
OK,这是第一个模块
再来一个
直接创建
OK,这样两个 模块都有 了
【配置依赖】
dubbo-web
<?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.dingjiaxiong</groupId>
<artifactId>dubbo-web</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
<dubbo.version>2.7.4.1</dubbo.version>
<zookeeper.version>4.0.0</zookeeper.version>
</properties>
<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之后统一为rg.apache.dubb -->
<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>
</dependencies>
<build>
<plugins>
<!--tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8000</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
dubbo-service
<?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.dingjiaxiong</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
<dubbo.version>2.7.4.1</dubbo.version>
<zookeeper.version>4.0.0</zookeeper.version>
</properties>
<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之后统一为rg.apache.dubb -->
<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>
</dependencies>
</project>
这边,就不用Tomcat 插件,因为要打成 jar,不用运行成web 【记得刷一下】
OK,现在编写业务接口【在dubbo-service 中】
package com.dingjiaxiong.service;
/**
* ClassName: UserService
* date: 2022/11/13 20:26
*
* @author DingJiaxiong
*/
public interface UserService {
public String sayHello();
}
OK,再来一个 实现类
package com.dingjiaxiong.service.impl;
import com.dingjiaxiong.service.UserService;
import org.springframework.stereotype.Service;
/**
* ClassName: UserServiceImpl
* date: 2022/11/13 20:27
*
* @author DingJiaxiong
*/
@Service
public class UserServiceImpl implements UserService {
public String sayHello() {
return "Hello , Dubbo!";
}
}
OK
创建Spring 配置文件【因为 要扫描bean 】
直接copy 老师给的
复制到 resources 下
哇, 一些快要忘记 的东西,浮现脑海
修改配置文件,包扫描
<?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.dingjiaxiong.service"/>
</beans>
OK, 这样就完成 了Spring 的配置
【现在来 做dubbo-web】
首先是打包方式 → war
OK
创建web 工程那套 目录
OK,web.xml 直接复制老师给的资料了
OK,先解决第一个报红
使 web 模块依赖于 service 模块
OK
第一个解决
springmvc 的配置文件 笔者就直接复制 老师的了
OK, 问题解决
修改springmvc 的配置文件
<?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.dingjiaxiong.controller"/>
</beans>
OK
编写 控制器
package com.dingjiaxiong.controller;
import com.dingjiaxiong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* ClassName: UserController
* date: 2022/11/13 20:39
*
* @author DingJiaxiong
*/
@RestController
@RequestMapping("/user")
public class UserController {
//注入 Service
@Autowired
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
return userService.sayHello();
}
}
OK,这样 基本环境就算 准备好了
现在 先把service 模块 install 一下
OK, 启动web 项目
OK,端口 8000 已经跑起来了
浏览器 访问测试 http://localhost:8000/user/sayHello.do
OK,没毛病
现在是一种 本地调用的模块,OK,之后我们 就会加入 Dubbo