[springcloud学习笔记day01]SpringCloud入门学习
程序员文章站
2022-07-13 08:33:00
...
本笔记是基于尚硅谷周阳老师课程记录,有问题可以一起讨论哈~
SpringCloud入门
一、SpringBoot及SpringCloud版本选择(截止2020.6.20)
1.SpringBoot版本
根据spring官网可以得知,目前最稳定的最新版是2.3.1(查询网页)
2.SpringCloud版本
根据spring官网可以得知,目前最稳定的最新版是Hoxton SR5(查询网页)
3.版本选择
版本选择的原则需要兼顾两者的版本,以spring官网声明为主
这里推荐使用的是SpringCloud的Hoxton.SR5,SpringBoot的2.2.1.RELEASE
二、部分组件停用与替代
- 服务注册中心:Eureka停用,可以使用zookeeper,Consul,Nacos代替
- 服务调用1: Ribbon准备停更,LoadBalance作为替代
- 服务调用2: Feign被OpenFeign替代
- 服务降级: Hystrix被替代,新的为resilence4j(国外),国内是sentienl(阿里)
- 服务网关: 新组件为gateway
- 服务配置: 原为Config,推荐使用阿里的Nacos
- 服务总线: Bus停用,新的为Nacos
三、环境搭建
约定>配置>编码
1.父工程搭建
新建一个maven工程
注意设置字符编码要调整为UTF-8
在设置开启注解支持
将java编译改成8
如果有整洁的习惯,可以设置文件过滤 File types
2.整理父工程pom文件
-
删除除了pom文件的其他文件
-
package设置打包方式为pom,一般父工程都是pom
-
添加pom依赖
以下来自网友整理,版本还是使用老师的2.2.2.RELEASE和Hoxton.SR1
<?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.xiaoxiao.springcloud</groupId> <artifactId>cloud2020</artifactId> <version>1.0-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> <mysql.version>5.1.47</mysql.version> <druid.version>1.1.16</druid.version> <mybatis.spring.boot.version>2.1.1</mybatis.spring.boot.version> </properties> <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version --> <dependencyManagement> <dependencies> <!--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 alibaba 2.1.0.RELEASE--> <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> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <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> <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> <optional>true</optional> </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>
(如果出现Element ‘xxx’ cannot have character [children]建议手敲,原因是复制的时候有空格缩进 )
出现对齐线说明没问题了
我这里排查结果是注释前面有一堆空格导致出问题
3.构建子模块payment8001
构造模式五步走:
1.建module
2.改POM
3.写YML文件(配置文件)
4.编写主启动类
5.编写业务类
1.建立子模块
2.编写pom文件
摘自网友
<dependencies>
<!-- 包含了sleuth zipkin 数据链路追踪-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--eureka客户端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</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>
3.编写yml文件
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据库操作类型
driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包
#数据库更新后需要加入如下参数,更正编码规则和时区等
url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.eiletxie.springcloud.entities
4.编写主方法
5.编写业务
1.建表
CREATE TABLE payment (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`serial` VARCHAR(200) DEFAULT NULL,
PRIMARY KEY(`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
2.编写实体类
编写支付实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id ;
private String serial;
}
编写返回给前端的实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
private Integer code;//返回的状态码
private String message;//错误信息
private T data;
public CommonResult(Integer code, String message) {
this(code,message,null);
}
}
3.编写Dao接口及Mapper
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiaoxiao.springcloud.dao.PaymentDao">
<insert id="create" parameterType="com.xiaoxiao.springcloud.entities.Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment (`serial`) values(#{serial})
</insert>
<resultMap id="BaseResultMap" type="com.xiaoxiao.springcloud.entities.Payment" >
<id column="id" property="id" jdbcType="BIGINT"></id>
<id column="serial" property="serial" jdbcType="VARCHAR"></id>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id = #{id}
</select>
</mapper>
4.编写Service层
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
5.编写Control层
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping("/payment/create")
public CommonResult create(Payment payment) {
int result = paymentService.create(payment);
log.info("插入数据的编号是"+result);
if(result>0) {
return new CommonResult(200,"插入数据成功",result);
}else {
return new CommonResult(444,"插入数据失败",null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult create(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("查询的支付是"+payment);
if(payment!=null) {
return new CommonResult(200,"查询记录成功",payment);
}else {
return new CommonResult(444,"查询失败,查询ID是"+id,null);
}
}
}
注意:如果log标红,那么说明没有安装lombok插件
6.配置devtools热部署工具
1.加入pom依赖
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
2.设置
3.crol+shift+alt+/
4.先写子模块order80
操作步骤和前面类似,端口改为80,将entities复制
消费者只需要提供一个controller层
1.注入一个resttemplate
restTemplate是一种restful风格的模板
@Configuration
public class ApplicationConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
2.配置controller层
@RestController
public class OrderController {
public final String PAYMENT_URL="http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@PostMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment) {
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
3.测试
如果测试结果没有成功插入数据库,可能是因为在调用插入数据库的paymentController没有加入@requestBody注解
5.工程重构
1.改写POM
<dependencies>
<!--热部署-->
<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>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>