springCloud的学习

七大组件

Nacos 注册中心 (服务接口进行注册/发现)

Nacos 配置中心 (每个服务的配置可以在nacos页面上进行动态配置)

OpenFeign 声明式Http客户端(进行nacos上配置的接口进行使用)

Sentinel: 服务容错

GateWay: API网关

Sleuth: 调用链监控

Seata: 分布式事务解决方案

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

pom 复制代码
<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包引入(也就是每个模块都需要)

pom 复制代码
<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  </dependency>

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

ymal 复制代码
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

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

java 复制代码
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {

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

}

OpenFeign的配置与使用

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

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

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

java 复制代码
@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包,用于放远程调用的接口

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

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

nacos配置文件的设置

①common的pom中引入该依赖

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

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

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之前经过的过滤

java 复制代码
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}
相关推荐
王解11 分钟前
CTF学习
学习
AI视觉网奇13 分钟前
PlayerStreaming 驱动audio2face 学习笔记
笔记·学习·ue5
enjoy编程16 分钟前
Spring-AI 脱离 IDE 的束缚:OpenCode 让 AI 开发回归终端本源
人工智能·spring·ai·claude·gemini·claude code·opencode
承渊政道16 分钟前
Linux系统学习【Linux基础指令以及权限问题】
linux·服务器·学习
Engineer邓祥浩20 分钟前
设计模式学习(11) 23-9 组合模式
学习·设计模式·组合模式
专注于大数据技术栈21 分钟前
java学习--什么是线程安全和不安全
java·学习·安全
Engineer邓祥浩22 分钟前
设计模式学习(13) 23-11 享元模式
学习·设计模式·享元模式
南山十一少23 分钟前
最新款2025版的IDEA的下载、注册以及进行spring boot 工程和spring cloud工程的搭建和使用
spring boot·spring cloud·intellij-idea
week_泽24 分钟前
第3课:构建AI代理系统面临的挑战 - 学习笔记_3
人工智能·笔记·学习·ai agent
week_泽26 分钟前
第8课:LangGraph Memory管理机制与实现方案 - 学习笔记_8
java·笔记·学习·ai agent