总体介绍
我们会使用一个Dept部门模块做一个微服务通用案例Consumer消费者(Client)通过REST调用Provider提供者(Server)提供的服务。
回忆Spring,SpringMVC,MyBatis等以往学习的知识。。。
Maven的分包分模块架构复习
一个简单的Maven模块结构是这样的:
– app-parent:一个父项目(app-parent)聚合很多子项目(app-util,app-dao,app-web…)
|-- pom.xml
|
|-- app-core
||---- pom.xml
|
|-- app.web
||---- pom.xml
…
一个父项目带着多个子Module子模块
MircroServiceCloud父工程(Project)下初次带着3个子模块(Module)
SpringCloud版本选择
大版本说明:
Spring Boot | Spring Cloud | 关系 |
---|---|---|
1.2.x | Angel版本(天使) | 兼容Spring Boot 1.2.x |
1.3.x | Brixton版本(布里克斯顿) | 兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x |
1.4.x | Camden版本(卡姆登) | 兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x |
1.5.x | Dalston版本(多尔斯顿) | 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x |
1.5.x | Edgware版本(埃奇韦尔) | 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x |
2.0.x | Finchley版本(芬奇利) | 兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x |
2.1.x | Greenwich版本(格林威治) |
实际开发版本关系:
spring-boot-starter-parent | spring-boot-starter-parent | spring-cloud-dependencies | spring-cloud-dependencies |
---|---|---|---|
版本号 | 发布日期 | 版本号 | 发布日期 |
1.5.2.RELEASE | 2017年3月 | Dalston.RC1 | 2017年未知月 |
1.5.9.RELEASE | Nov,2017 | Edgware.RELEASE | Nov,2017 |
1.5.16.RELEASE | Sep,2018 | Edgware.SR5 | Oct.2018 |
1.5.20.RELEASE | Apr,2019 | Edgware.SR5 | Oct.2018 |
2.0.2.RELEASE | May,2018 | Finchley.BUILD-SNAPSHOT | 2018年未知月 |
2.0.6.RELEASE | Oct,2018 | Finchley.SR2 | Oct.2018 |
2.1.4.RELEASE | Apr,2019 | Greanwich.SR1 | Mar.2019 |
使用后两个
创建工程
创建父工程
新建父工程项目springcloud,切记Packaging是pom模式
主要是定义POM文件,将后续各个子模块公用的jar包等统一提取出来,类似一个抽象父类
父工程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.kuang</groupId> <artifactId>springcloud</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>springcloud-api</module> <module>springcloud-provider-dept-8001</module> </modules> <!--打包方式 pom--> <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.10</lombok.version> </properties> <dependencyManagement> <dependencies> <!--springCloud的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!--SpringBoot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--数据库--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--SpringBoot 启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--以下主要是用于日志和测试--> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!--logback--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> </dependencies> </dependencyManagement> </project>
创建子模块 springcloud-api
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"> <parent> <artifactId>springcloud</artifactId> <groupId>com.kuang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-api</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <!--当前的Module自己需要的依赖,如果父依赖中已经设置了版本,这里就不用写了--> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
数据库的创建
实体类的编写
package com.kuang.springcloud.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; /* * 所有的实体类必须要实现序列化 */ @Data @NoArgsConstructor @Accessors(chain = true) // 链式写法 public class Dept implements Serializable { //Dept 实体类, orm 类表关系映射 private Long deptno;//主键 private String dname; // 这个数据是存在哪个数据库的字段~ 微服务架构:一个服务对应一个数据库,同一个信息可能存在不同的数据库 private String db_source; public Dept(String dname) { this.dname = dname; } /* 链式写法: Dept dept = new Dept(); dept.setDeptNo(11).setDname('ssss').setDb_source('001'); */ }
子模块springcloud-provider-dept-8001服务的提供者的编写
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"> <parent> <artifactId>springcloud</artifactId> <groupId>com.kuang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-provider-dept-8001</artifactId> <dependencies> <!--我们需要拿到实体类,所以要配置api module--> <dependency> <groupId>com.kuang</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jetty--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <!--热部署工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
application.yaml:
server: port: 8001 mybatis: type-aliases-package: com.kuang.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml spring: application: name: springcloud-provider-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8 username: root password: 15591177926
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--开启二级缓存--> <setting name="cacheEnabled" value="true"/> </settings> </configuration>
DeptMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.springcloud.dao.DeptDao"> <insert id="addDept" parameterType="dept"> insert into dept (dname,db_source) values (#{dname},DATABASE()); </insert> <select id="queryById" parameterType="long" resultType="dept"> select * from depth where deptno = #{deptno}; </select> <select id="queryAll" resultType="dept"> select * from dept; </select> </mapper>
接口DeptController的编写:
// 提供Restful服务 @RestController public class DeptController { @Autowired private DeptService deptService; @PostMapping("/dept/add") public boolean addDept(Dept dept) { return deptService.addDept(dept); } @GetMapping("/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return deptService.queryById(id); } @GetMapping("/dept/list") public List<Dept> queryAll() { return deptService.queryAll(); } }
整体的目录结构:
子模块springcloud-consumer-dept-80的编写
pom依赖编写
<?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>springcloud</artifactId> <groupId>com.kuang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-consumer-dept-80</artifactId> <dependencies> <dependency> <groupId>com.kuang</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
application.yaml
server: port: 80
**将RestTemplate注册到spring中:**ConfigBean.java
@Configuration public class ConfigBean { //Cofiguration -- spring applicationContext.xml @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
DeptConsumerController.java
@RestController public class DeptConsumerController { // 理解:消费者,不应该有service层 // RestTemplate ... 供我们直接调用就可以了!注解到spring中 // (url,实体:Map,Class<T> responseType) @Autowired private RestTemplate restTemplate;//提供多种便捷访问Http的方法 private static final String REST_URL_PREFIX = "http://localhost:8001"; @RequestMapping("/consumer/dept/add") public boolean add(Dept dept) { return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class); } @RequestMapping("/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class); } @RequestMapping("/consumer/dept/list") public List<Dept> list() { return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class); } }
启动服务:DeptConsumer_80
@SpringBootApplication public class DeptConsumer_80 { public static void main(String[] args) { SpringApplication.run(DeptConsumer_80.class,args); } }
整体目录结构