SpringBoot 2.1.4 Integration Dubbo [ultra-detailed & with source code]

table of Contents

First, version and description

Second, the project structure Introduction

Third, the integration start

1. Create springboot-dubbo-paren parent project (pom)

2. Create springboot-dubbo-public-api-service public Api Interface project (pom)

3. Create springboot-dubbo-public-api-member-service Member Services Interface Engineering (jar)

4, create springboot-dubbo-api-member-service-impl Member realization of the project (jar)

5, create springboot-dubbo-order-web orders for engineering (jar)

Fourth, start the project

V. package

Sixth, source code download

First, version and description
JDK: 1.8

SpringBoot:2.1.4.RELEASE

ZooKeeper:3.4.14 

Dubbo:0.2.0(dubbo-spring-boot-starter)

ZooKeeper Download: https: //mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.14.tar.gz

Second, the project structure Introduction


Third, the integration start
1. Create springboot-dubbo-paren parent project (POM)
1, created after the completion of the src directory delete

2, pom-dependent parent project as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>
 
    <groupId>com.slming</groupId>
    <artifactId>springboot-dubbo-parent</artifactId>
    <packaging>pom</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <modules>
        <module>springboot-dubbo-public-api-service</module>
        <module>springboot-dubbo-api-member-service-impl</module>
        <module>springboot-dubbo-order-web</module>
    </modules>
 
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.Encoding>UTF-8</project.reporting.Encoding>
    </properties>
 
    <dependencies>
        <!-- Dubbo -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
 
        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <type>pom</type>
        </dependency>
 
        <!-- Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId> springboot-Maven-plugin </ artifactId>
            </ plugin>
        </ plugins>
    </ Build>
</ Project>
2, create springboot-dubbo-public-api- service public Api Interface project (pom)
Note : sub-projects under the parent project engineering (springboot-dubbo-paren) ( Module)

1. Once created deleted src directory

2, the interface of public works Api pom-dependent as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
    <!-- 继承自父工程 -->
    <parent>
        <artifactId>springboot-dubbo-parent</artifactId>
        <groupId>com.slming</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <packaging>pom</packaging>
    
    <modules>
        <module>springboot-dubbo-public-api-member-service</module>
    </modules>
 
    <artifactId> springboot-Dubbo-public-API-Service </ artifactId>
 
</ Project>
3, create springboot-dubbo-public-api- member-service Member Services Interface Engineering (jar)
Note: This project is a public Api Interface Engineering (springboot-dubbo-public-api -service) sub-project, right-click interfaces Api public works select "New" ---> "Module" 

1, Member Services Interface Engineering pom-dependent as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
    <!-- 父工程为公共API接口工程 -->
    <parent>
        <artifactId>springboot-dubbo-public-api-service</artifactId>
        <groupId>com.slming</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
 
    <artifactId>-Dubbo-public-springboot API-Member-Service </ artifactId> 2, create a membership service interface: com.slming.api.memeber.IMemberService
 
</ Project>

com.slming.api.memeber Package;
 
/ **
 * @author itslm
 * /
public interface IMemberService {
 
    / **
     * Get Member
     * a
     * <P> the Author: Leiming She </ P>
     * <P> a Date : 2019-04-29 10:05:36 </ P>
     *
     * @param username username
     * @return java.lang.String
     * /
    String GetMember (String username);
}
. 4, creating springboot-dubbo-api-member -service-impl Member realization of the project (jar)
Note: the sub-project (Module) under the parent project engineering (springboot-dubbo-paren)

1, Member Services pom-dependent realization of the project are as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
    <!-- 继承自父工程 -->
    <parent>
        <artifactId>springboot-dubbo-parent</artifactId>
        <groupId>com.slming</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <modelVersion>4.0.0</modelVersion>
 
    <packaging>jar</packaging>
 
    <artifactId>springboot-dubbo-api-member-service-impl</artifactId>
 
    <dependencies>
        <dependency>
            <artifactId>springboot-dubbo-public-api-member-service</artifactId>
            <groupId>com.slming</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
 
</project>
2、创建会员服务实现类:com.slming.api.memeber.impl.MemberServiceImpl

Note: This class is @Service notes for the next com.alibaba.dubbo.config.annotation.Service package, do not guide the wrong bag oh ~

package com.slming.api.memeber.impl;
 
import com.alibaba.dubbo.config.annotation.Service;
import com.slming.api.memeber.IMemberService;
 
/**
 * @author itslm
 */
@Service
public class MemberServiceImpl implements IMemberService {
 
    /**
     * 获取会员
     * <br/>
     * <p>Author: Leiming She</p>
     * <p>Date: 2019-04-29 10:05:36</p>
     *
     * @param username 用户名
     * @return java.lang.String
     */
    @Override
    public String getMember(String username) {
        return "Dubbo:" + username;
    }
}
3、创建application.yml文件:

Server:
  Port: 8080
Dubbo:
  the Application:
    name: Member
  Protocol:
    Port: 20880
    name: Dubbo
  Registry:
    address: ZooKeeper: //127.0.0.1: 2181
4, to create applications startup class: com.slming.AppMemberApplication

Note: @EnableDubbo need to add annotations, support for open Dubbo

com.slming Package;
 
Import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
/ **
 * @author itslm
 * /
@SpringBootApplication
@EnableDubbo
public class AppMemberApplication {
 
    public static void main (String [] args) {
        SpringApplication.run (AppMemberApplication.class, args);
    }
}
. 5, creating springboot-dubbo-order-web line Engineering (JAR)
Note: sub-project (Module) under the parent project engineering (springboot-dubbo-paren)

1, pom-dependent engineering orders as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
    <parent>
        <artifactId>springboot-dubbo-parent</artifactId>
        <groupId>com.slming</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <modelVersion>4.0.0</modelVersion>
 
    <packaging>jar</packaging>
 
    <artifactId>springboot-dubbo-order-web</artifactId>
 
    <dependencies>
        <dependency>
            <artifactId>springboot-dubbo-public-api-member-service</artifactId>
            <groupId>com.slming</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
2、创建订单访问类:com.slming.order.controller.OrderController

Note: no longer used here @Autoware be injected,

           Injection was used in com.alibaba.dubbo.config.annotation.Reference packet @Reference

package com.slming.order.controller;
 
import com.alibaba.dubbo.config.annotation.Reference;
import com.slming.api.memeber.IMemberService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author itslm
 */
@RestController
public class OrderController {
 
    @Reference
    private IMemberService memberService;
 
    @GetMapping("getMember/{username}")
    public String getMember(@PathVariable("username") String username){
        return memberService.getMember(username);
    }
}
3. Create application.yml file:

Server:
  Port: 8082
Dubbo:
  the Application:
    name: the Order
  Registry:
    address: ZooKeeper: //127.0.0.1: 2181
  Consumer:
    timeout: 5000
4, to create applications startup class: com.slming.AppOrderApplication

Note: @EnableDubbo need to add annotations, support for open Dubbo

package com.slming;
 
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * @author itslm
 */
@EnableDubbo
@SpringBootApplication
public class AppOrderApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(AppOrderApplication.class, args);
    }
}
四、启动项目
1、启动zookeeper注册中心

2, start the service members achieve the project (springboot-dubbo-api-member-service-impl)

3, the start order item (springboot-dubbo-order-web)

4, point your browser to: localhost: 8082 / getMember / itslm enter, if that is the following page shows the successful integration!

V. pack
1, the install can be directly in the parent project

2, if the package is successful, the bottom of the console will output the following information:

Since the output more information, on top of the information omitted here ...... just posted a lowermost output.
 
[INFO] ----------------------------------------------- -------------------------
[the INFO] the Reactor the Summary:
[the INFO] 
[the INFO] springboot-parent-1.0-Dubbo the SNAPSHOT ..... SUCCESS .......... [0.320 S]
[the INFO] springboot-Dubbo-public-API-SUCCESS-Service ................ [0.020 S]
[the INFO ] springboot-Dubbo-public-API-Member-SUCCESS-Service ......... [2.450 S]
[the INFO] springboot-Dubbo-API-Member-..........-Service-impl . SUCCESS [1.440 S]
[the INFO] springboot Dubbo-Order-Web-1.0-SUCCESS ............ the SNAPSHOT [0.550 S]
[the INFO] ----------- -------------------------------------------------- -----------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.660 s
[INFO] Finished at: 2019-04-30T15:23:35+08:00
[INFO] ------------------------------------------------------------------------
 
Process finished with exit code 0
3、查看jar包

At this time, members of the project realization (springboot-dubbo-api-member-service-impl) and order item (springboot-dubbo-order-web) will be more target directory under the directory, there are packaged inside the jar package, They are as follows:

springboot-dubbo-api-member-service-impl-1.0-SNAPSHOT.jar

springboot-dubbo-order-web-1.0-SNAPSHOT.jar

4, run the jar package:

Open a command line window in the directory where the jar package, push the order of execution (Note: The order can not be reversed, otherwise the visit will throw a 500 error)

-jar springboot-Dubbo-the Java API-Member-Service-impl-1.0-SNAPSHOT.jar
 
the Java -jar springboot-Dubbo-the Order-Web-1.0-SNAPSHOT.jar
six or download the source code
GitHub Source address: https: // github. com / sheleiming / springboot-dubbo- parent

If negligence and mistakes, welcome criticism, in order to constantly improve. If you have questions or comments, please contact the bloggers: [email protected]

Like 1 point
favorites
Share

She Lei Ming's blog
has released three original articles won praise · 1 · views 1038
private letter

----------------
Disclaimer: This article is the original article CSDN bloggers "She Lei Ming's blog", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/qq_29077619/article/details/89675370

Published 51 original articles · won praise 80 · views 930 000 +

Guess you like

Origin blog.csdn.net/xiyang_1990/article/details/103543973