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

项目结构

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

Parent Model - dubboTwo

  • pom.xml


    4.0.0

    com.debug.mooc.dubbo.two
    dubboTwo
    pom
    1.0.1
    
        api
        model
        server
    

    
        UTF-8
        1.8
        ${java.version}
        ${java.version}
    

Sub Model - api

pom.xml
  • 有自己的 api


    
        com.debug.mooc.dubbo.two
        dubboTwo
        1.0.1
    
    4.0.0

    api

    
        4.1.2
        1.16.10
    

    
        
        
            javax.validation
            validation-api
            1.1.0.Final
        
        
            org.hibernate
            hibernate-validator
            5.3.5.Final
        

        
        
            com.github.pagehelper
            pagehelper
            ${mybatis-pagehelper.version}
        

        
        
            org.projectlombok
            lombok
            ${lombok.version}
        

    



Sub Model - model

pom.xml
  • 有自己的 model
  • 依赖于 api 子模块;


    
        dubboTwo
        com.debug.mooc.dubbo.two
        1.0.1
    
    4.0.0

    model

    
        1.1.1
        4.1.2
    

    
        
        
            com.debug.mooc.dubbo.two
            api
            ${project.parent.version}
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis-spring-boot.version}
        

    


Sub Model - server

  • 作为服务提供者的主体,是个 Springboot 项目,依赖于服务提供者的 api 模块,通过该模块完成对服务提供者的远程过程调用;
  • 也需要整合 Dubbo & Zookeeper;
pom.xml
  • 有自己的 server
  • 依赖于 model 子模块;
  • 依赖于服务提供者的 api 模块;


    
        dubboTwo
        com.debug.mooc.dubbo.two
        1.0.1
    
    4.0.0

    server

    jar

    
        com.debug.mooc.dubbo.two.server.BootMoreApplication

        
        2.0.5.RELEASE

        1.2.0.RELEASE
        1.7.13
        1.2.17
        5.1.37
        1.0.16
        19.0
        2.9.2
        3.1

        3.4.10
        2.12.0
        2.8.4

        3.0.7.Final
        3.1.2
        2.6.1
        4.3.6

    

    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    

        
        
            com.debug.mooc.dubbo.one
            api
            1.0.1
        

        
        
            com.debug.mooc.dubbo.two
            model
            ${project.parent.version}
        

        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        

        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        

        
            log4j
            log4j
            ${log4j.version}
        

        
        
            com.google.guava
            guava
            ${guava.version}
        

        
        
            joda-time
            joda-time
            ${joda-time.version}
        

        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        

        
        
            com.alibaba
            druid
            ${druid.version}
        

        
        
            org.springframework.boot
            spring-boot-starter-web
            ${spring-boot.version}
            
                
                    ch.qos.logback
                    logback-classic
                
                
                    org.slf4j
                    log4j-over-slf4j
                
            
        

        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            
        

        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            
        

        
            javax.servlet
            jstl
        

        
        
            com.alibaba
            dubbo
            ${dubbo.version}
            
                
                    curator-framework
                    org.apache.curator
                
                
                    curator-client
                    org.apache.curator
                
                
                    guava
                    com.google.guava
                
                
                    spring-web
                    org.springframework
                
            
        

        
            cglib
            cglib
            ${cglib.version}
            
                
                    asm
                    org.ow2.asm
                
            
        

        
        
            org.apache.zookeeper
            zookeeper
            ${zookeeper.version}
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
        
            org.apache.curator
            curator-framework
            ${curator.version}
        

        
        
            org.jboss.resteasy
            resteasy-jaxrs
            ${resteasy.version}
            
                
                    httpclient
                    org.apache.httpcomponents
                
            
        
        
            org.jboss.resteasy
            resteasy-netty
            ${resteasy.version}
        
        
            org.jboss.resteasy
            resteasy-client
            ${resteasy.version}
        
        
            org.jboss.resteasy
            resteasy-jackson-provider
            ${resteasy.version}
        


        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.google.code.gson
            gson
            ${gson.version}
        

        
        
            com.squareup.okhttp3
            okhttp
            ${okhttp.version}
        

    

    
        dubboTwo-${project.parent.version}
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
                
                    
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.4
                
                    false
                
            
        

        
            
                src/main/resources
                true
            
        
    


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&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 模块的接口,需要专门的配置;



    
    

    
    

    
    
    

    
    

    
    
    




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 list(){
        Map 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 pageList(Integer pageNo, Integer pageSize){
        if (pageNo == null || pageSize == null || pageNo <= 0 || pageSize <= 0){
            pageNo=1;
            pageSize=2;
        }

        Map 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 pageListParams(Integer pageNo,Integer pageSize,String search){
        if (pageNo==null || pageSize==null || pageNo<=0 || pageSize<=0){
            pageNo=1;
            pageSize=2;
        }
        Map 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;
    }
}

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

你可能感兴趣的:(快速上手(四)服务消费者 - 项目搭建)