4. SpringCloud Rest学习环境搭建:服务提供者
4.1 介绍
- 我们会使用一个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
一个父工程带着多个Moudule子模块
MicroServiceCloud父工程(Project)下初次带着3个子模块(Module)
- microservicecloud-api 【封装的整体entity/接口/公共配置等】
- microservicecloud-consumer-dept-80 【服务提供者】
- microservicecloud-provider-dept-8001 【服务消费者】
4.2 SpringCloud版本选择
大版本说明
SpringBoot | SpringCloud | 关系 |
---|---|---|
1.2.x | Angel版本(天使) | 兼容SpringBoot1.2x |
1.3.x | Brixton版本(布里克斯顿) | 兼容SpringBoot1.3x,也兼容SpringBoot1.4x |
1.4.x | Camden版本(卡姆登) | 兼容SpringBoot1.4x,也兼容SpringBoot1.5x |
1.5.x | Dalston版本(多尔斯顿) | 兼容SpringBoot1.5x,不兼容SpringBoot2.0x |
1.5.x | Edgware版本(埃奇韦尔) | 兼容SpringBoot1.5x,不兼容SpringBoot2.0x |
2.0.x | Finchley版本(芬奇利) | 兼容SpringBoot2.0x,不兼容SpringBoot1.5x |
2.1.x | Greenwich版本(格林威治) |
实际开发版本关系
spring-boot-starter-parent | spring-cloud-dependencles | ||
---|---|---|---|
版本号 | 发布日期 | 版本号 | 发布日期 |
1.5.2.RELEASE | 2017-03 | Dalston.RC1 | 2017-x |
1.5.9.RELEASE | 2017-11 | Edgware.RELEASE | 2017-11 |
1.5.16.RELEASE | 2018-04 | Edgware.SR5 | 2018-10 |
1.5.20.RELEASE | 2018-09 | Edgware.SR5 | 2018-10 |
2.0.2.RELEASE | 2018-05 | Fomchiey.BULD-SNAPSHOT | 2018-x |
2.0.6.RELEASE | 2018-10 | Fomchiey-SR2 | 2018-10 |
2.1.4.RELEASE | 2019-04 | Greenwich.SR1 | 2019-03 |
使用后两个
4.3 创建父工程
- 新建父工程项目springcloud,切记Packageing是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>org.example</groupId>
<artifactId>com.xuuyan</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springcloud-01api</module>
<module>springcloud-02provider-dept-8001</module>
</modules>
<!-- 打包方式 -->
<packaging>pom</packaging>
<properties>
<junit.version>4.12</junit.version>
<lombok.version>1.18.12</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>
<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.23</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
父工程为springcloud,其下有多个子mudule,详情参考完整代码了解
springcloud-consumer-dept-80访问springcloud-provider-dept-8001下的controller使用REST方式
创建数据库
创建子模块——生产者(3个)
记住:这里的每个子模块,编写的套路基本都是一样的
导入相关依赖
编写application.yml配置文件
编写springboot主启动类
编写业务逻辑代码
当然再以上步骤开始之前,应该先把数据库建好,以及抽取出公共的接口(实体类),打包供其他子模块导入使用
springcloud-01api
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>com.xuuyan</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-01api</artifactId>
<!-- 当前的module自己需要的依赖,如果父依赖中已经配置了版本这里就不用写了 -->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
pojo
package com.xuyuan.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 {//实体类,orm mysql 类表关系映射
private Long deptno;//主键
private String dname;
//这个数据数存在那个数据库中的字段 一个服务对应一个数据库,同一个信息可能存在不同的数据库
private String db_source;
public Dept(String dname) {
this.dname = dname;
}
/**
链式写法
Dept dept = new Dept();
dept.setDeptNo(11).setName('ssss').
*/
}
生产者provider springcloud-02provider-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>com.xuuyan</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-02provider-dept-8001</artifactId>
<dependencies>
<dependency>
<!-- 我们需要拿到实体类,所以要配置 api module-->
<groupId>org.example</groupId>
<artifactId>springcloud-01api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</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>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<!-- TEST-->
<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>
yml配置文件
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.xuyuan.springcloud.pojo
config-location: classpath:mybatisa/mybatis-config.xml
mapper-locations: classpath:mybatisa/mapper/*.xml
#spring的配置
spring:
application:
name: springcloud-02provider-dept
datasource:
username: root
password: 123
url: jdbc:mysql://localhost:3306/db01?serverTimezone=GMT%2B8&characterEncoding=utf-8&useUnicode=true
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
springboot主启动该类
package com.xuyuan.springcloud;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//启动类
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
业务逻辑代码,这里我们再也熟悉不过了:mapper-service-controller
mapper
package com.xuyuan.springcloud.mapper;
import com.xuyuan.springcloud.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@Mapper
@Repository
public interface DeptMapper {
public int addDept(Dept dept);
public Dept queryById(@Param("deptno") Long id);
public List<Dept> queryAll();
}
mapper.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.xuyuan.springcloud.mapper.DeptMapper">
<insert id="addDept" parameterType="Dept">
insert into db01.dept (dname,db_source) values (#{dname},DATABASE())
</insert>
<select id="queryById" resultType="Dept" parameterType="Long">
select * from db01.dept where deptno=#{deptno}
</select>
<select id="queryAll" resultType="Dept">
select * from db01.dept
</select>
</mapper>
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>
service
package com.xuyuan.springcloud.service;
import com.xuyuan.springcloud.pojo.Dept;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface DeptService {
public int addDept(Dept dept);
public Dept queryById( Long id);
public List<Dept> queryAll();
}
serviceImlp
package com.xuyuan.springcloud.service;
import com.xuyuan.springcloud.mapper.DeptMapper;
import com.xuyuan.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
public int addDept(Dept dept) {
return deptMapper.addDept(dept);
}
public Dept queryById(Long id) {
return deptMapper.queryById(id);
}
public List<Dept> queryAll() {
return deptMapper.queryAll();
}
}
controller
package com.xuyuan.springcloud.controller;
import com.xuyuan.springcloud.pojo.Dept;
import com.xuyuan.springcloud.service.DeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class DeptController {
@Autowired
private DeptServiceImpl deptService;
@PostMapping("/dept/add")
public int 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 (Dept dept){
return deptService.queryAll();
}
}