快速上手(四)服务消费者 - 项目搭建

项目结构

项目是以Maven构建的多模块项目,1个父项目,3个子项目;

Parent Model - dubboTwo

  • 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.debug.mooc.dubbo.two</groupId>
    <artifactId>dubboTwo</artifactId>
    <packaging>pom</packaging>
    <version>1.0.1</version>
    <modules>
        <module>api</module>
        <module>model</module>
        <module>server</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
</project>

Sub Model - api

pom.xml
  • 有自己的 <artifactId>api</artifactId>;
<?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>
        <groupId>com.debug.mooc.dubbo.two</groupId>
        <artifactId>dubboTwo</artifactId>
        <version>1.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>api</artifactId>

    <properties>
        <mybatis-pagehelper.version>4.1.2</mybatis-pagehelper.version>
        <lombok.version>1.16.10</lombok.version>
    </properties>

    <dependencies>
        <!--java校验 跟 hibernate校验-->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.3.5.Final</version>
        </dependency>

        <!--for page-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${mybatis-pagehelper.version}</version>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

    </dependencies>


</project>

Sub Model - model

pom.xml
  • 有自己的 <artifactId>model</artifactId>;
  • 依赖于 api 子模块;
<?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>dubboTwo</artifactId>
        <groupId>com.debug.mooc.dubbo.two</groupId>
        <version>1.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>model</artifactId>

    <properties>
        <mybatis-spring-boot.version>1.1.1</mybatis-spring-boot.version>
        <mybatis-pagehelper.version>4.1.2</mybatis-pagehelper.version>
    </properties>

    <dependencies>
        <!--api-->
        <dependency>
            <groupId>com.debug.mooc.dubbo.two</groupId>
            <artifactId>api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

        <!--spring-mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot.version}</version>
        </dependency>

    </dependencies>

</project>

Sub Model - server

  • 作为服务提供者的主体,是个 Springboot 项目,依赖于服务提供者的 api 模块,通过该模块完成对服务提供者的远程过程调用;
  • 也需要整合 Dubbo & Zookeeper;
pom.xml
  • 有自己的 <artifactId>server</artifactId>;
  • 依赖于 model 子模块;
  • 依赖于服务提供者的 api 模块;
<?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>dubboTwo</artifactId>
        <groupId>com.debug.mooc.dubbo.two</groupId>
        <version>1.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>server</artifactId>

    <packaging>jar</packaging>

    <properties>
        <start-class>com.debug.mooc.dubbo.two.server.BootMoreApplication</start-class>

        <!--<spring-boot.version>1.3.3.RELEASE</spring-boot.version>-->
        <spring-boot.version>2.0.5.RELEASE</spring-boot.version>

        <spring-session.version>1.2.0.RELEASE</spring-session.version>
        <slf4j.version>1.7.13</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <mysql.version>5.1.37</mysql.version>
        <druid.version>1.0.16</druid.version>
        <guava.version>19.0</guava.version>
        <joda-time.version>2.9.2</joda-time.version>
        <cglib.version>3.1</cglib.version>

        <zookeeper.version>3.4.10</zookeeper.version>
        <curator.version>2.12.0</curator.version>
        <dubbo.version>2.8.4</dubbo.version>

        <resteasy.version>3.0.7.Final</resteasy.version>
        <okhttp.version>3.1.2</okhttp.version>
        <gson.version>2.6.1</gson.version>
        <httpclient.version>4.3.6</httpclient.version>

    </properties>

    <!-- 依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <!--dubboOne发布的api-->
        <dependency>
            <groupId>com.debug.mooc.dubbo.one</groupId>
            <artifactId>api</artifactId>
            <version>1.0.1</version>
        </dependency>

        <!--model-->
        <dependency>
            <groupId>com.debug.mooc.dubbo.two</groupId>
            <artifactId>model</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

        <!--log-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <!--guava-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>

        <!-- time -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda-time.version}</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!--spring-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>log4j-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- jsp 支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--<scope>provided</scope>-->
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!--<scope>provided</scope>-->
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>curator-framework</artifactId>
                    <groupId>org.apache.curator</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>curator-client</artifactId>
                    <groupId>org.apache.curator</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>guava</artifactId>
                    <groupId>com.google.guava</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>${cglib.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>asm</artifactId>
                    <groupId>org.ow2.asm</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- zookeeper start -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${curator.version}</version>
        </dependency>

        <!--dubbo rest-->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>${resteasy.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>httpclient</artifactId>
                    <groupId>org.apache.httpcomponents</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-netty</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>${resteasy.version}</version>
        </dependency>


        <!--for test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>
        </dependency>

        <!-- okhttp -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>${okhttp.version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>dubboTwo-${project.parent.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>
application.properties
  • tomcat 监听在 8093 端口;
  • 指定了注册中心 zookeeper 的地址;
#profile
#spring.profiles.active=production
#spring.profiles.active=local

server.port=8094
#server.context-path=/dubboTwo
server.servlet.context-path=/dubboTwo

#logging
logging.path=/
logging.file=dubboTwo.log

logging.level.org.springframework = INFO
logging.level.com.fasterxml.jackson = INFO
logging.level.com.debug.mooc.dubbo.two = DEBUG

#json\u5E8F\u5217\u5316\u914D\u7F6E
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

#spring.datasource.initialize=false
spring.datasource.initialization-mode=never
spring.jmx.enabled=false


#\u6570\u636E\u6E90\u914D\u7F6E
datasource.url=jdbc:mysql://127.0.0.1:3306/mooc_two?useUnicode=true&amp;characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
datasource.username=root
datasource.password=MyNewPass4!

#mybatis
mybatis.config-location=classpath:mybatis-config.xml
mybatis.checkConfigLocation = true
mybatis.mapper-locations=classpath:mappers/*.xml


#dubbo zookeeper\u914D\u7F6E\u4FE1\u606F
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo 配置(一) - spring/spring-dubbo.xml
  • 服务消费者同时也是服务提供者;
  • 对于依赖的 dubboOne 中的 api 模块的接口,需要专门的配置;
<?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服务类所在包的注解-->
    <dubbo:annotation package="com.debug.mooc.dubbo.two.server.service.dubbo" />

    <!--注册中心zookeeper配置信息-->
    <dubbo:registry address="${dubbo.registry.address}" />

    <!--支持两种协议的调用:rpc-dubbo协议;http协议-rest api-url调用-->
    <dubbo:protocol name="rest" threads="500" contextpath="v1" server="tomcat" accepts="500"/>
    <dubbo:protocol name="dubbo" />

    <!--消费方信息配置-->
    <dubbo:application name="dubboTwo-consume" owner="debug" organization="dubbox"/>

    <!--引入服务提供方提供的dubbo服务-url="dubbo://127.0.0.1:20903" -->
    <dubbo:reference interface="com.debug.mooc.dubbo.one.api.service.IDubboItemService" id="dubboItemService"
                     url="dubbo://127.0.0.1:20903" protocol="dubbo" version="1.0" timeout="20000">
    </dubbo:reference>



</beans>
dubbo 配置(二) - dubbo.properties
  • RPC 监听在 20904 端口;
  • HTTP 监听在 9014 端口;
dubbo.container=log4j,spring
dubbo.reference.check=false
dubbo.registry.client=curator
dubbo.application.name=dubboTwo-provider
dubbo.application.owner=debug

#dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.dubbo.port=20904

#dubbo
dubbo.protocol.name=rest
dubbo.protocol.rest.port=9014
dubbo.protocol.rest.server=tomcat

dubbo.service.loadbalance=roundrobin
启动类
  • 该加载的配置文件都加载上,没什么好说的;
package com.debug.mooc.dubbo.two.server;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
//import org.springframework.boot.context.web.SpringBootServletInitializer; //1.x版本

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; //2.x版本

import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(value = {"classpath:spring/spring-jdbc.xml","classpath:spring/spring-dubbo.xml"})
@MapperScan(basePackages = "com.debug.mooc.dubbo.two.model.mapper")
public class BootMoreApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(BootMoreApplication.class);
    }

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

}
RPC 调用
  • 注入 IDubboItemService,这是服务提供者 dubboOne 的子模块 api 中暴露出来的接口;spring/spring-dubbo.xml 中将它置入 IOC 容器,id 为 dubboItemService,这才使得 @Autowired 得以成功;
  • 对消费者提供者 dubboOne 服务的调用,和调用本地方法是一样的;
package com.debug.mooc.dubbo.two.server.controller;

import com.debug.mooc.dubbo.one.api.response.BaseResponse;
import com.debug.mooc.dubbo.one.api.service.IDubboItemService;
import com.google.common.collect.Maps;
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.RestController;

import java.util.Map;

@RestController
public class ItemController {

    private static final Logger log= LoggerFactory.getLogger(ItemController.class);

    private static final String prefix = "item";

    @Autowired
    private IDubboItemService dubboItemService;



    /**
     * 用户商城列表查询
     * @return
     */
    @RequestMapping(value = prefix + "/list", method = RequestMethod.GET)
    public Map<String,Object> list(){
        Map<String,Object> resMap = Maps.newHashMap();
        resMap.put("code","0");
        resMap.put("msg","成功");

        //TODO:调用服务提供方dubboOne提供的列表查询功能
        try {
            BaseResponse response = dubboItemService.listItems();
            if (response != null && response.getCode().equals(0)){
                resMap.put("data", response.getData());
            }
        }catch (Exception e){
            e.printStackTrace();
            resMap.put("code", "-1");
            resMap.put("msg", "失败");
        }
        return resMap;
    }


    /**
     * 用户商城列表查询-分页查询
     * @return
     */
    @RequestMapping(value = prefix + "/page/list", method = RequestMethod.GET)
    public Map<String,Object> pageList(Integer pageNo, Integer pageSize){
        if (pageNo == null || pageSize == null || pageNo <= 0 || pageSize <= 0){
            pageNo=1;
            pageSize=2;
        }

        Map<String,Object> resMap = Maps.newHashMap();
        resMap.put("code","0");
        resMap.put("msg","成功");

        //TODO:调用服务提供方dubboOne提供的列表查询-分页查询功能
        try {
            BaseResponse response = dubboItemService.listPageItems(pageNo, pageSize);
            if (response != null && response.getCode().equals(0)){
                resMap.put("data", response.getData());
            }
        }catch (Exception e){
            e.printStackTrace();
            resMap.put("code", "-1");
            resMap.put("msg", "失败");
        }
        return resMap;
    }


    /**
     * 用户商城列表查询-分页查询-带参数模糊查询
     * @return
     */
    @RequestMapping(value = prefix+"/page/list/params",method = RequestMethod.GET)
    public Map<String,Object> pageListParams(Integer pageNo,Integer pageSize,String search){
        if (pageNo==null || pageSize==null || pageNo<=0 || pageSize<=0){
            pageNo=1;
            pageSize=2;
        }
        Map<String,Object> resMap= Maps.newHashMap();
        resMap.put("code","0");
        resMap.put("msg","成功");

        //TODO:调用服务提供方dubboOne提供的列表查询-分页查询功能
        try {
            BaseResponse response=dubboItemService.listPageItemsParams(pageNo,pageSize,search);
            if (response!=null && response.getCode().equals(0)){
                resMap.put("data",response.getData());

            }
        }catch (Exception e){
            e.printStackTrace();
            resMap.put("code","-1");
            resMap.put("msg","失败");
        }
        return resMap;
    }
}

上一篇:快速上手(三)服务提供者 - 项目搭建

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值