Springboot整合Dubbo - 项目创建和环境搭建

1. 使用IDEA新建一个Maven项目


选择Maven后,点击next下一步



修改项目的pom.xml文件

<?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>

    <groupId>com.boot</groupId>
    <artifactId>boot-dubbo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--在这里设置打包类型为pom,作用是为了实现多模块项目-->
    <packaging>pom</packaging>

</project>

2. 创建boot-dubbo的子模块项目

创建Dubbo服务接口项目,我们把项目建立在刚才我们创建的boot-dubbo项目下面,作为boot-dubbo的子模块项目进行管理。

点击我们刚才新建好的项目,新建一个boot-dubbo的子模块 项目


选择Maven后点击 next


配置模块参数


完成项目子模块的创建

同理我们继续我们第二步的步骤,再次创建一个boot-dubbo-server这子模块项目。

创建完成后,我们的整体项目结构图如下图所示:


3. 定义每个项目的pom.xml文件

打开我们的顶层项目boot-dubbo的pom.xml文件

<?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>

    <groupId>com.main</groupId>
    <artifactId>boot-dubbo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>boot-dubbo-api</module>
        <module>boot-dubbo-server</module>
    </modules>

    <!--在这里设置打包类型为pom,作用是为了实现多模块项目-->
    <packaging>pom</packaging>

    <!-- 设置我们项目的一些版本属性 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
        <dubbo.version>2.5.5</dubbo.version>
        <zkclient.version>0.10</zkclient.version>
        <lombok.version>1.16.18</lombok.version>
        <spring-boot.version>1.5.7.RELEASE</spring-boot.version>
    </properties>


    <!-- 第一步:添加Springboot的parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <!-- 声明一些项目依赖管理,方便我们的依赖版本管理 -->
    <dependencyManagement>
        <dependencies>
            <!-- Springboot依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>

            <!-- Springboot-web依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>

            <!-- 使用lombok实现JavaBean的get、set、toString、hashCode、equals等方法的自动生成  -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <scope>provided</scope>
            </dependency>

            <!-- Dubbo依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>
</project>

boot-dubbo-api的pom.xml文件

<?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>boot-dubbo</artifactId>
        <groupId>com.main</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>boot-dubbo-api</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

boot-dubbo-server的pom.xml文件

<?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>boot-dubbo</artifactId>
        <groupId>com.main</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>boot-dubbo-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.main</groupId>
            <artifactId>boot-dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </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>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

创建如下文件目录


application.yml 

# 在这里编写springboot的配置信息
server:
  port: 8080
  context-path: /
User.java
package com.boot.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private Integer gender;

    public Integer getId () {
        return id;
    }

    public void setId (Integer id) {
        this.id = id;
    }

    public String getUsername () {
        return username;
    }

    public void setUsername (String username) {
        this.username = username;
    }

    public String getPassword () {
        return password;
    }

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

    public Integer getAge () {
        return age;
    }

    public void setAge (Integer age) {
        this.age = age;
    }

    public Integer getGender () {
        return gender;
    }

    public void setGender (Integer gender) {
        this.gender = gender;
    }
}

TestService.java

package com.boot.service;

import com.boot.entity.User;

public interface TestService {

    String sayHello(String str);

    User findUser();
}

TestServiceImpl.java

package com.boot.com.boot.service.impl;

import com.boot.entity.User;
import com.boot.service.TestService;
import org.springframework.stereotype.Service;

/**
 * @description: TODO
 * @author:LiBoMing
 * @date: 2018/3/30 15:54
 */
@Service
public class TestServiceImpl implements TestService {

    @Override
    public String sayHello (String str) {
        return "hello boy!";
    }

    @Override
    public User findUser () {
        return null;
    }
}

TestController.java

package com.boot.controller;

import com.boot.entity.User;
import com.boot.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test/")
public class TestController {
    @Autowired
    private TestService testService;

    @RequestMapping("hello")
    public String hello() {
        return testService.sayHello("Hello springboot and dubbo!");
    }

    @RequestMapping("user")
    public User user() {
        return testService.findUser();
    }
}

ServerApplication.java

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource({"classpath:config/spring-dubbo.xml"})
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

spring-dubbo.xml主要是对外开放接口的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 <!--   <dubbo:application name="consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:annotation package="com.boot.controller"/>-->
</beans>
最后启动就可以了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值