当前位置: 首页 > article >正文

springCloud的学习

七大组件

Nacos 注册中心 (服务接口进行注册/发现)
Nacos 配置中心 (每个服务的配置可以在nacos页面上进行动态配置)
OpenFeign 声明式Http客户端(进行nacos上配置的接口进行使用)
Sentinel: 服务容错
GateWay: API网关
Sleuth: 调用链监控
Seata: 分布式事务解决方案

在common包中的pom进行cloud依赖管理

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.5.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

则后续的cloud依赖不用设置版本

nacos发现与注册的配置与使用

① 下载nacos服务(本地开发windows下载直接启动,正常项目则是linux版本然后后台启动),然后启动

② common包引入(也就是每个模块都需要)

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

③ 再去相应的模块进行nacos地址的配置
地址的设置与该模块名的设置

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/gulimall_ums
    driver-class-name: com.mysql.jdbc.Driver
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: gulimall-member

④每个模块的主进程上方添加注解(服务发现注解)

@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {

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

}

OpenFeign的配置与使用

那个模块需要远程调用别人,则引入该依赖

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

① 在主程序上添加OpenFeign的注解

@EnableFeignClients(basePackages = "com.atguigu.gulimall.member.feign")
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {

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

}

② member模块远程调用coupon模块
创建一个feign包,用于放远程调用的接口

@FeignClient("gulimall-coupon")//声明式对coupon接口的远程调用
public interface CouponFeignService {
    @RequestMapping("/coupon/coupon/member/list")//这是coupon该接口的完整路径
    public R membercoupons();
}

③ 在member自己的方法中,注入该sevice,然后使用该接口即可

nacos配置文件的设置

①common的pom中引入该依赖

        <!--        nacos-配置中心来做配置管理-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

② 在需要nacos配置文件的模块创建bootstrap.properties,会优先于本地的配置文件

spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848

③ 在配置中心添加 当前 模块名.properties的配置文件(配置级)

命名空间

① 环境的方式进行隔离
在不同的环境下存放不同的配置文件(进行环境隔离)
在bootstrap.properties 配置使用那个命名空间

②模块之间进行隔离

配置级

所有的配置的集合

加载多个配置级
在这里插入图片描述
bootstrap.properties中进行设置
在这里插入图片描述

配置分组(同一命名空间的不同分组)

默认所有的配置级都属于: DEFAULT_GROUP;

gateway网关设置

- 代表的是一组设置
id 为名字
uri 为要路由的地方
predicates为断言,满足则可达到uri

filter 在到达uri之前经过的过滤

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: https://www.baidu.com
          predicates:
            - Query=url,baidu
      #  这个断言机制是 当访问网络参数带有 url且值为baidu时,则条约到uri
        - id: qq_route
          uri: https://www.qq.com
          predicates:
            - Query=url,qq

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

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}


原文地址:https://blog.csdn.net/please93/article/details/146353325
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/594297.html

相关文章:

  • 强大的AI网站推荐(第一集)—— Devv AI
  • 若依框架入门指南:快速上手SpringBoot+前后端分离版
  • 深入理解 Re-parameterizable RegionText Alignment (RepRTA) 技术
  • 【数学建模】TOPSIS法简介及应用
  • Neo4j GDS-04-图的中心性分析介绍
  • 第29周 面试题精讲(2)
  • helm部署metricbeat
  • Fiddler查看响应时间
  • django入门教程之request和reponse【二】
  • Bash中关于制表符\t站位情况说明
  • Verilog-HDL/SystemVerilog/Bluespec SystemVerilog vscode 配置
  • 【 Kubernetes 风云录 】- Istio的一致性哈希机制
  • Flutter 学习之旅 之 flutter 使用 SQLite(sqflite) 实现简单的数据本地化 保存/获取/移除/判断是否存在 的简单封装
  • 【自定义微信小程序拉下选择过滤组件】searchable-select
  • 每日OJ_牛客_MT1最大差值_模拟+贪心_C++_Java
  • 超详细正则表达式逐字解析!!!
  • 更改 docker0 IP
  • 【python】OpenCV—Template Matching
  • 科技查新和查收查引有什么区别?
  • 9、Python collections模块高效数据结构