SpringCloud 学习笔记 —— 一、背景-优快云博客
SpringCloud 学习笔记 —— 二、微服务与微服务架构-优快云博客
SpringCloud 学习笔记 —— 三、SpringCloud 入门概述-优快云博客
SpringCloud 学习笔记 —— 四、SpringCloud Rest 学习环境搭建:服务提供者-优快云博客
SpringCloud 学习笔记 —— 五、Eureka 服务注册与发现-优快云博客
SpringCloud 学习笔记 —— 六、Ribbon:负载均衡(基于客户端)-优快云博客
四、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-provider-dept-8001 【服务提供者】
- microservicecloud-consumer-dept-80 【服务消费者】
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 |
使用后两个
推荐:
1、SpringCloud: Hoxton.SR8
2、SpringBoot:2.3.3.RELEASE
以下版本全为:2.2.5.RELEASE
3、服务者:pring-cloud-starter-netflix-eureka-client
4、Eureka:spring-cloud-starter-netflix-eureka-server
5、消费者+Ribbon:spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-netflix-ribbon
6、Feign: spring-cloud-starter-openfeign
7、Hystrix:spring-cloud-starter-netflix-hystrix
spring-cloud-starter-netflix-hystrix-dashboard
8、zuul:spring-cloud-starter-netflix-zuul
4.3 创建父工程
-
新建父工程项目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.haust</groupId> <artifactId>springcloud</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>springcloud-api</module> <module>springcloud-provider-dept-8001</module> <module>springcloud-consumer-dept-80</module> <module>springcloud-eureka-7001</module> <module>springcloud-eureka-7002</module> <module>springcloud-eureka-7003</module> <module>springcloud-provider-dept-8002</module> <module>springcloud-provider-dept-8003</module> <module>springcloud-consumer-dept-feign</module> <module>springcloud-provider-dept-hystrix-8001</module> <module>springcloud-consumer-hystrix-dashboard</module> <module>springcloud-zuul-9527</module> <module>springcloud-config-server-3344</module> <module>springcloud-config-client-3355</module> <module>springcloud-config-eureka-7001</module> <module>springcloud-config-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.18</lombok.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>0.2.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--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> <!--日志测试~--> <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> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
-
父工程为springcloud,其下有多个子mudule,详情参考完整代码了解
-
实体类一定要实现 Serializable,便于序列化对象;
在类上加 @Accessors(chain = true) // 链式写法,即
- Dept dept = new Dept();
- dept.setDaptNo(11).setDname(‘ssss’).setDb_source(‘db01’);