SpringCloud+SpringCloud Alibaba+MyBatis-Plus

SpringCloud+SpringCloud Alibaba+MyBatis-Plus

一、简介

  1. SpringCloud 的几大痛点

    SpringCloud 部分组件停止维护和更新,给开发带来不便;

    SpringCloud 部分环境搭建复杂,没有完善的可视化界面,我们需要大量的二次开发和定制

    SpringCloud 配置复杂,难以上手,部分配置差别难以区分和合理应用

  2. SpringCloud Alibaba 的优势:

    阿里使用过的组件经历了考验,性能强悍,设计合理,现在开源出来大家用

    成套的产品搭配完善的可视化界面给开发运维带来极大的便利

    搭建简单,学习曲线低。

  3. 结合 SpringCloud Alibaba 我们最终的技术搭配方案:

    SpringCloud Alibaba - Nacos:注册中心(服务发现/注册)

    SpringCloud Alibaba - Nacos:配置中心(动态配置管理)

    SpringCloud - Ribbon:负载均衡

    SpringCloud - Feign:声明式 HTTP 客户端(调用远程服务)

    SpringCloud Alibaba - Sentinel:服务容错(限流、降级、熔断)

    SpringCloud - Gateway:API 网关(webflux 编程模式)

    SpringCloud - Sleuth:调用链监控

    SpringCloud Alibaba - Seata:原 Fescar,即分布式事务解决方案

二、SpringBoot、SpringCloud、SpringCloudAlibaba

版本依赖关系的官网:https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明

	 
      <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-dependenciesartifactId>
        <version>2.2.5.RELEASEversion>
        <type>pomtype>
        <scope>importscope>
      dependency>
      
      <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-dependenciesartifactId>
        <version>Hoxton.SR3version>
        <type>pomtype>
        <scope>importscope>
      dependency>
      
      <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-alibaba-dependenciesartifactId>
        <version>2.2.1.RELEASEversion>
        <type>pomtype>
        <scope>importscope>
      dependency>

三、Nacos

​ Nacos 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理

​ 平台。他是使用 java 编写。需要依赖 java 环境

​ Nacos 文档地址: https://nacos.io/zh-cn/docs/quick-start.html

​ https://nacos.io/zh-cn/docs/what-is-nacos.html

  1. nacos依赖

<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency> 


<dependency>
     <groupId>com.alibaba.cloudgroupId>
     <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
 dependency>
  1. application.yml配置

    spring:
      application:
        name: #nacos注册名字
        
     #注册中心的配置
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848 #配置Nacos地址
    
  2. bootstrap.yml

    spring:
    #配置中心的配置
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848 #Nacos服务注册中心地址
          config:
            server-addr: localhost:8848 #Nacos作为配置中心地址
            file-extension: yaml #指定yaml格式的配置
    

  3. 加@EnableDiscoveryClient注解

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

四、Feign远程调用

  1. 导依赖

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-openfeignartifactId>
    dependency>
    
  2. 在启动类加上@EnableFeignClients注解

  3. 编写接口,进行远程调用

    @FeignClient("stores") 
    public interface StoreClient { 
      @RequestMapping(method = RequestMethod.GET, value = "/stores") 
      List<Store> getStores();
                                                         
      @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json") 
      Store update(@PathVariable("storeId") Long storeId, Store store); 
    }
    

五、Gateway

  1. 引入依赖

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-gatewayartifactId>
    dependency>
    
    1. 编写路由规则-application.yml
​```yaml
server:
  port: 88

spring:
  application:
    name: gulimall-geteway

  cloud:
    nacos:
      discovery:
        server-addr: 192.168.1.11:8848  #配置Nacos地址

    gateway:
      routes:
#        - id: test_route
#          uri: https://ww.baidu,com
#          predicates:
#            - Query=url,baidu

        - id: product_route
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?.*),/$\{segment}

        - id: admin_route         # 路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: lb://renren-fast   # lb:负载均衡  匹配后提供服务的路由地址
          predicates:
            - Path=/api/**        # 断言,路径相匹配的进行路由
          filters:
            - RewritePath=/api/(?.*),/renren-fast/$\{segment}  # 重写路径
​```

​

MyBatis-Plus配置

  1. 导入依赖

    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-boot-starterartifactId>
        <version>3.4.2version>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>8.0.23version>
    dependency>
    
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>1.18.18version>
    dependency>
    
  2. 在application.yml中添加配置

 spring:
   datasource:
     username: root
     password: root
     driverClassName: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://192.168.1.11:3306/mall_sms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai

  mybatis-plus:
    mapperLocations: classpath:mapper/**/*.xml  #数据访问层.xml文件路径
    global-config:
      db-config:
        id-type: auto   #自动生成填充主键id
        logic-delete-field: 1  #1代表删除
        logic-not-delete-value: 0 #0代表没有删除
  1. 在启动类上加@MapperScan(“对应接口dao层的包”)
  2. 编写mybatis配置文件
package com.lyh.mall.product.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement //开启事务
@MapperScan("com.lyh.mall.product.dao")
public class MyBatisConfig {

	/**
     * 分页插件
     */
    // 旧版
    /*@Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }*/

    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

你可能感兴趣的:(Spring全家桶等技术,spring,cloud,spring,cloud,alibaba)