又陷入知識盲區了,面試被問SpringBoot集成dubbo,我當時就懵了

前言

前兩天在和粉絲聊天的時候,粉絲跟我說以前在面試的時候被問到SpringBoot這一塊的知識被問的有點懵,和我問了很多這方面的東西。過後我想了想不如把這些東西分享出來吧,讓更多的人看到,這樣無論是對本身知識的一個提高仍是對面試的準備都有好處。好了,滿滿的乾貨都在下面了!java

1.建立maven項目父工程(不使用idea的spring Initializr)

不勾選任何模板,直接使用默認項目模板 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>
複製代碼

2. 建立生產者

同第一步相同,建立子模塊,名爲providerbash

刪除src文件夾,該模塊仍做爲父工程使用網絡

修改pom文件,繼承父工程,並設置打包方式爲pomapp

<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和實體類,不寫實現方式dom

<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

成功!!!

最後

你們看完有什麼不懂的能夠在下方留言討論,也能夠關注我私信問我,我看到後都會回答的。也歡迎你們關注個人公衆號:前程有光,金三銀四跳槽面試季,整理了1000多道將近500多頁pdf文檔的Java面試題資料,文章都會在裏面更新,整理的資料也會放在裏面。謝謝你的觀看,以爲文章對你有幫助的話記得關注我點個贊支持一下!

相關文章
相關標籤/搜索