preface

When CHATTING with my fans two days ago, my fans told me that they were a little confused when asked about SpringBoot in the interview, and asked me a lot about it. Later, I thought it would be better to share these things, so that more people can see them, which is good for both my knowledge and preparation for the interview. All right, there’s a lot of dry stuff down there!

1. Create a Maven project parent (without Using Spring Initializr of IDEA)

Uncheck any templates and use the default project template directly

Delete the SRC folder. This project was created to be a parent project, without the SRC folder, just keep the POM files

Edit poM files

Set the parent project packaging mode to POM for dependency management

	<packaging>pom</packaging>
Copy the code

Add necessary dependencies

<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> <! <dependency> <groupId>org.apache.dubbo</groupId> < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 2.7.7 < / version > < / dependency > <! --> <dependency> <groupId> org.apache.toth </groupId> < artifactId > curator - framework < / artifactId > < version > 4.0.1 < / version > < / dependency > <! --> <dependency> <groupId> org.apache.toth </groupId> < artifactId > curator - recipes < / artifactId > < version > 2.8.0 < / version > < / dependency > <! --> <dependency> <groupId>com.101tec</groupId> <artifactId> zkClient </artifactId> <version>0.7</version> </dependency> </dependencies> </dependencyManagement>Copy the code

2. Create producers

As in the first step, create a submodule called Provider

Delete the SRC folder. The module is still used as the parent project

Modify the POM file, inherit the parent project, and set the packaging mode to POM

Dubbo </groupId> <artifactId>base</artifactId> <version>1.0</version> </parent> <artifactId>provider</artifactId> <packaging>pom</packaging>Copy the code

Create the subproject provider-API,

Modify the POM file, inherit the parent project provider. This project constrains API specifications for producers and constraints. Producers and consumers rely on this module to communicate with each other

<parent> <groupId>online.hupeng.dubbo</groupId> <artifactId>provider</artifactId> <version>1.0</version> </parent> < < artifactId > provider - API/artifactId > < version > 1.0 < / version > <! --> <packaging>jar</packaging>Copy the code

Write the code

Entity class user

package online.hupeng.dubbo.provider.domain; import java.io.Serializable; /* The Serializable interface must be implemented when an entity class is returned by an RPC method. Dubbo is implemented when a consumer calls a producer method remotely and the producer returns the serialized value. */ * The Serializable interface is not implemented here, and an error will be reported when the method is called remotely. */ public class User implements Serializable {private String account; private String password; public StringgetAccount() {
		return account;
	}

	public void setAccount(String account) {
	this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) { this.password = password; }}Copy the code

UserService interface

	package online.hupeng.dubbo.provider.service;

	import online.hupeng.dubbo.provider.domain.User;

	public interface UserService {

		User getUserInstance();
	}
Copy the code

Run the MVN install command to dump the JAR package into the local repository. End Create a sub-project under provider (this module is the real producer).

Edit POM file to inherit parent project

<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 >Copy the code

Add must depend on

<dependencies> <! <dependency> <groupId>online.hupeng.dubbo</groupId> <artifactId>provider-api</artifactId> < version > 1.0 < / version > < / dependency > < the 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>
Copy the code

The implementation class for 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 annotation, specify the interface version number and timeout period, the caller must fill in the correct version information */ @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");
	returnuser; }}Copy the code

Application. Yml configuration

dubbo:
	application:
		# specify the service name
		name: provider
	registry:
		# Communication protocol
		protocol: zookeeper
		Registry addressAddress: 127.0.0.1# registry port number
		port: 2181
		# can not configuration protocol and port, direct configuration for the zookeeper: / / 127.0.0.1:2181

	protocol:
	name: dubbo
	Service exposed port
	port: 8081

# SpringBoot package scan (dubbo)
	scan:
	base-packages: online.hupeng.dubbo
Copy the code

Add the dubbo annotation @enableDubbo to the startup class

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); }}Copy the code

3. Create consumers

Create the Consumer module under the root project to edit the POM file and inherit the parent project

<parent> <artifactId>base</artifactId> <groupId>online.hupeng.dubbo</groupId> <version>1.0</version> </parent> The < artifactId > consumer < / artifactId > < version > 1.0 < / version >Copy the code

Add must depend on

<dependencies> <! > <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>
Copy the code

Edit controller layer code remote call

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 {/* Remote call annotation, the correct version of the producer, there is no need for @autowird injection object */ @dubboReference (version ="1.0")
	private UserService userService;

	@ResponseBody
	@GetMapping("/test")
	public User getUser() {
    	returnuserService.getUserInstance(); }}Copy the code

Configure the application.yml file

Dubbo: Application: name: Provider Registry: protocol: Zookeeper Address: 127.0.0.1 Port: 2181 Protocol: name: dubboService exposed port
		port: 8081

	# package scanning
	scan:
		base-packages: online.hupeng.dubbo
	```
Copy the code

Add the SpringBoot boot class

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); }}Copy the code

4. Test

Enable ZooKeeper, provider-service, and consumer in sequence to access http://localhost/test

Success!!

The last

You can leave a comment below to discuss what you don’t understand. You can also pay attention to my private letter and ask me. I will answer it after I see it. Also welcome everyone to pay attention to my official account: bright future, Golden Three silver four job-hunting interview season, sorted out more than 1000 nearly 500 pages of PDF documents of Java interview questions, articles will be updated in it, sorted out the information will be placed in it. Thank you for watching, think the article is helpful to you remember to pay attention to me a thumb-up support!