Eclipse构建Maven分包分模块项目并构建服务端

本篇是 SpringCloud 学习笔记的第三篇。 Eclipse 构建Maven 分包分模块项目。本篇构建的Maven模块项目是之后SpringCloud各个组件整合的基础模块。
本篇关联篇章如下:

  • 主页
  • 1、简单微服务SpringCloud学习
  • 2、微服务架构与SpringCloud

首先说一下Maven 模块结构:

``

一个简单的Maven模块结构是这样的:
---- app-parent 一个父项目(app-parent)聚合很多子项目(app-util,app-dao,app-service,app-web)
|---- pom.xml (pom)
|
|-------- app-util
| |-------- pom.xml (jar)
|
|-------- app-dao
| |-------- pom.xml (jar)
|
|-------- app-service
| |-------- pom.xml (jar)
|
|-------- app-web
|-------- pom.xml (war)

环境

JDK 1.8/Eclipse/Spring Boot 1.5.9.RELEASE/Spring Cloud Dalston.SR1

构建Maven 父项目

打开Eclipse,新建Maven 项目 springcloudDemo
Eclipse构建Maven分包分模块项目并构建服务端_第1张图片
注意:

  • 由于笔记是学习完写的,所以在截图的时候完全按照自己搭建的项目名来写,出现项目名重复,所以这里直接马赛克。正常不会出现这种问题。
  • 请选择 pom

打开父项目:观察结构
Eclipse构建Maven分包分模块项目并构建服务端_第2张图片
添加pom 依赖:

<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.0modelVersion>
  <groupId>com.mike.demogroupId>
  <artifactId>springcloudDemoartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>pompackaging>
  
  <properties>
   <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
   <maven.compiler.source>1.8maven.compiler.source>
   <maven.compiler.target>1.8maven.compiler.target>
   <junit.version>4.12junit.version>
   <log4j.version>1.2.17log4j.version>
   <lombok.version>1.16.18lombok.version>
  properties>
 
  <dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>org.springframework.cloudgroupId>
       <artifactId>spring-cloud-dependenciesartifactId>
       <version>Dalston.SR1version>
       <type>pomtype>
       <scope>importscope>
     dependency>
     <dependency>
       <groupId>org.springframework.bootgroupId>
       <artifactId>spring-boot-dependenciesartifactId>
       <version>1.5.9.RELEASEversion>
       <type>pomtype>
       <scope>importscope>
     dependency>
     <dependency>
       <groupId>mysqlgroupId>
       <artifactId>mysql-connector-javaartifactId>
       <version>5.0.4version>
     dependency>
     <dependency>
       <groupId>com.alibabagroupId>
       <artifactId>druidartifactId>
       <version>1.0.31version>
     dependency>
     <dependency>
       <groupId>org.mybatis.spring.bootgroupId>
       <artifactId>mybatis-spring-boot-starterartifactId>
       <version>1.3.0version>
     dependency>
     <dependency>
       <groupId>ch.qos.logbackgroupId>
       <artifactId>logback-coreartifactId>
       <version>1.2.3version>
     dependency>
     <dependency>
       <groupId>junitgroupId>
       <artifactId>junitartifactId>
       <version>${junit.version}version>
       <scope>testscope>
     dependency>
     <dependency>
       <groupId>log4jgroupId>
       <artifactId>log4jartifactId>
       <version>${log4j.version}version>
     dependency>
   dependencies>
  dependencyManagement>
project>

基础模快共有三个

  • springcloudDemo-api 封装的整体entity/接口/公共配置等
  • springcloudDemo-provider-dept-8001 微服务落地的服务提供者
  • springcloudDemo-consumer-dept-80 微服务调用的客户端使用
    下面分别就上面上个模块进行构建

pringcloudDemo-api 构建

  1. 鼠标右键点击 父项目 springcloudDemo,新建 Maven Mondule ,点击下一步
    Eclipse构建Maven分包分模块项目并构建服务端_第3张图片
  2. 输入模块名称:springcloudDemo-api
    Eclipse构建Maven分包分模块项目并构建服务端_第4张图片
  3. 选择Jar 包,点击Finish
    Eclipse构建Maven分包分模块项目并构建服务端_第5张图片
  4. 查看父项目此时的情况
    Eclipse构建Maven分包分模块项目并构建服务端_第6张图片
  5. pom 依赖
<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.0modelVersion>
  <parent>
    <groupId>com.mike.demogroupId>
    <artifactId>springcloudDemoartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>springcloudDemo-apiartifactId>
  <dependencies>
   <dependency>
     <groupId>org.projectlombokgroupId>
     <artifactId>lombokartifactId>
   dependency>
  dependencies>
project>
  1. 新建 Model Dept
    Eclipse构建Maven分包分模块项目并构建服务端_第7张图片
package com.mike.demo.entities;

import java.io.Serializable;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@SuppressWarnings("serial")
@NoArgsConstructor
@Data
@Accessors(chain=true)
public class Dept implements Serializable //必须序列化
{
  private Long  deptno;   //主键
  private String  dname;   //部门名称
  private String  db_source;//来自那个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库
  
  public Dept(String dname)
  {
   super();
   this.dname = dname;
  }
  public Dept(Long deptno, String dname, String db_source) {
	super();
	this.deptno = deptno;
	this.dname = dname;
	this.db_source = db_source;
  }
	public Long getDeptno() {
		return deptno;
	}
	public void setDeptno(Long deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getDb_source() {
		return db_source;
	}
	public void setDb_source(String db_source) {
		this.db_source = db_source;
	}
}

注意: 可能大家认为 使用了 lombok 为什么还需要写setter/getter 方法,但是在之后的组件整合的过程中,返回Dept Json 数据的时候无法正确返回所以加上了。
8. 点击pom 文件 run : maven clean
9. clean 之后,maven install 将 api 打成jar包供之后的模块使用。
注意: 在打包的过程中,可能出现 Perhaps you are running on a JRE rather than a JDK? 解决方案可以参考:传送门

springcloudDemo-provider-dept-8001

如同上面新建 springcloudDemo-api 的步骤一样,新建项目。
建成后,父项目拥有:
Eclipse构建Maven分包分模块项目并构建服务端_第8张图片
Eclipse构建Maven分包分模块项目并构建服务端_第9张图片
下面看一下 springcloudDemo-api 项目结构:
Eclipse构建Maven分包分模块项目并构建服务端_第10张图片

  1. pom 文件
 
<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.0modelVersion>
 
  <parent>
    <groupId>com.mike.demogroupId>
    <artifactId>springcloudDemoartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>springcloudDemo-provider-dept-8001artifactId>
 
  <dependencies>
   <dependency>
     <groupId>com.mike.demogroupId>
     <artifactId>springcloudDemo-apiartifactId>
     <version>${project.version}version>
   dependency>
   <dependency>
     <groupId>junitgroupId>
     <artifactId>junitartifactId>
   dependency>
   <dependency>
     <groupId>mysqlgroupId>
     <artifactId>mysql-connector-javaartifactId>
   dependency>
   <dependency>
     <groupId>com.alibabagroupId>
     <artifactId>druidartifactId>
   dependency>
   <dependency>
     <groupId>ch.qos.logbackgroupId>
     <artifactId>logback-coreartifactId>
   dependency>
   <dependency>
     <groupId>org.mybatis.spring.bootgroupId>
     <artifactId>mybatis-spring-boot-starterartifactId>
   dependency>
   <dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-jettyartifactId>
   dependency>
   <dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-webartifactId>
   dependency>
   <dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-testartifactId>
   dependency>
   
   <dependency>
     <groupId>org.springframeworkgroupId>
     <artifactId>springloadedartifactId>
   dependency>
   <dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-devtoolsartifactId>
   dependency>
  dependencies>
 
project>
  1. 新建application.yml 文件
server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.mike.demo.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
spring:
   application:
    name: springcloudDemo-dept 
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://xxxxxxxxx:3306/cloudDB01              # 数据库名称
    username: root
    password: root
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间
  1. mybaties.cfg.xml


  
<configuration>
 
  <settings>
   <setting name="cacheEnabled" value="true"/>
  settings>
 
configuration>
  1. DeptMapper.xml


 
<mapper namespace="com.mike.demo.dao.DeptDao">
 
  <select id="findById" resultType="Dept" parameterType="Long">
   select deptno,dname,db_source from dept where deptno=#{deptno}; 
  select>
  <select id="findAll" resultType="Dept">
   select deptno,dname,db_source from dept; 
  select>
  <insert id="addDept" parameterType="Dept">
   INSERT INTO dept(dname,db_source) VALUES(#{dname},DATABASE());
  insert>
  
mapper>
  1. DeptDao.java
package com.mike.demo.dao;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.mike.demo.entities.Dept;

@Mapper
public interface DeptDao
{
  public boolean addDept(Dept dept);
 
  public Dept findById(Long id);
 
  public List<Dept> findAll();
}
  1. DeptService.java
package com.mike.demo.service;

import java.util.List;
import com.mike.demo.entities.Dept;
public interface DeptService {
	public boolean add(Dept dept);
	public Dept    get(Long id);
    public List<Dept> list();

}
  1. DeptServiceImpl.java
package com.mike.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mike.demo.dao.DeptDao;
import com.mike.demo.entities.Dept;
import com.mike.demo.service.DeptService;

@Service
public class DeptServiceImpl implements DeptService
{
  @Autowired
  private DeptDao dao ;
  
  @Override
  public boolean add(Dept dept)
  {
   return dao.addDept(dept);
  }
 
  @Override
  public Dept get(Long id)
  {
   return dao.findById(id);
  }
 
  @Override
  public List<Dept> list()
  {
   return dao.findAll();
  }
 
}
  1. DeptController.java
package com.mike.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mike.demo.entities.Dept;
import com.mike.demo.service.DeptService;

@RestController
public class DeptController
{
  @Autowired
  private DeptService service;
  
  @RequestMapping(value="/dept/add",method=RequestMethod.POST)
  public boolean add(@RequestBody Dept dept)
  {
   return service.add(dept);
  }
  
  @RequestMapping(value="/dept/get/{id}",method=RequestMethod.GET)
  public Dept get(@PathVariable("id") Long id)
  {
   return service.get(id);
  }
  
  @RequestMapping(value="/dept/list",method=RequestMethod.GET)
  public List<Dept> list()
  {
   return service.list();
  }
}
  1. 服务启动类 DeptProvider8001_App
package com.mike.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 服务的提供者
 * @author asus
 *
 */
@SpringBootApplication
public class DeptProvider8001_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptProvider8001_App.class, args);
  }
}

数据库文件, 根据上面application.yml 配置的数据库,到Mysql中建库建表

DROP DATABASE IF EXISTS cloudDB01;
CREATE DATABASE cloudDB01 CHARACTER SET UTF8;
USE cloudDB01;
CREATE TABLE dept
(
  deptno BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  dname VARCHAR(60),
  db_source   VARCHAR(60)
);
INSERT INTO dept(dname,db_source) VALUES('开发部',DATABASE());
INSERT INTO dept(dname,db_source) VALUES('人事部',DATABASE());
INSERT INTO dept(dname,db_source) VALUES('财务部',DATABASE());
INSERT INTO dept(dname,db_source) VALUES('市场部',DATABASE());
INSERT INTO dept(dname,db_source) VALUES('运维部',DATABASE());
 
SELECT * FROM dept;

服务端测试

启动启动类
Eclipse构建Maven分包分模块项目并构建服务端_第11张图片
Eclipse构建Maven分包分模块项目并构建服务端_第12张图片
这样我们的 Eclipse 分包构建项目就完成了,且完成了 SpringCloud 服务端的程序,由于篇幅有限,所以客户端的放在下一个篇中。

你可能感兴趣的:(Spring,Cloud)