七大组件
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}