微服务(一)【手摸手带你搭建Spring Cloud】 怎么理解微服务的概念?Spring Cloud又是什么?Spring Cloud和微服务有着什么样的关系?

我就是我 2022-12-20 03:12 124阅读 0赞

本身,在学习之前,我也是对spring cloud 比较陌生,也不理解什么所谓的注册中心等内容。学完之后就想从一个小白的角度去讲,去着手了解这个spring cloud 是什么东西,一个篇章可能介绍不完,也没法完整地去解释这是个什么内容,因此我想通过几篇连载去讲述,当然会有很多不足,因此也欢迎大家私聊我给出意见。

Spirng Cloud(一 什么是微服务)

  • 前言
  • 一、微服务是什么?
  • 二、踏出微服务(Spring Cloud)的第一步
    • 1、创建父工程
    • 2、创建消费服务(custom-service)和user服务(user-service)
    • 3、现在我们开始配置,配置成Spring Boot项目
      • 3.1 父工程pom
      • 3.2 子工程pom
      • 3.3 custom-service启动类
      • 3.4 custom-service配置文件
      • 3.5 custom-service的service和control层
      • 3.6 开始入手微服务

前言

大家可能在此之前多多少少都听说过微服务。微服务在最近几年也是很火,如果你不会微服务都可能都显得不那么主流了。甚至有很多团队强行为了微服务而去微服务,最终写成一个大型的分布式单体应用,就是改造后的系统既没有微服务的快速扩容,灵活发布的特性,也让原本的单体应用失去了方便开发,部署容易的特性(项目拆为多份,开发部署复杂度都提高了),不得不说是得不偿失。因此我们在了解SpringCloud 前非常有必要了解这是个什么东西,我们为什么使用他?怎么样设计更加合理。


一、微服务是什么?

首先呢,我认为了解一个陌生的东西,更好的是通过代码,通过实践去了解更为深刻,也更好了解。
不需要用多么专业的术语,因为我在一开始看到这些专业的术语我是懵逼的,虽然那些术语让人觉得很装逼,但是我更希望知道这是个什么东西更为重要。那么本次面向的读者呢也是刚要了解这部分内容的朋友,如果你是大佬,emmm,这文章肯定有很多不足,可以私聊我帮忙指点。
在这里插入图片描述
上面这是我在别的地方找到的一张图,可以清楚地看到这是个逐步解耦的过程。
解耦不解耦,怎么理解,怎么解释?微服务是什么样?
解耦就是分开,怎么分开,先看下没分开前,我们的工程目录是什么样。
在这里插入图片描述
大家注意看这个service、handler、mapper的包,此时都在一个工程中。
可以看到单体应用缺点
部署成本高:无论是修改一行代码还是十行还是全部,所有的代码都需要替换。
改动影响大,风险高(耦合度高):在同一个工程里,你改动的内容可能有很多地方需要修改。
因为成本高,风险高,所以导致部署频率低(无法快速交付客户需求)

所以微服务的目的就是解决这些问题
怎么做呢?就是将系统应由原来的单体变成几十到几百个不同的工程。
这就是所谓的解耦,让每个服务可以独立运行。每一个子工程都会部署在一台服务器上,这么多服务器我们便称之为服务集群。

因此我们就可以看到微服务的优点
针对特定服务发布,影响小,风险小,成本低(因为服务分离了,增加服务只需加一个,对其它的不用全部更新)
频繁发布版本,快速交付需求
低成本扩容,弹性伸缩,适应云环境

当然,它也带来了很多缺点
分布式系统的复杂性
部署,测试和监控的成本问题
分布式事务和CAP的相关问题

产生服务间的依赖,服务如何拆封,内部接口规范,数据传递等问题,尤其是服务拆分,需要团队熟悉业务流程,懂得取舍,要保证拆分的粒度服务既符合“高内聚,低耦合”的基本原则,还要兼顾业务的发展以及公司的愿景,要还要说服团队成员为之努力,并且积极投入,在多方中间取得平衡。

二、踏出微服务(Spring Cloud)的第一步

既然都说了手摸手,那当然就是从创建工程开始,并且本次为了介绍微服务,所以会抛开dao层。使大家更清晰明了看到微服务的运转。首先肯定要自己装maven。

1、创建父工程

Create new project - maven - 勾选create from archetype - (maven-archetype-quickstart)-next
在这里插入图片描述
groupId 我这边就是org(个人)antry(我的网名)大家也可以和我一样,也可以用自己的。当然后面有的地方可能用到。注意一下就行,还有artifactId 是父工程名,我这边创建的父工程名时studycloud
在这里插入图片描述
选择自己的maven库
在这里插入图片描述
这就是文件存储位置
在这里插入图片描述

2、创建消费服务(custom-service)和user服务(user-service)

父工程创建好后我们先不急着配置,再创建两个子工程。
custom-service大家看这个名称可以理解这是一个消费者,因此这个user±service就是认为是生产者服务。再明了一点讲,我们一会儿就是要去访问custom的服务器,通过custom访问user服务。
在这里插入图片描述
后面其它选的和父工程是一样的,只有最后一步注意一下。
这个横杆需要自己加上,到这边它会没掉。
在这里插入图片描述
在这里插入图片描述
在每个子工程下创建resources文件夹
在这里插入图片描述
在这里插入图片描述

3、现在我们开始配置,配置成Spring Boot项目

当然了,学这个spring cloud 你要先去了解一下spring boot 了
下面话不多说,开始配置

3.1 父工程pom

添加 spring-boot parent标签

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.3.4.RELEASE</version>
  5. </parent>

3.2 子工程pom

两个子工程一样
首先告诉系统子工程的父亲,因此添加parent标签

  1. <parent>
  2. <groupId>org.antry</groupId>
  3. <artifactId>studycloud</artifactId>
  4. <version>1.0</version>
  5. </parent>

添加两个dependency

  1. <!-- springboot 启动-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- 热启动依赖-->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-devtools</artifactId>
  10. <optional>true</optional>
  11. </dependency>

和一个plugin

  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-maven-plugin</artifactId>
  4. </plugin>

3.3 custom-service启动类

现在我们先测试custom工程是否能够使用,那么我这边也介绍一个插件,大家可以安装一下,用于快速创建启动类。
在这里插入图片描述
我们需要配置,所以我们也勾选这个application.yml,它会在resources下生成
在这里插入图片描述
大家记得刷新maven,不然会找不到包。

3.4 custom-service配置文件

application.yml中我们先配置端口和服务名

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: custom-service

3.5 custom-service的service和control层

在这里插入图片描述
UserService接口

  1. package org.antry.service;
  2. /** * @ClassName UserService * @Description 接口 * @Autor TT * @Date 2020/11/11 11:30 * @Version 1.0 */
  3. public interface UserService {
  4. /** * 返回 * @param id * @return */
  5. public String doGetUser(Long id);
  6. }

UserServiceImpl实现类

  1. package org.antry.service;
  2. import org.springframework.stereotype.Service;
  3. /** * @ClassName UserService * @Description 实现类 * @Autor T_Antry * @Date 2020/11/11 11:26 * @Version 1.0 */
  4. @Service
  5. public class UserServiceImpl implements UserService{
  6. @Override
  7. public String doGetUser(Long id){
  8. return "custom-Service回应:" +
  9. "<div><h1>T_Antry工作室-springcloud动手了解</h1></div>" +
  10. "customService[无中转]:"+String.valueOf(id);
  11. }
  12. }

handler类

  1. package org.antry.controller;
  2. import org.antry.service.UserService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. /** * @ClassName UserHandler * @Description TODO * @Autor T_Antry * @Date 2020/11/11 11:32 * @Version 1.0 */
  9. @RestController
  10. @RequestMapping("user")
  11. public class UserHandler {
  12. @Autowired
  13. private UserService userService;
  14. @GetMapping("/{id}")
  15. public String doGet(@PathVariable Long id){
  16. return userService.doGetUser(id);
  17. }
  18. }

我们可以跑起来测试一下这个custom工程,现在还不是微服务,只是一个普通的spring boot 的工程。
运行起来之后,我们在url中输入 http://localhost:9001/user/123
我们就能够看到:

在这里插入图片描述

接下里,我们配置同样的步骤,也让user-service的工程也能够这样访问。当然,端口配置不同的端口,例如我这边就把它配置成1001,如下:

  1. server:
  2. port: 1001
  3. spring:
  4. application:
  5. name: user-service

测试结果如下:
在这里插入图片描述

3.6 开始入手微服务

我们现在呢就是做到去访问custom-service服务器去访问user-service
那么我们是通过Restemplate去访问的,一开始我看到RestTemplate是非常懵逼的,这其实就是一个类,也不是什么东西,现在还不必太过纠结这是什么东西。只需要知道,我们用这个类的对象来拿到其它服务器的执行结果就可以了。
这次测试我们不需要修改user-service的工程,我们只需要修改custom-service的handler,以及启动类。
启动类新增Template配置

  1. package org.antry;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.web.client.RestTemplate;
  6. @SpringBootApplication
  7. public class CustomService {
  8. public static void main(String[] args) {
  9. SpringApplication.run(CustomService.class, args);
  10. }
  11. @Bean
  12. public RestTemplate restTemplate(){
  13. return new RestTemplate();
  14. }
  15. }

修改handler类
这个控制层,我们原本调用的是custom-service这台服务器自己本身的服务,那么我们现在通过写死的url去访问user-service,并拿到结果再返回。

  1. package org.antry.controller;
  2. import org.antry.service.UserService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. /** * @ClassName UserHandler * @Description TODO * @Autor T_Antry * @Date 2020/11/11 11:32 * @Version 1.0 */
  10. @RestController
  11. @RequestMapping("user")
  12. public class UserHandler {
  13. @Autowired
  14. private RestTemplate restTemplate;
  15. @Autowired
  16. private UserService userService;
  17. @GetMapping("/{id}")
  18. public String doGet(@PathVariable Long id){
  19. String url = "http://127.0.0.1:1001/user/"+id;
  20. return "<div><h1>custom-Service回应:</h1><div>"+restTemplate.getForObject(url, String.class);
  21. // return userService.doGetUser(id);
  22. }
  23. }

此时大家通过custom-service,就可以访问到user-service
在这里插入图片描述
以上的部分呢,其实我们已经可以看到服务之间可以通过这样的方式去联系,我想大家通过实践也能有自己的一些理解。
那么我也会把这一部分的源码上传,现在的这种方式当然大家也会认为它的不便捷,还要写死url等问题,我将会在进行解释,喜欢就点个赞吧。
本次例程资源
下一章
Spring cloud 理论+实践+解析 手摸手带你一起搭建(二 Eureka服务注册中心)

发表评论

表情:
评论列表 (有 0 条评论,124人围观)

还没有评论,来说两句吧...

相关阅读

    相关 服务Spring Cloud学习

    Spring Cloud是一个基于Springboot实现的微服务架构开发工具。它为微服务架构中设计的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策...