springcloud学习

本文介绍了SpringCloud作为微服务架构的实现方式,包括服务注册与发现、服务调用、服务熔断、负载均衡等核心组件。通过详细步骤展示了如何从创建Maven工程开始,搭建SpringBoot微服务,并使用SpringCloud进行管理,最后总结了创建SpringCloud项目的基本流程。

springcloud


一、微服务架构

微服务架构说一种架构模式,他提倡将单一应用程序划分成一组小的服务,服务之间互相协调、互相配合,为用户提供最终的价值。每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制互相协作(通常是基于HTTP协议的RESTFUL API)。每个服务都是围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外,应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语言、工具对其进行构建。

springboot就可以将一个个的功能模块当成一个微服务,用springcloud进行管理连接这些微服务,构建成一个整的项目。

二、springCloud的维度

  • 服务注册与发现:EUREKA
  • 服务调用:NETFLIX OSS RIBBON或NETFLIX FEIGN
  • 服务熔断:HYSTRIX
  • 负载均衡:
  • 服务降级:HYSTRIX
  • 服务消息队列
  • 配置中心管理
  • 服务网关:NETFLIX Zuul
  • 服务监控
  • 全链路追踪
  • 自动化构建部署
  • 服务定时任务调度操作

服务分布式配置:spring cloud Config
服务开发:spring boot

三、springcloud和springboot的关系

  • springboot专注于快速方便的开发单个个体微服务。
  • springCloud是关注全局的微服务线条整理治理框架,他将springboot开发的一个个单体微服务整合并管理起来,为各个微服务之间提供:配置管理,服务发现,断路器,路由,微代理,事件总线,全局锁,决策竞选,分布式会话等等集成服务。
  • springboot可以离开springcloud独立使用,开发项目,但是springcloud离不开springboot,属于依赖关系。
  • springboot专注于快速、方便的开发单个个体微服务。springCloud关注全局的服务治理框架。

二、使用步骤

1.创建maven工程——搭建环境

在这里插入图片描述
修改字符编码:
在这里插入图片描述
注解生效激活:
在这里插入图片描述
java编译版本:
在这里插入图片描述

2.选择springcloud和springboot的版本

在这里插入图片描述

3.修改父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>8.0.22</mysql.version>
    <druid.version>1.1.16</druid.version>
    <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
  </properties>

  <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version  -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.4.2</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2020.0.1</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>

4.mvn:install将父工程发布到仓库方便子工程继承

在这里插入图片描述
注:若没报错,则证明父项目和其maven环境没有问题,可以往下写!

5.创建提供实体类的项目共同的api的module

5.1创建module

在这里插入图片描述

5.2编写pom

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <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>

        <!-- 时间格式-->
        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>

5.3编写实体类和返回结果集类

在这里插入图片描述

@Data                               //自动生成get、set、toString的方法
@NoArgsConstructor                  //空参构造
@AllArgsConstructor                 //全参构造
public class CommonResult<T> {

    //404 not_found data
    private Integer code;           //返回结果代码
    private String message;         //返回结果信息
    private T data;                 //返回结果属性

    public CommonResult(Integer code,String message){
        this(code,message,null);
    }

}

6、编写服务提供者module

6.1创建module

在这里插入图片描述

6.2改pom文件

<dependencies>
        <!-- 引入自己定义的api通用包,可以使用通用包内的实体Entity-->
        <dependency>
            <groupId>cn.ffcs.springcloud</groupId>
            <artifactId>springcloud-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>
            <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>
        <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>
        <!-- lombok简化代码-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- springboot集成的单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

6.3写yml配置文件

server:
  port: 8001 #服务端口

spring:
  application:
    name: cloud-payment-service #服务名
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource  #当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动包
    url: jdbc:mysql://localhost:3306/cloud?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    username: root
    password: root

devtools:
  restart:
    enabled: true #是否支持热部署

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: cn.ffcs.springcloud.entities  #所有entity别名所在包

6.4编写主启动

package cn.ffcs.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);
    }

}

6.5编写业务类

在这里插入图片描述

7、编写消费者module

7.1创建moudle

在这里插入图片描述

7.2改pom文件

<dependencies>
        <!-- 引入自己定义的api通用包,可以使用通用包内的实体Entity-->
        <dependency>
            <groupId>cn.ffcs.springcloud</groupId>
            <artifactId>springcloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- springboot的图形化监控展现(每个module的pom中的依赖必须要有)-->
        <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>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

7.3写yml

server:
  port: 80

7.4编写主启动

package cn.ffcs.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}

7.5编写业务类

在这里插入图片描述
编写消费者模块只需要controller类,并使用RestTemplate去调用提供者类的方法

ApplicationContextConfig.java

package cn.ffcs.springcloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 *程序上下文配置
 */

@Configuration
public class ApplicationContextConfig {

    //客户端模板工具集对象
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

OrderController.java

package cn.ffcs.springcloud.controller;

import cn.ffcs.springcloud.entities.CommonResult;
import cn.ffcs.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderController {

    public static final String PAYMENT_URL = "http://localhost:8001";

    /**
     * 用这个对象去找8001端口下的服务
     */
    @Autowired
    private RestTemplate restTemplate;

    //客户端发的都是get请求
    @GetMapping("/consumer/payment/addPayment")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/addPayment",payment,CommonResult.class);
    }

    @GetMapping("/consumer/payment/getPaymentById/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") long id){
        return restTemplate.getForObject(PAYMENT_URL+"payment/getPaymentById/"+id,CommonResult.class);
    }
}

注:客户端发送的都是getMapping请求


总结

创建一个基本的spingcloud项目步骤:

  1. 创建一个普通maven项目,将src目录删除,将其用做父模块。
  2. 对maven项目进行相应的设置。(字符编码、注解生效激活、java编译版本确认);
  3. 创建api模块。(创建module、改pom、写业务类(只有entities,提供共同的实体类和返回结果集类));
  4. 创建提供者模块。(创建module、改pom、写yml、写主启动、写业务类(controller、service、dao));
  5. 创建消费者模块。(创建module、改pom、写yml、写主启动、写业务类(config(将RestTemplet对象创建并注入到ioc容器)、controller);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值