(三)SpringCloud学习系列-Rest微服务构建
转载:https://www.cnblogs.com/XiangHuiBlog/p/12089755.html
总体介绍
承接着我们的springmvc+mybatis+mysql初级高级课程,以Dept部门模块做一个微服务通用案例Consumer消费者(Client)通过REST调用Provider提供者(Server)提供的服务
Maven的分包分模块架构
一个Project带着多个Module子模块
MicroServiceCloud父工程(Project)下初次带着3个子模块(Module)
microservicecloud-api 封装的整体Entity/接口/公共配置等
microservicecloud-provider-dept-8001 微服务落地的服务提供者
microservicecloud-consumer-dept-80 微服务调用的客户端使用 80端口
1.新建父工程microservicecloud,切记是Packageing是pom模式
主要是定义POM文件,将后续各个子模块公用的jar包等统一提出来,类似一个抽象父类
<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.atguigu.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
2.新建microservicecloud-api
创建完成后请回到父工程查看pom文件变化
修改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.0</modelVersion> <parent><!-- 子类里面显示声明才能有明确的继承表现,无意外就是父类的默认版本否则自己定义 --> <groupId>com.atguigu.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-api</artifactId><!-- 当前Module我自己叫什么名字 --> <dependencies><!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
新建部门Entity且配合lombok使用mvn clean install后给其它模块引用,达到通用目的。也即需要用到部门实体的话,不用每个工程都定义一份,直接引用本模块即可
package com.atguigu.springcloud.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; } }