教你手把手搭建springcloud项目

前言

纸上得来终觉浅,绝知此事要躬行。
复制代码

描述

方便自己做个笔记,大家有需要也可以康康。
人处于学习的状态是最充实的,下面讲解和搭建一下springcloud的几个组件和功能,
这里将会搭建nacos、gateway、feign、Hystrix
复制代码

面试必问

  • 什么是微服务
  • 服务之间如何调用
  • springcloud和springcloud的区别
  • 什么是服务熔断、降级等等等

nacos

image.png

什么是nacos,从哪里下载nacos,请移步官方文档:[https://nacos.io/zh-cn/docs/what-is-nacos.html](url)
进入到nacos的目录nacos-server-1.1.4\nacos\conf
在application.conf里面执行nacos-mysql.sql文件,在自己对应的数据库建立好
进入到nacos-server-1.1.4\nacos\bin双击startup.cmd  nacos就启动了。
复制代码

gateway

image.png

1. 创建好maven的父工程,这里是我的父工程引入的依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
</parent>
<dependencyManagement>
    <dependencies>
        <!-- 此管理不加 会出问题【 Spring Boot [2.0.6.RELEASE] is not compatible with this Spring Cloud release train】-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>0.2.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>   <!-- 将jar统一打进lib目录下 -->
            <directory>src/main/resources/lib</directory>
            <targetPath>BOOT-INF/lib</targetPath>
        </resource>
    </resources>

    <!-- 配置打包及打包后的项目存放路径 outputDirectory -->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <finalName>${project.name}</finalName>
                <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                <outputDirectory>E:/compose/squarepay</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>
复制代码

2. 然后再创建一个子工程,我这里命名为child-gateway

image.png
gateway引入的依赖:


<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <!--actuator 监控-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

好了,依赖都引入完了,接下来就是我们的application了,加入相对应的注解。
复制代码

image.png

3. 接下来我们启动nacos和gateway服务

image.png

image.png
可以看到,gateway服务注册到nacos里面去了,至于gateway的很多功能,路由,安全,过滤,流控等功能,个人认为就是类似于单体结构的拦截器,本来也是拆出去的,我觉得这样理解就好,有时间大家可以多去尝试。

feign,Hystrix

引入的依赖:

<dependency>
    <groupId> org.springframework.cloud </groupId >
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId >
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
复制代码

搭建好两个服务,一个provider一个consumer。
我这里搭建的两个服务

image.png
这个是我consumer做的操作
image.png
consumer:controller
image.png
熔断可以把另一个服务关了测试。

熔断:

image.png

image.png

个人理解

eureka -> nacos
zuul -> gateway
feign -> openfeign
Hystrix ->Sentinel
微服务也在不断的更新迭代,我们需要不断学习。
复制代码

结尾

个人刚写没多久,有错误请多指正,欢迎交流,我也会虚心请教。
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享