这期内容当中小编将会给大家带来有关如何正确的使用springcloud,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
微服务是软件开发的一种架构方式,由单一的应用小程序构成的小服务;一个软件系统由多个服务组成;在微服务中,服务是细粒度的,协议是轻量级的(部署简单、性能开销小)
随着时代的发展,单体架构(MVC三层模型)越来越不能满足企业的要求;业务规模的不断扩大、团队开发人员的增多,使得单体架构出现了以下几个问题:
(1)部署效率低(比如代码量非常多,依赖的包非常多,那么每一次编译打包、部署测试的时间就会很久)
(2)团队开发成本高(如果某个模块出现问题,可能就要多人修改)
(3)高可用性低(打成war包部署之后,如果某一块出现了问题,其他模块都会受到影响)
(4)一旦代码膨胀(简单了说你写的代码量大,但功能没实现几个,浪费资源),上线就会变慢
SOA(面向服务编程)便出现了
SOA简介
SOA是一个组件模型,它将应用程序的不同功能单元(称为服务)进行拆分,并通过这些服务之间定义良好的接口和协议联系起来。
虽然解决了服务的启动时间,但服务化也有很大的缺点
1、耦合度较高(一个依赖可能另一个模块,另外一个模块又可能依赖其他模块)
2、公司成本高(每一个模块都要由相应的人员或者团队维护)
2014年,随着容器化技术的成熟以及 DevOps 文化的兴起,微服务便应运而生了
微服务的特点
1.服务细粒度
2.协议轻量级
3.部署简单
4.服务的独立维护性
springcloud是分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体。
1.分布式/版本化配置
2.服务注册和发现
3.路由
4.服务到服务的通话
5.负载均衡
6.断路器
7.全局锁
8.领导选举和集群状态
9.分布式消息传递
SpringCloud的官网地址:https://spring.io/projects/spring-cloud#overview
1.进入springcloud官方页面:https://spring.io/projects/spring-cloud#learn
2. LEARN->Reference Doc.
3. 版本选择
最近发现网络上很多springcloud入门体验都是关于消费-订单的小Demo,那我当然不能落后了(菜是原罪?)
1、创建一个父项目(统一管理子模块依赖版本)
2、创建第一个子模块-----》订单服务模块
3、创建第二个子模块-----》客户消费模块
编写代码之前应该先构思好流程,如果直接就去码代码,码了一会可能又要添添改改;还有可能做完项目之后又要对项目进行重构,这就是没有构思好流程。
1、技术选型
2、业务流程
3、核心业务分析
4、代码优化
…
删除多余的文件,留下一个pom.xml文件
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.cloud</groupId> <artifactId>springcloud_all</artifactId> <version>1.0-SNAPSHOT</version> <modules> ## 创建了子模块之后会自动加入 ## <module>payment-module</module> </modules> <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.13</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> <mysql.version>8.0.19</mysql.version> <druid.version>1.2.4</druid.version> <mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version> <druid.spring.boot.version>1.2.4</druid.spring.boot.version> <springfox-swagger-ui.version>2.9.2</springfox-swagger-ui.version> <springfox-swagger2.version>2.9.2</springfox-swagger2.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.3</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring-cloud 2020.0.2--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.2</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring-cloud alibaba--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2021.1</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>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.spring.boot.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox-swagger-ui.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox-swagger2.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>
右键->New->Module
PaymentApplication启动类
@SpringBootApplication public class PaymentApplication { public static void main(String[] args) { SpringApplication.run(PaymentApplication.class,args); } }
Ⅲ.Ⅰ、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_all</artifactId> <groupId>com.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.pay</groupId> <artifactId>payment-module</artifactId> <dependencies> <!-- web+监控--> <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> <!-- mybatis--> <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> </dependency> <!-- mysql--> <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> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies> </project>
Ⅲ.Ⅱ、application.yml 配置文件编写
server: port: 8081 spring: application: name: payment-module datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: hao20001010 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.cloud.entity
Ⅲ.Ⅲ、Controller层编写
package com.cloud.controller; import com.cloud.Service.PaymentService; import com.cloud.entity.Payment; import com.cloud.entity.Result; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author:抱着鱼睡觉的喵喵 * @date:2021/4/18 * @description: */ @RestController @Slf4j @Api(value = "《----订单模块----》") public class PaymentController { private static final Logger logger = LoggerFactory.getLogger(PaymentController.class); @Resource private PaymentService paymentService; /** * 创建订单模块 * @api /payment/create * @param payment Payment实体类 * @return Result<></> */ @ApiOperation(value = "创建订单") @PostMapping(value = "/payment/create") public Result create(@RequestBody Payment payment) { int result = paymentService.create(payment); logger.info("插入结果========》" +result); if (result > 0) { return new Result(200,"插入成功", result); } else { return new Result(500, "插入失败", null); } } /** * 订单查询模块 * @api /payment/get/{id} * @param id 编号 * @return Payment实体类 */ @GetMapping(value = "/payment/get/{id}") @ApiModelProperty(value = "订单查询") public Result getPaymentById(@PathVariable(value = "id") Integer id) { Payment payment = paymentService.getPaymentById(id); logger.info("订单查询结果=========>"+payment); if (payment != null) { return new Result(200, "查询成功", payment); } else { logger.error("订单插叙失败!请排查原因"); return new Result(500, "查询失败,无该订单ID:"+id, null); } } }
Ⅲ.Ⅳ、service层
public interface PaymentService { int create(Payment payment); Payment getPaymentById(@Param("id") Integer id); }
@Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentMapper paymentMapper; @Override public int create(Payment payment) { return paymentMapper.create(payment); } @Override public Payment getPaymentById(Integer id) { return paymentMapper.getPaymentById(id); } }
Ⅲ.Ⅴ、mapper层
@Mapper public interface PaymentMapper { int create(Payment payment); Payment getPaymentById(@Param("id") Integer id); }
Ⅲ.Ⅵ、mapper.xml
<?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.cloud.mapper.PaymentMapper"> <insert id="create" parameterType="payment" keyProperty="id" useGeneratedKeys="true"> insert into payment(orders)values(#{orders}) </insert> <select id="getPaymentById" parameterType="payment" resultMap="BaseResultMap" > select * from payment where id=#{id} </select> <resultMap id="BaseResultMap" type="payment"> <id column="id" property="id" jdbcType="INTEGER"/> <id column="orders" property="orders" jdbcType="VARCHAR"/> </resultMap> </mapper>
@Data public class Result<T> implements Serializable { private Integer code; private String message; private T data; public Result(){} public Result(Integer code, String msg) { this(code, msg, null); } public Result(Integer code, String msg, T data) { this.code = code; this.message = msg; this.data = data; } }
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel(value = "Payment实体类") public class Payment { @ApiModelProperty(value = "id") private Integer id; @ApiModelProperty(value = "订单信息") private String orders; }
Payment、Result以及ConsumerApplication启动类和上面类似。
PRE:RestTemplate详解
RestTemplate是spring封装的一个服务端远程HTTP请求工具,支持常见的Rest请求(get、post等)
简单了说就是用来远程调用的,因为你订单模块是一个服务,消费模块又是一个单独的服务,两个服务端的端口号要进行交互,所以就有了RestTemplate
RestTemplate是服务端HTTP调用的
Jsonp是客户端调用的
server: port: 80
@Configuration public class ApplicationContextConfig { @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
Ⅲ、ConsumerController
@RestController @Api(value = "消费模块") public class ConsumerController { private static final String CONSUMER_URL = "http://localhost:8081"; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/create") @ApiOperation(value = "模拟添加") public Result<Payment> create(Payment payment) { return restTemplate.postForObject(CONSUMER_URL + "/payment/create", payment, Result.class); } @ApiOperation(value = "模拟获取") @GetMapping("/consumer/payment/get/{id}") public Result<Payment> getPayment(@PathVariable(value = "id") Integer id) { return restTemplate.getForObject(CONSUMER_URL + "/payment/get/" + id, Result.class); } }
上述就是小编为大家分享的如何正确的使用springcloud了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。