Springboot + Dubbo 构建多模块项目 (二)

4.接着第一节内容,我们来创建springboot-service

在springboot-parent 项目上右击>New->Module后,选择Spring Initializr,默认使用的Java版本是1.8。

对应的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.myapp</groupId>
    <artifactId>springboot-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.myapp</groupId>
            <artifactId>springboot-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.myapp</groupId>
            <artifactId>springboot-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--引入dubbo的依赖-->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- 引入zookeeper的依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--解决maven打包报错-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

    1.创建Springboot-interface接口的实现类:

package com.myapp.core.facade;

import com.alibaba.dubbo.config.annotation.Service;
import com.myapp.HelloService;
import com.myapp.core.service.HelloAService;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;

/**
 * @author :zzb
 * @createDate :2019/6/25 15:39
 * @desc :
 */
@Component
@Service
public class HelloServiceImpl implements HelloService {

    @Resource
    private HelloAService helloAService;
    @Override
    public String sayHello(String name) {
        System.out.println("-------------001-----------------执行API层Service---------------------------");
        return helloAService.sayHello(name);
    }
}

   2.为了业务逻辑更加清晰,创建Biz接口以及实现类

package com.myapp.core.service;

/**
 * @author :zzb
 * @createDate :2019/6/25 15:54
 * @desc :
 */
public interface HelloAService {
    public  String sayHello(String name);
}
package com.myapp.core.service.impl;

import com.myapp.core.service.HelloAService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :zzb
 * @createDate :2019/6/25 15:55
 * @desc :
 */
@Service("helloAService")
public class HelloAServiceImpl implements HelloAService {



    @Override
    public String sayHello(String name) {
        System.out.println("----------------------002-----------------执行BIZ层service--------------------------------");
        return name+"-------Service";
    }
}

   3.在启动类上添加dubbo注解 @EnableDubboConfiguration

package com.myapp.core;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration
public class SpringbootServiceApplication {

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

}

4.Resources目录下默认生成Springboot配置文件为application.properties配置文件,建议重新添加application.yml

##服务端口
server:
  port: 8334

##配置zookeeper
spring:
  dubbo:
    application:
      name: dubbo-provider
      registry: zookeeper://192.168.1.102:2181

修改为自己相应的zookeeper注册地址。

下部分内容,请浏览下一篇 Springboot + Dubbo构建多模块项目(三)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值