前兩天在和粉絲聊天的時候,粉絲跟我說以前在面試的時候被問到SpringBoot這一塊的知識被問的有點懵,和我問了很多這方面的東西。過後我想了想不如把這些東西分享出來吧,讓更多的人看到,這樣無論是對本身知識的一個提高仍是對面試的準備都有好處。好了,滿滿的乾貨都在下面了!java
不勾選任何模板,直接使用默認項目模板
web
刪除src文件夾,建立這個項目的目的是爲了做爲父工程,無需src文件夾,只保留pom文件便可面試
編輯pom文件spring
設置父工程打包方式爲pom,用於依賴管理apache
<packaging>pom</packaging>
添加必要依賴api
<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>
同第一步相同,建立子模塊,名爲provider網絡
刪除src文件夾,該模塊仍做爲父工程使用app
修改pom文件,繼承父工程,並設置打包方式爲pomdom
<parent> <groupId>online.hupeng.dubbo</groupId> <artifactId>base</artifactId> <version>1.0</version> </parent> <artifactId>provider</artifactId> <packaging>pom</packaging>
修改pom文件,繼承父工程provider
這個工程爲生產者和約束者約束api規範,生產者和消費者都需依賴此模塊聯繫彼此
此模塊中只寫api和實體類,不寫實現方式maven
<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; } }
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>
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; } }
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
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); } }
在根項目下建立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>
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(); } }
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) 成功!!! ## 最後 感謝你看到這裏,看完有什麼的不懂的能夠在評論區問我,以爲文章對你有幫助的話記得給我點個贊,天天都會分享java相關技術文章或行業資訊,歡迎你們關注和轉發文章!