Nacos:文档 https://nacos.io/zh-cn/docs/what-is-nacos.html
Install:https://blog.csdn.net/weixin_40939212/article/details/108337893
测试:http://localhost:8848/nacos(登录:nacos/nacos)
服务提供者配置(三部曲)*N(端口不一样的多台)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
server:
port: 9001
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
#nacos 服务地址
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
#注意这里的* 需要加上引号 不然启动报错
include: '*'
@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
}
服务提供者 9001/9002 application.name= nacos-provider
1、Consumer配置三部曲(与服务提供者一致,端口83)
2、由于集成了Ribbon做负载均衡,使用了RestTemplate做微服务发送,所以要增加RestTemplate的配置类
@Configuration
public class ApplictationContextConfig {
@Bean
@LoadBalanced//如果有多台服务提供者 不可忘加
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3、调用
@RestController
public class NacosConsumerController {
private static final String URL = "http://nacos-provider";//服务提供方
@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/consumer/{string}")
public String echo(@PathVariable String string) {
return "NacosConsumerController: " + restTemplate.getForObject(URL + "/provider/" + string, String.class);
}
5、测试 http://localhost:83/consumer/nacos
官网:https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-config
基于注册中心基础上,增加相关config的配置
1、pom
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
2、新增bootstrap.yml配置文件
Nacos与config一样,在初始化的时候,先从配置中心拉取配置。而在springboot中配置文件的优先级顺序,bootstrap 要高于application。相同的内容会被bootstrap中的覆盖。
server:
port: 83
spring:
application:
name: nacos-config
profiles:
active: dev
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
discovery:
#nacos \u670D\u52A1\u5730\u5740
server-addr: localhost:8848
3、Nacos配置中心增加配置文件
4、使用@RefreshScope进行配置刷新
@RestController
@RefreshScope//自动刷新注解
public class NacosConfigController {}
1、namespace 开发环境隔离
2、Group 可以把不同的微服务划分为同一组
3、Service 就是微服务集群
nginx(集群) 反向代理 Nacos集群,Nacos集群连接同一个数据库进行持久化
微服务注册地址变成nginx
cloud:
nacos:
config:
#集群部署 地址修改为nginx代理地址
server-addr: localhost:8848
file-extension: yaml
discovery:
#集群部署 地址修改为nginx代理地址
server-addr: localhost:8848
Sentinel 的主要特性:
1、下载安装Sentinel
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>2.2.3.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
<version>1.8.0version>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
server:
port: 8401
spring:
application:
name: sentinel-provider
cloud:
nacos:
discovery:
#nacos 服务注册地址
server-addr: localhost:8848
sentinel:
transport:
#sentinel dashboard 地址
dashboard: localhost:8080
management:
endpoints:
web:
exposure:
include: '*'
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelApplication.class, args);
}
}
@RestController
public class SentinelController {
@RequestMapping("/sentinel/testA")
public String testA() {
return "AAAAAAAAAA";
}
@RequestMapping("/sentinel/testB")
public String testB() {
return "BBBBBBBBBB";
}
}
在nacos配置管理中增加配置,配置会持久化到nacos的持久化数据库mysql
Seata 是一个用于微服务架构的分布式事务解决方案,它的特点是高性能且易于使用,旨在实现简单并快速的事务提交与回滚。
从传统的单体应用说起,假设一个单体应用的业务由 3 个模块构成,三者使用单个本地数据源。
这样的话本地事务很自然就可以保证数据一致性。
但是在微服务架构中就不这么简单了,这 3 个模块被设计为 3 个不同数据源之上的 3 个服务,每个服务对应一个数据库。
本地事务当然也可以保证每个服务中的数据一致性,但是扩展到整个应用、整个业务逻辑范围来看,情况如何呢?
Seata 就是用于解决上述微服务架构中的事务问题的解决方案。
如下图所示,分布式事务是一个全局事务(Global Transaction),由一批分支事务(Branch Transation)组成,通常分支事务只是本地事务。
Seata 中有三大基本组件:
Seata 管理分布式事务的典型生命周期:
两阶段提交协议的演变:
在数据库本地事务隔离级别 读已提交(Read Committed) 或以上的基础上,Seata(AT 模式)的默认全局隔离级别是 读未提交(Read Uncommitted) 。
如果应用在特定场景下,必需要求全局的 读已提交 ,目前 Seata 的方式是通过 SELECT FOR UPDATE 语句的代理。
分支提交或回滚。TC 驱动 XID 的相应全局事务下的所有分支事务,完成分支提交或回滚。
两阶段提交协议的演变:
在数据库本地事务隔离级别 读已提交(Read Committed) 或以上的基础上,Seata(AT 模式)的默认全局隔离级别是 读未提交(Read Uncommitted) 。
如果应用在特定场景下,必需要求全局的 读已提交 ,目前 Seata 的方式是通过 SELECT FOR UPDATE 语句的代理。