写在前面
- 官方代码地址:https://github.com/zzyybs/atguigu_spirngcloud2020
- 本文地址:https://blog.csdn.net/hancoder/article/details/109063671
- 本文下载方式:见博客中谷粒商城下载方式(记得注意cloud笔记,否则默认为谷粒笔记)
什么是微服务架构集中:
SpringCloud 是微服务一站式服务解决方案,微服务全家桶。它是微服务开发的主流技术栈。它使用名称,而不是数字版本号。
SpringCloud 和 springCloud Alibaba 目前是最主流的微服务框架组合。
版本选择:
选用 springboot 和 springCloud 版本有约束,不按其约束会有冲突。
版本问题
本研究的各种软件版本:
- boot使用数字作为版本。强烈建议官网升级到2.0以上
- cloud以字母为版本,伦敦地铁站名称
Cloud Release Train | Boot Version |
---|---|
Hoxton | 2.2.x, 2.3.x (Starting with SR5) |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
查看版本对应关系:https://start.spring.io/actuator/info
"spring-cloud": {
"Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M5", "Finchley.M3": "Spring Boot >=2.0.0.M5 and <=2.0.0.M5", "Finchley.M4": "Spring Boot >=2.0.0.M6 and <=2.0.0.M6", "Finchley.M5": "Spring Boot >=2.0.0.M7 and <=2.0.0.M7", "Finchley.M6": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1", "Finchley.M7": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2", "Finchley.M9": "Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE", "Finchley.RC1": "Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE", "Finchley.RC2": "Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE", "Finchley.SR4": "Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT", "Finchley.BUILD-SNAPSHOT": "Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3", "Greenwich.M1": "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE", "Greenwich.SR6": "Spring Boot >=2.1.0.RELEASE and <2.1.18.BUILD-SNAPSHOT", "Greenwich.BUILD-SNAPSHOT": "Spring Boot >=2.1.18.BUILD-SNAPSHOT and lt;2.2.0.M4",
"Hoxton.SR8": "Spring Boot >=2.2.0.M4 and <2.3.5.BUILD-SNAPSHOT",
"Hoxton.BUILD-SNAPSHOT": "Spring Boot >=2.3.5.BUILD-SNAPSHOT and <2.4.0.M1",
"2020.0.0-M3": "Spring Boot >=2.4.0.M1 and <=2.4.0.M1",
"2020.0.0-SNAPSHOT": "Spring Boot >=2.4.0.M2"
},
尚硅谷阳哥教程版本:
cloud | Hoxton.SR1 |
boot | 2.2.2.RELEASE |
cloud alibaba | 2.1.0.RELEASE |
java | java8 |
maven | 3.5及以上 |
mysql | 5.7及以上 |
cloud版本决定了boot版本
微服务停更说明
1,Eureka停用,可以使用zk作为服务注册中心
2,服务调用,Ribbon准备停更,代替为LoadBalance
3,Feign改为OpenFeign
4,Hystrix停更,改为resilence4j
或者阿里巴巴的sentienl
5.Zuul改为gateway
6,服务配置Config改为 Nacos
7,服务总线Bus改为Nacos
Cloud简介
参考资料,尽量去官网
https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/
工程建造
写一个下图的Hello World
构建父工程,后面的项目模块都在此工程中:
设置编码:Settings -> File Encodings
注解激活:
Java版本确定:
父工程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"> <modelVersion>4.0.0</modelVersion> <groupId>com.dkf.cloud</groupId> <artifactId>cloud2020</artifactId> <version>1.0-SNAPSHOT</version> <!-- 第一步 打包方式pom--> <packaging>pom</packaging> <!-- 统一管理 jar 包版本 --> <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> <mysql.version>5.1.47</mysql.version> <druid.version>1.1.16</druid.version> <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version> </properties> <!-- 子块基础之后,提供作用:
锁定版本 + 子module不用写 groupId 和 version --> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </dependency> <!-- 下面三个基本是微服务架构的标配 --> <!--spring boot 2.2.2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud Hoxton.SR1--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud 阿里巴巴--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope> </dependency> <!-- druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build> </project>
上面配置的解释:
首先要加
<packaging>pom</packaging>
这个。为了让项目顺利的运行,我们必须使用统一的版本号; (1)在我们项目中,我们会发现在父模块的pom文件中常常会出现dependencyManagement元素,这是因为我们可以通过其来管理子模块的版本号,也就是说我们在父模块中; (1)上面说到dependencyManagement只是声明一个依赖,而不实现引入,故我们在子模块中也需要对依赖进行声明,倘若不声明子模块自己的依赖,是不会从父模块中继承的;只有子模块中也声明了依赖。并且没有写对应的版本号它才会从父类中继承;并且version和scope都是取自父类;此外要是子模块中自己定义了自己的版本号,是不会继承自父类的。 dependencyManagement只是用来管理依赖,规定未添加版本号的子模块依赖继承自它,dependencies是用来声明子模块自己的依赖,可以在其中来写自己需要的版本号
聚合版本依赖,dependencyManagement只声明依赖,并不实现引入,所以子项目还需要写要引入的依赖。
可以统一版本
父工程创建完成执行mvn:install将父工程发布到仓库方便子工程继承
第一个微服务架构
创建一个module后(只能改a),在父工程的pom里多了个<modules>
,
在模块的pom里没有gv,只有a。
模块里的<dependencies>
里的依赖只有ga,没有v
提供者
cloud-provider-payment8001 子工程的pom文件:
这里面的 lombok 这个包,引入以后,实体类不用再写set 和 get
可以如下写实体类:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @AllArgsConstructor @NoArgsConstructor public class Payment implements Serializable { private Integer id; private String serial; }
<?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>cloud2020</artifactId>
<groupId>com.dkf.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8001</artifactId>
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<!--这个和web要写到一块-->
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
cloud-provider-payment8001 子工程的yml文件:
server:
port: 8001
spring:
application:
name: cloud-provider-payment8001
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.dkf.springcloud.entities # 所有Entity 别名类所在包
cloud-provider-payment8001 子工程的主启动类:
package com.dkf.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args){
SpringApplication.run(PaymentMain8001.class, args);
}
}
下面的常规操作:
-
①建表SQL
create table `payment`( `id` bigint(20) not null auto_increment comment 'ID', `serial` varchar(200) default '', PRIMARY KEY (`id`) )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 select * from payment;
entities
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data //set/get方法
@AllArgsConstructor //有参构造器
@NoArgsConstructor //无参构造器
public class Payment implements Serializable {
private long id;//数据库是bigint
private String serial;
}
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //返回给前端的通用json数据串 @Data //set/get方法 @AllArgsConstructor //有参构造器 @NoArgsConstructor //无参构造器 public class CommonResult<T> { private Integer code; private String message; private T data; //泛型,对应类型的json数据 //
自定义两个参数的构造方法 public CommonResult(Integer code, String message){ this(code, message, null); } }
dao
@Mapper // 是ibatis下面的注解 //@Repositoty有时候会有问题 public interface PaymentDao { int