Project Structure:

  • API modules – definitions of interfaces, entities, and so on
  • Provider module — provider
  • Consumer module — consumer

 

Project Address:

Github address: github.com/Silentims/s…


 

I. Parent Project Dubbo:

1. Create a SpringBoot initialization project in IDEA

 

2. Name the project using JDK1.8

 

3. Select the SpringBoot version:

 

4. Delete unnecessary files and directories

 

5. Edit the POM. XML file, add the package mode as POM, add the sub-module reference (create one by one in the following steps), and complete the XML file as follows

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.10. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>dubbo-boot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo-boot</name> <description>Spring boot dubbo </description> <! Pom --> <packaging> POm </packaging> <properties> <java.version>1.8</java.version> </properties> <! -- Submodule reference --> <modules> <module> dubo-api </module> <module> dubo-consumer </module> <module> dubo-provider </module> </modules> <dependencies> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> <? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.10. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> <name> dubo-api </name> <description>Demo project for Spring Boot</description> <! -- Packing method: Jar --> <packaging>jar</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> </dependencies> <build> <plugins>  <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

2. API module

1. Create the submodule (create the process as in step 1)

 

2. Delete redundant files and directories, also delete the main startup class, this module mainly defines interface, entity, public resources, here demonstrates integration of Dubbo demo interface, code directory structure is as follows

 

3. Modify the POM. XML file

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.10. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> <name> dubo-api </name> <description>Demo project for Spring Boot</description> <! -- Packing method: Jar --> <packaging>jar</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> </dependencies> <build> <plugins>  <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

4. The interface code is as follows:

package com.example.dubbo.api.manager; Public interface DemoApi {/** * demo method * @param name * @return */  String sayHello(String name); }Copy the code

5. Complete API module, code structure is as follows:

 

Provider module

1. Create the submodule Dubo-provider, and delete unnecessary files as follows:

 

 

2. Modify the POM. XML file and complete the following configuration (the annotated part indicates the newly added dependency) :

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.10. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name> Dubo-provider </name> <description>Demo project for Spring Boot</description> <! -- Packing method: Jar --> <packaging>jar</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <! Dubbo Provider fails to start because spring-boot-starter-web dependency is not added. Has been missing in the startup log shows "O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat initialized with the port (s) : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! -- This is the defined interface package, <dependency> <groupId>com.example</groupId> <artifactId> dubo-api </artifactId> The < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > <! > <dependency> <groupId>com.alibaba.boot</groupId> <artifactId> dubo-spring-boot-starter </artifactId> < version > 0.2.0 < / version > < / dependency > < the 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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

3. Modify the Application configuration file (here I changed it to yML file format)

Server: port: 8087 Dubbo: application: # This name is displayed as the provider. Dubbo-springboot-provider # App owner: Jessy Heung # App owner organization: Jessy Heung Registry: ID: Zookeeper # registry # protocol: zookeeper # registry address: Zookeeper: / / 127.0.0.1:2181 # # dubbo protocol on port 20880 exposed service agreement protocol name: name: dubbo # protocol port port: 20880 # protocol access log accesslog: Dubo-access. log Number of retries Provider: retries: 0 # Timeout: 3000 # Register the monitoring center monitor: protocol: registryCopy the code

4. Write service implementation code, create a service implementation class, the specific code is as follows:

  • Note that the @service annotation is under dubbo
package com.example.dubbo.provider.impl; import com.alibaba.dubbo.config.annotation.Service; import com.example.dubbo.api.manager.DemoApi; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Service public class DemoApiImpl implements DemoApi { public static final Logger logger = LoggerFactory.getLogger(DemoApiImpl.class); @override public String sayHello(String name) {logger.info(" [provider] [service implementation] -- request parameter name: {}",name); Return "hello"+ name +"(Springboot with Dubbo -- annotated) "; }}Copy the code

5. In the main startup class, start with Dubbo

package com.example.dubbo.provider; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; Dubbo public class DubboProviderApplication {public static void main(String[]) args) { SpringApplication.run(DubboProviderApplication.class, args); }}Copy the code

6. Complete file directory structure:

 

 

4. Consumer module

Create submodule dubo-consumer (the process is the same as provider, but the module name is different)

2. Modify pom.xml (same as provider)

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.10. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name> Dubo-consumer </name> <description>Demo project for Spring Boot</description> <! -- Packing method: Jar --> <packaging>jar</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <! -- Dubbo consumer fails to start because there is no spring-boot-starter-web dependency Has been missing in the startup log shows "O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat initialized with the port (s) : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! -- This is the defined interface package, <dependency> <groupId>com.example</groupId> <artifactId> dubo-api </artifactId> The < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > <! > <dependency> <groupId>com.alibaba.boot</groupId> <artifactId> dubo-spring-boot-starter </artifactId> <version>0.2.0</version> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

3. Modify the application configuration file (in YML format), including the port number and application name

Server: port: 8088 dubbo: application: # dubbo admin user name: dubbo-springboot-consumer # owner: Organization: Jessy Heung Registry: id: zookeeper-registry protocol: Zookeeper # registry address For local zk registered address: here they are: / / 127.0.0.1:2181 # dubbo agreement exposed services at port 20880 # name protocol: name: Dubbo protocol port: 20880 protocol accesslog Accesslog: dubo-access. log Provider: retries: 0 Monitor: Protocol: registryCopy the code

4. Service layer code implementation

package com.example.dubbo.consumer.service; import com.alibaba.dubbo.config.annotation.Reference; import com.example.dubbo.api.manager.DemoApi; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; Public class DemoService {private} public class DemoService {private} public class DemoService {private static final Logger logger = LoggerFactory.getLogger(DemoService.class); Private DemoApi DemoApi; Public String sayHello(String name){logger.info(" [consumer] [service layer] -- request parameter name: {}",name); return demoApi.sayHello(name); }}Copy the code

5. Control layer code implementation

package com.example.dubbo.consumer.controller; import com.example.dubbo.consumer.service.DemoService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @auth Jessy Heung @date 2020-09-25 */ @restController @requestMapping ("/demo") public class DemoController { private static Logger logger = LoggerFactory.getLogger(DemoController.class); @Autowired private DemoService demoService; /** * Open a browser, Enter /demo/sayHello * @return */ @requestMapping (value = "/sayHello", Method = requestmethod.get) @responseBody public String sayHello(){logger.info(" [consumer] [controller] -- request entry "); return demoService.sayHello("Jessy"); }}Copy the code

6. In the main startup class, start with Dubbo

package com.example.dubbo.consumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }}
Copy the code

7. Complete file directory structure

 

 

Build and deploy Dubbo admin

1. Download Git clone to a local official project, the official project at https://github.com/apache/dubbo-admin

git clone https://github.com/apache/dubbo-admin.git
Copy the code

2. Open the readme_en. md file in the project and deploy it as instructed

3. Enter localhost://8080 in the browser. The deployment is successful if the following page is displayed

 

 

Six, start the project, test

1. Start Provider and then Consumer:

 

 

2. The access addresses localhost: 8088 / demo/sayHello

 

Check dubbo admin for service calls

1. View service information

 

 

2. View the service invocation relationship

 

 

At this point, Springboot integration with Dubbo is complete with a simple demo with annotations.