項目結(jié)構(gòu)
項目是以Maven構(gòu)建的多模塊項目拷肌,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
- 作為服務(wù)提供者的主體,是個 Springboot 項目凌净,依賴于服務(wù)提供者的 api 模塊悲龟,通過該模塊完成對服務(wù)提供者的遠(yuǎn)程過程調(diào)用;
- 也需要整合 Dubbo & Zookeeper冰寻;
pom.xml
- 有自己的 <artifactId>server</artifactId>躲舌;
- 依賴于 model 子模塊;
- 依賴于服務(wù)提供者的 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發(fā)布的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 監(jiān)聽在 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
- 服務(wù)消費者同時也是服務(wù)提供者;
- 對于依賴的 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">
<!--發(fā)布出去的dubbo服務(wù)類所在包的注解-->
<dubbo:annotation package="com.debug.mooc.dubbo.two.server.service.dubbo" />
<!--注冊中心zookeeper配置信息-->
<dubbo:registry address="${dubbo.registry.address}" />
<!--支持兩種協(xié)議的調(diào)用:rpc-dubbo協(xié)議;http協(xié)議-rest api-url調(diào)用-->
<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"/>
<!--引入服務(wù)提供方提供的dubbo服務(wù)-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 監(jiān)聽在 20904 端口煤蚌;
- HTTP 監(jiān)聽在 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 調(diào)用
- 注入 IDubboItemService筒占,這是服務(wù)提供者 dubboOne 的子模塊 api 中暴露出來的接口;spring/spring-dubbo.xml 中將它置入 IOC 容器蜘犁,id 為 dubboItemService翰苫,這才使得 @Autowired 得以成功;
- 對消費者提供者 dubboOne 服務(wù)的調(diào)用这橙,和調(diào)用本地方法是一樣的奏窑;
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:調(diào)用服務(wù)提供方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:調(diào)用服務(wù)提供方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;
}
/**
* 用戶商城列表查詢-分頁查詢-帶參數(shù)模糊查詢
* @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:調(diào)用服務(wù)提供方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;
}
}