1. 程式人生 > >Springboot整合dubbo構建maven多模組專案(四)

Springboot整合dubbo構建maven多模組專案(四)

在中,我們配置了一個dubbo專案,包含三個模組springboot-dubbo-api、springboot-dubbo-server和springboot-dubbo-client,並且在springboot-dubbo-server和springboot-dubbo-client的pom中都添加了對dubbo、zk及springboot-dubbo-api的依賴。

下面我們一起來實踐下dubbo服務端及客戶端的簡單設定,以及在dubbo admin中檢視服務的註冊及消費情況。

1 環境搭建

dubbo環境的搭建可參考文章環境搭建(一):Dubbo環境搭建,包括基於docker的zookeeper安裝及dubbo admin的安裝。

2 dubbo服務提供方配置

資料庫建立表:SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_name` varchar(20) DEFAULT '' COMMENT '登入名',
  `real_name` varchar(10) DEFAULT '' COMMENT '真實姓名',
  `park_code` char(8) DEFAULT '' COMMENT '園區編號',
  `park_name` varchar(20) DEFAULT '' COMMENT '園區',
  `pwd` varchar(20) DEFAULT '' COMMENT '密碼',
  `state` tinyint(4) DEFAULT '0' COMMENT '狀態,0-無效,1-有效',
  `role` tinyint(4) DEFAULT '0' COMMENT '角色,0-園區管理員,1-系統管理員',
  `last_date` timestamp NULL DEFAULT NULL COMMENT '最近一次訪問時間',
  `last_ip` int(10) unsigned DEFAULT '0' COMMENT '最後一次登入的IP',
  `login_count` int(6) DEFAULT '0' COMMENT '登入次數',
  `date_add` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '新增時間',
  `date_upd` timestamp NULL DEFAULT NULL COMMENT '修改時間',
  PRIMARY KEY (`id`),
  KEY `fk_user_park` (`park_code`),
  CONSTRAINT `fk_user_park` FOREIGN KEY (`park_code`) REFERENCES `park` (`code`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;

2.1 在springboot-dubbo-api中建立實體類及服務介面

BaseEntity.java:

package com.example.demo.model;
import java.io.Serializable;
import java.util.Date;
/**
 * @author liyanlei
 * @Description:實體公共屬性
* @date 2018-01-15 16:15
 */
public class BaseEntity implements Serializable {

    /**
     * 刪除標記(0:正常;1:刪除;2:稽核;)
*/
//    public static final String DEL_FLAG_NORMAL = "0";
// public static final String DEL_FLAG_DELETE = "1"; // public static final String DEL_FLAG_AUDIT = "2"; /** 新增時間 */ private Date dateAdd; /** 修改時間 */ private Date dateUpd; // private String delFlag; //0 有效 1 刪除 public BaseEntity() { } public BaseEntity(Date dateAdd, Date dateUpd) { this.dateAdd = dateAdd; this.dateUpd = dateUpd; } public Date getDateAdd() { return dateAdd; } public void setDateAdd(Date dateAdd) { this.dateAdd = dateAdd; } public Date getDateUpd() { return dateUpd; } public void setDateUpd(Date dateUpd) { this.dateUpd = dateUpd; } }
User.java
package com.example.demo.model;
import java.util.Date;
public class User extends BaseEntity {
    /** 主鍵ID,自增  */
private Integer id;
/** 登入名 */
private String userName;
/** 真實姓名 */
private String realName;
/** 園區編號 */
private String parkCode;
/** 園區 */
private String parkName;
/** 密碼 */
private String pwd;
/** 狀態,0-無效,1-有效 */
private Byte state;
/** 角色,0-園區管理員,1-系統管理員 */
private Byte role;
/** 最近一次訪問時間 */
private Date lastDate;
/** 最後一次登入的IP */
private String lastIp;
/** 登入次數 */
private Integer loginCount;
    public User() {

    }

    public User(Integer id, String userName, String realName, String parkCode, String parkName, String pwd, Byte state, Byte role, Date lastDate, String lastIp, Integer loginCount, Date dateAdd, Date dateUpd) {
        super(dateAdd, dateUpd);
        this.id = id;
        this.userName = userName;
        this.realName = realName;
        this.parkCode = parkCode;
        this.parkName = parkName;
        this.pwd = pwd;
        this.state = state;
        this.role = role;
        this.lastDate = lastDate;
        this.lastIp = lastIp;
        this.loginCount = loginCount;
}

    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 getRealName() {
        return realName;
}

    public void setRealName(String realName) {
        this.realName = realName;
}

    public String getParkCode() {
        return parkCode;
}

    public void setParkCode(String parkCode) {
        this.parkCode = parkCode;
}

    public String getParkName() {
        return parkName;
}

    public void setParkName(String parkName) {
        this.parkName = parkName;
}

    public String getPwd() {
        return pwd;
}

    public void setPwd(String pwd) {
        this.pwd = pwd;
}

    public Byte getState() {
        return state;
}

    public void setState(Byte state) {
        this.state = state;
}

    public Byte getRole() {
        return role;
}

    public void setRole(Byte role) {
        this.role = role;
}

    public Date getLastDate() {
        return lastDate;
}

    public void setLastDate(Date lastDate) {
        this.lastDate = lastDate;
}

    public String getLastIp() {
        return lastIp;
}

    public void setLastIp(String lastIp) {
        this.lastIp = lastIp;
}

    public Integer getLoginCount() {
        return loginCount;
}

    public void setLoginCount(Integer loginCount) {
        this.loginCount = loginCount;
}
}
Pagination.java
package com.example.demo.vo.queryVo;
/**
 * 分頁實體類
*/
public class Pagination {

    //第幾頁 從0開始
private int pageNo = 0;
//每頁多少條資料
private int pageSize = 10;
    private int totalCount = 0;
    private int skip = 0;
    public int getPageNo() {
        return pageNo;
}

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
}

    public int getPageSize() {
        return pageSize;
}

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
}

    public int getSkip() {
        return this.pageNo * this.pageSize;
}

    public void setSkip(int skip) {
        this.skip = skip;
}
}
UserQuery.java
package com.example.demo.vo.queryVo;
import java.io.Serializable;
/**
 * 查詢條件
*/
public class UserQuery extends Pagination implements Serializable {
    private static final long serialVersionUID = 1609252883937908952L;
//查詢條件
private int notId = -1;
/** 園區編號 */
private String parkCode;
/** 登入名 or 真實姓名 */
private String name;
/** 登入名 or 真實姓名 模糊查詢 */
private String nameLike;
//查詢條件
public int groupId = -1; //ID
public int getNotId() {
        return notId;
}

    public void setNotId(int notId) {
        this.notId = notId;
}

    public int getGroupId() {
        return groupId;
}

    public void setGroupId(int groupId) {
        this.groupId = groupId;
}

    public static long getSerialVersionUID() {
        return serialVersionUID;
}

    public String getParkCode() {
        return parkCode;
}

    public void setParkCode(String parkCode) {
        this.parkCode = parkCode;
}

    public String getName() {
        return name;
}

    public void setName(String name) {
        this.name = name;
}

    public String getNameLike() {
        return nameLike;
}

    public void setNameLike(String nameLike) {
        this.nameLike = nameLike;
}
}
PageModel.java
package com.example.demo.vo.resultVo;
import java.io.Serializable;
import java.util.List;
/**
 * 分頁,返回資料
*/
public class PageModel<T> implements Serializable{
    //結果集
private List<T> datas;
//每頁多少條資料
private int pageSize = 10;
//第幾頁 從0開始
private int pageNo = 0;
//總條數
private long totalCount = 0;
//總頁數
private int totalPages = 0;
    private int last_index = 0;
    public List<T> getDatas() {
        return datas;
}

    public void setDatas(List<T> datas) {
        this.datas = datas;
}

    public int getPageSize() {
        return pageSize;
}

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
}

    public int getPageNo() {
        return pageNo;
}

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
}

    public long getTotalCount() {
        return totalCount;
}

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
}

    public int getTotalPages() {
        return (int) (totalCount % pageSize >0 ? (totalCount/pageSize+1) :(totalCount / pageSize));
}

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
}

    public int getLast_index() {
        return last_index;
}

    public void setLast_index(int last_index) {
        this.last_index = last_index;
}
}
UserService.java
package com.example.demo.api;
import com.example.demo.model.User;
import com.example.demo.vo.queryVo.UserQuery;
import com.example.demo.vo.resultVo.PageModel;
import java.util.List;
/**
 * @author liyanlei
 * @Description: 使用者Service
 * @date 2018-01-15 16:15
 */
public interface UserService {

    /**
     * 根據id查詢
* @param id  使用者ID
     * @return  商戶訂單
*/
User findOneById(Integer id);
/**
     * 根據使用者名稱密碼查詢
* @param userName
* @param pwd
* @return
*/
User findOneByNameAndPwd(String userName, String pwd);
/**
     * 根據使用者名稱稱查詢
* @param userName
* @return
*/
User findOneByUserName(String userName);
/**
     * 根據組ID查詢
* @param groupId
* @return
*/
List<User> findListByGroupId(int groupId);
/**
     * 根據角色列表查詢
* @param roleTypes
* @return
*/
List<User> findListByRoles(String roleTypes);
/**
     * 根據條件分頁查詢
* @param query 查詢條件
* @return 分頁資訊
*/
PageModel<User> findPageByQuery(UserQuery query);
/**
     * 根據條件查詢記錄總條數
* @param query 查詢條件
* @return 符合條件的記錄數
*/
int findCountByQuery(UserQuery query);
/**
     * 查詢賬號是否存在
* @param userName 賬號
* @param notId  = ID
     * @return 0:不存在,其他:存在。
*/
int findCountByUserName(String userName, Integer notId);
/**
     * 新增一個使用者
* @param user
* @return
*/
int insertOne(User user);
/**
     * 插入(插入屬性不為空的值)
     * @param user
* @return
*/
int insertSelective(User user);
/**
     * 根據ID修改使用者資訊
* @param user
* @return
*/
int updateById(User user);
/**
     * 根據ID修改組資訊
* @param user
* @return
*/
int updateByIdSelective(User user);
    int delById(Integer id);
//    PageModel<User> getCarrierPage(Integer pageIndex, Integer pageSize, CarrierVo carrierVo);
}

2.2 springboot-dubbo-server中實現springboot-dubbo-api介面

目錄結構:


2.2.1 整合mybatis - pom.xml中引入jar包

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.1</version>
</dependency>

2.2.2 整合mybatis - application.properties 資料庫連線配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/XXX?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=xxxspring.datasource.password=xxxspring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=10
spring.datasource.max-idle=5
spring.datasource.min-idle=0
xxx需要修改為自己資料庫的配置

2.2.3 整合mybatis - UserMapper.java

package com.example.demo.dao;
import com.example.demo.model.User;
import com.example.demo.vo.queryVo.UserQuery;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserMapper {

    /**
     * 根據條件分頁查詢
* @param query 查詢條件
* @return 分頁資料
*/
List<User> selectListByQuery(UserQuery query);
/**
     * 根據條件查詢記錄總條數
* @param query 查詢條件
* @return 符合條件的記錄數
*/
int selectCountByQuery(UserQuery query);
@Delete({
        "delete from user",
"where id = #{id,jdbcType=INTEGER}"
})
    int deleteByPrimaryKey(Integer id);
@Insert({
        "insert into user (user_name, real_name, ",
"park_code, park_name, ",
"pwd, state, role, ",
"last_date, last_ip, ",
"login_count, date_add, ",
"date_upd)",
"values (#{userName,jdbcType=VARCHAR}, #{realName,jdbcType=VARCHAR}, ",
"#{parkCode,jdbcType=CHAR}, #{parkName,jdbcType=VARCHAR}, ",
"#{pwd,jdbcType=VARCHAR}, #{state,jdbcType=TINYINT}, #{role,jdbcType=TINYINT}, ",
"#{lastDate,jdbcType=TIMESTAMP}, INET_ATON(#{lastIp,jdbcType=INTEGER}), ",
"#{loginCount,jdbcType=INTEGER}, #{dateAdd,jdbcType=TIMESTAMP}, ",
"#{dateUpd,jdbcType=TIMESTAMP})"
})
//    @SelectKey(statement="VALUES IDENTITY_VAL_LOCAL()", keyProperty="id", before=false, resultType=Integer.class)
int insert(User record);
    int insertSelective(User record);
@Select({
        "select",
"id, user_name, real_name, park_code, park_name, pwd, state, role, last_date, ",
"INET_NTOA(last_ip) last_ip, login_count, date_add, date_upd",
"from user",
"where id = #{id,jdbcType=INTEGER}"
})
    @ResultMap("BaseResultMap")
    User selectByPrimaryKey(Integer id);
@Select({
            "select",
"id, user_name, real_name, park_code, park_name, pwd, state, role, last_date, ",
"INET_NTOA(last_ip) last_ip, login_count, date_add, date_upd",
"from user",
"where user_name = #{userName,jdbcType=VARCHAR} and pwd = #{pwd,jdbcType=VARCHAR}"
})
    @ResultMap("BaseResultMap")
    User selectOneByNameAndPwd(String userName, String pwd);
@Select({
            "select",
"id, user_name, real_name, park_code, park_name, pwd, state, role, last_date, ",
"INET_NTOA(last_ip) last_ip, login_count, date_add, date_upd",
"from user",
"where user_name = #{userName,jdbcType=VARCHAR}"
})
    @ResultMap("BaseResultMap")
    User selectOneByUserName(String userName);
/**
     * 查詢賬號是否存在
* @param userName 賬號
* @param notId  = ID
     * @return 0:不存在,其他:存在。
*/
@Select({
            "select count(*) from `user` ",
"where user_name = #{userName,jdbcType=VARCHAR} and id != #{notId,jdbcType=INTEGER}"
})
//    @ResultType("java.lang.Integer")
int selectCountByUserName(@Param("userName") String userName, @Param("notId") Integer notId);
    int updateByPrimaryKeySelective(User record);
@Update({
        "update user",
"set user_name = #{userName,jdbcType=VARCHAR},",
"real_name = #{realName,jdbcType=VARCHAR},",
"park_code = #{parkCode,jdbcType=CHAR},",
"park_name = #{parkName,jdbcType=VARCHAR},",
"pwd = #{pwd,jdbcType=VARCHAR},",
"state = #{state,jdbcType=TINYINT},",
"role = #{role,jdbcType=TINYINT},",
"last_date = #{lastDate,jdbcType=TIMESTAMP},",
"last_ip = INET_ATON(#{lastIp,jdbcType=INTEGER}),",
"login_count = #{loginCount,jdbcType=INTEGER},",
"date_add = #{dateAdd,jdbcType=TIMESTAMP},",
"date_upd = #{dateUpd,jdbcType=TIMESTAMP}",
"where id = #{id,jdbcType=INTEGER}"
})
    int updateByPrimaryKey(User record);
}

2.2.4 整合mybatis - UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.example.demo.model.User" >
    <constructor >
      <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="user_name" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="real_name" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="park_code" jdbcType="CHAR" javaType="java.lang.String" />
      <arg column="park_name" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="pwd" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="state" jdbcType="TINYINT" javaType="java.lang.Byte" />
      <arg column="role" jdbcType="TINYINT" javaType="java.lang.Byte" />

            
           

相關推薦

Springboot整合dubbo構建maven模組專案

在中,我們配置了一個dubbo專案,包含三個模組springboot-dubbo-api、springboot-dubbo-server和springboot-dubbo-client,並且在springboot-dubbo-server和springboot-dubbo-c

Springboot整合dubbo構建maven模組專案- 專案建立和pom.xml中jar包配置

       以前一直用Spring作為容器構建專案,但是看到Spring官網一直在推Springboot,最重要的是Springboot確實避免自己尋找多個jar包(大多數情況下,可能自己都不記得該引入哪些jar包)和jar包之間衝突的問題,同時省掉了在整合其他框架時候

STS(Eclipse)構建Maven模組專案水平結構和樹形結構

在配置完Java開發環境和MAVEN後,我們來建立專案 STS(Eclipse)下載 請到官網http://spring.io/tools/sts/all/ 下載STS最新版 然後放到G盤,然後解壓到當前 G:\sts-bundle\

構建Maven模組專案+SSM框架整合(一)

Maven Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model

Intellij 構建maven模組專案(二)

拿了一個,黑馬程式培訓專案,來講! 淘淘商城。 1. 父工程,下面,兩個子工程。 2.manager,聚合工程,聚合工程下面新增,5個子模組。 3.為什麼要使用,聚合工程?這個就要自己去學maven相關。   使用不使用聚合,關聯依賴和繼承都是要自己手動去關聯的。

Intellij 構建maven模組專案(一)

參考: IntelliJ IDEA 構建maven多模組工程專案 來自 <https://blog.csdn.net/sinat_34344123/article/details/79080601> idea--8.新建maven父子專案 來自 <https:/

使用maven構建模組專案

1. 使用dependencyManagement管理依賴 dependencyManagement的作用:Maven使用dependencyManagement元素來提供一種管理依賴版本號的方法。 (1)helloweb-parent——>pom

使用maven構建模組專案

1. 建立helloweb專案的骨架結構 (1)建立一個maven專案helloweb,刪除target資料夾,如下圖所示 (2)在helloweb目錄下,建立以下4個資料夾 a. helloweb-parent b. helloweb-en

SpringBoot+Maven模組專案建立、依賴、打包可執行jar包部署測試完整流程

開發環境:IDEA,                   SprngBoot 2.0.4,                   Maven 2.19.1 工程結構:                              父工程father            

SpringBoot 模組專案moduleService自動注入@Autowired空指標錯誤解決

 報錯資訊,這個我是截了上一部分,為了省空間下部分我就用...代替了。 java.lang.NullPointerException at com.jd.impl.UploadServiceImpl.uploadBlock(UploadServiceImpl.java:39)

Springboot+Maven模組開發 初始化工程新建第一個web工程

學習Springboot+maven多模組開發筆記。 首先建立一個空專案,新建一個pom檔案,該pom檔案是整個工程的parent pom。 pom檔案內容如下: <pre name="code" class="html"><?xml version

SpringBoot 模組專案moduleService自動注入@Autowired

如果你因為Service注入失敗,看過無數文章,甚至看過N份原始碼仍不得要領,希望我能終結你的問題; SpringBoot中Service自動注入很方便,例: Service.class(介面類)

springbootmaven模組專案架構微服務搭建--跳過springmvc單一專案直接構建模組並衍化為微服務專案

  總想對微服務架構做一個小小的總結,不知如何下手,最近覺得還是從搭建微服務的過程來入手,對於springboot的maven專案從構建多模組架構進而衍化為常用的微服務架構來做個記錄吧。   首先,建立多個springboot專案,專案結構如圖:       裁剪後如右側   建立完成後,先解釋一下:s

springboot + dubbo + zookeeper 註冊中心 + maven模組專案框架搭建具體操作

1. 專案搭建前瞭解: Maven多模組專案可以解決專案中出現多個相同的jar包和service介面以及實體類物件的問題,可以將相同的提取成一個專案來維護管理,然後其他需要用到則只要引用jar包即可使用。 2. 將springboot + dubbo + zookeeper專案進行拆分: 生

SpringBoot+Maven 模組專案構建、執行、打包實戰

專案使用的工具: IntelliJ IDEA JDK 1.8 apache-maven-3.3.9 專案的目錄: 主專案 springboot-multi 子模組 entity、dao、service、web 一、使用IDEA建立一個Spring

SpringBoot建立maven模組專案

SpringBoot建立maven多模組專案 專案結構     該專案名稱為springboot-maven-multi,由springboot-maven-multi、user-dao、user-domain、user-service、user-web個模組組成,其中spring

springbootmaven模組專案架構微服務搭建——依賴方式的模組演化為微服務專案

在上一篇依賴方式多模組的基礎上對專案進行改造。主要改造user-service專案,service要配置mapper。mybatis及資料庫相關的東西,後面的介面消費方user就不再需要了 注意:以下程式碼是在不同場所的機器上寫的,資料庫什麼的會有不同,結構也會有不同,最終的程式碼會以其中一個傳遞到本人gi

基於STS對springboot eureka叢集進行Maven模組構建

基於STS對springboot進行Maven多模組構建 這是整體的模組 1、父級模組: 把pom檔案中的war 改成pom 2、(1)構建eureka-server-springboot 在主工程右鍵—>maven—>New Maven Module Project

IntelliJ IDEA 構建maven模組工程專案(詳細圖)

食用前須知 本文以a b c 三個模組為例來搭建專案,以達到通俗易懂的初衷 模組a —– 基模組,就是人們常說的parent 模組b —– 其他模組都需要使用的一些工具,比如時間工具,json工具等 模組c —– 專案主要的內容,一般為

基於maven使用IDEA搭建和部署SpringBoot模組專案Multi-Module

What matters in life is not what happens to you but what you remember and how you remember it. 生命中真正重要的不是你遭遇了什麼,而是你記住了哪些事,又是如