共计 7803 个字符,预计需要花费 20 分钟才能阅读完成。
前言
前两天在和粉丝聊天的时候,粉丝跟我说之前在面试的时候被问到 SpringBoot 这一块的常识被问的有点懵,和我问了不少这方面的货色。预先我想了想不如把这些货色分享进去吧,让更多的人看到,这样不论是对本人常识的一个晋升还是对面试的筹备都有益处。好了,满满的干货都在上面了!
1. 创立 maven 我的项目父工程(不应用 idea 的 spring Initializr)
不勾选任何模板,间接应用默认我的项目模板
删除 src 文件夹,创立这个我的项目的目标是为了作为父工程,无需 src 文件夹,只保留 pom 文件即可
编辑 pom 文件
设置父工程打包形式为 pom,用于依赖治理
<packaging>pom</packaging>
增加必要依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- dubbo 依赖 apache 版 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<!-- dubbo 所需其余依赖 应用 alibaba 的 dubbo 则不须要 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<!-- dubbo 所需其余依赖 应用 alibaba 的 dubbo 则不须要 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<!-- zookeeper 依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
</dependency>
</dependencies>
</dependencyManagement>
2. 创立生产者
同第一步雷同,创立子模块,名为 provider
删除 src 文件夹,该模块仍作为父工程应用
批改 pom 文件,继承父工程,并设置打包形式为 pom
<parent>
<groupId>online.hupeng.dubbo</groupId>
<artifactId>base</artifactId>
<version>1.0</version>
</parent>
<artifactId>provider</artifactId>
<packaging>pom</packaging>
创立子工程 provider-api,
批改 pom 文件,继承父工程 provider
这个工程为生产者和束缚者束缚 api 标准,生产者和消费者都需依赖此模块分割彼此
此模块中只写 api 和实体类,不写实现形式
<parent>
<groupId>online.hupeng.dubbo</groupId>
<artifactId>provider</artifactId>
<version>1.0</version>
</parent>
<artifactId>provider-api</artifactId>
<version>1.0</version>
<!-- maven 默认打包形式为 jar 包,此处可不必显示指定 -->
<packaging>jar</packaging>
编写代码
实体类 user
package online.hupeng.dubbo.provider.domain;
import java.io.Serializable;
/*
当实体类作为 RPC 办法的返回值时,必须实现 Serializable 接口,dubbo 的实现原理就是
消费者近程调用生产者办法,生产者返回序列化后的返回值,消费者通过网络获取到序
列化后的数据再反序列化
*/
/*
此处不实现 Serializable 接口,近程调用办法时会报错,并提醒实体类需继承此接口
*/
public class User implements Serializable {
private String account;
private String password;
public String getAccount() {return account;}
public void setAccount(String account) {this.account = account;}
public String getPassword() {return password;}
public void setPassword(String password) {this.password = password;}
}
UserService 接口
package online.hupeng.dubbo.provider.service;
import online.hupeng.dubbo.provider.domain.User;
public interface UserService {User getUserInstance();
}
执行 mvn install 命令将 jar 包打入本地仓库
结束
在 provider 下创立子工程 provider-service 模块(此模块为真正的生产者)
编辑 pom 文件继承父工程
<parent>
<groupId>online.hupeng.dubbo</groupId>
<artifactId>provider</artifactId>
<version>1.0</version>
</parent>
<artifactId>provider-service</artifactId>
<version>1.0</version>
<name>provider-service</name>
<properties>
<java.version>1.8</java.version>
</properties>
增加必须依赖
<dependencies>
<!-- 此处依赖 api 模块标准接口 -->
<dependency>
<groupId>online.hupeng.dubbo</groupId>
<artifactId>provider-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
UserService 的实现类 UserServiceImpl
package online.hupeng.dubbo.provider.service.impl;
import online.hupeng.dubbo.provider.domain.User;
import online.hupeng.dubbo.provider.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
/*
dubbo 注解,指定接口版本号和超时工夫,调用方需正确填写版本信息
*/
@DubboService(version = "1.0", timeout = 300)
public class UserServiceImpl implements UserService {
@Override
public User getUserInstance() {User user = new User();
user.setAccount("admin");
user.setPassword("admin");
return user;
}
}
application.yml 配置
dubbo:
application:
# 指定该服务名称
name: provider
registry:
# 通信协议
protocol: zookeeper
# 注册核心地址
address: 127.0.0.1
# 注册核心端口号
port: 2181
# 也能够不配置 protocol 和 port,间接配置为 zookeeper://127.0.0.1:2181
protocol:
name: dubbo
# 服务裸露端口
port: 8081
# 包扫描(此处为扫描 dubbo 的注解,和 SpringBoot 无关)
scan:
base-packages: online.hupeng.dubbo
为启动类增加 dubbo 注解 @EnableDubbo
package online.hupeng.dubbo.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ProviderServiceApplication {public static void main(String[] args) {SpringApplication.run(ProviderServiceApplication.class, args);
}
}
3. 创立消费者
在根我的项目下创立 consumer 模块
编辑 pom 文件继承父我的项目
<parent>
<artifactId>base</artifactId>
<groupId>online.hupeng.dubbo</groupId>
<version>1.0</version>
</parent>
<artifactId>consumer</artifactId>
<version>1.0</version>
增加必须依赖
<dependencies>
<!-- 引入 provider-api 依赖用以近程调用 -->
<dependency>
<groupId>online.hupeng.dubbo</groupId>
<artifactId>provider-api</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
编辑 controller 层代码近程调用
package online.hupeng.dubbo.consumer.controller;
import online.hupeng.dubbo.provider.domain.User;
import online.hupeng.dubbo.provider.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ConsumerController {
/*
近程调用注解,需正确对应生产者的版本号,此处不须要 @autowird 注入对象
*/
@DubboReference(version = "1.0")
private UserService userService;
@ResponseBody
@GetMapping("/test")
public User getUser() {return userService.getUserInstance();
}
}
配置 application.yml 文件
dubbo:
application:
name: provider
registry:
protocol: zookeeper
address: 127.0.0.1
port: 2181
protocol:
name: dubbo
# 服务裸露端口
port: 8081
# 包扫描
scan:
base-packages: online.hupeng.dubbo
#### 增加 SpringBoot 启动类
package online.hupeng.dubbo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);
}
}
## 4. 测试
按程序启动 zookeeper、provider-service、consumer
拜访 http://localhost/test
![](https://upload-images.jianshu.io/upload_images/23140115-86109ded3f85528b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
胜利!!!## 最初