Spring Cloud Alibaba-(2)Nacos【服务注册与发现、配置管理】
Spring Cloud Alibaba-(1)搭建项目环境
Spring Cloud Alibaba-(2)Nacos【服务注册与发现、配置管理】
Spring Cloud Alibaba-(3)OpenFeign【服务调用】
Spring Cloud Alibaba-(4)Sentinel【流控和降级】
Spring Cloud Alibaba-(5)Seata【分布式事务】
1.Nacos 官网-https://nacos.io/
2.下载对应版本的Nacos
3.必须使用 bootstrap.yml 配置 nacos
3.1 订单微服务 bootstrap.yml 配置 nacos
server:port: 8000spring:application:name: orders-servicedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springcloud2024username: ***password: ***cloud:nacos:discovery:server-addr: http://localhost:8848config:server-addr: http://localhost:8848
3.2 产品微服务 bootstrap.yml 配置 nacos
server:port: 8400spring:application:name: orders-servicedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springcloud2024username: ***password: ***cloud:nacos:discovery:server-addr: http://localhost:8848config:server-addr: http://localhost:8848
3.3 用户微服务 bootstrap.yml 配置 nacos
server:port: 8300spring:application:name: orders-servicedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springcloud2024username: ***password: ***cloud:nacos:discovery:server-addr: http://localhost:8848config:server-addr: http://localhost:8848
4. 订单、产品、用户微服务 pom.xml 引入依赖
<!-- nacos-discovery 服务注册与发现 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency><!-- nacos-config 配置中心 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
5.standalone 模式启动 Nacos
6.启动微服务,会自动将微服务注册到Nacos
7.进入Nacos-DashBoard查看(http://localhost:8848/nacos)
8.Nacos 配置管理
8.1 Nacos 使用 mysql 持久化,配置完成后重启Nacos
8.2 Nacos 配置默认是Properties格式,如果使用yaml格式,要在bootstrap配置
8.2.1 Nacos创建2个配置文件
8.2.2 bootstrap.yml 配置
8.3 @Value 注解获取Nacos配置值,@RefreshScope 动态刷新获取Nacos修改的配置值
package com.dragon.controller;import cn.dev33.satoken.util.SaResult;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.dragon.openfeign.ProductServiceFeign;
import com.dragon.service.IOrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;@Tag(name = "订单微服务")
@Slf4j
@RefreshScope
@RestController
@RequestMapping("/order")
public class OrdersController {@Autowiredprivate IOrdersService ordersService;@Qualifier("com.dragon.openfeign.ProductServiceFeign")@Autowiredprivate ProductServiceFeign productServiceFeign;@Value("${env.name}")private String envName;@Operation(summary = "获取 Nacos 配置值")@GetMapping("/config")public String config(){return envName;}@Operation(summary = "下单")@PostMapping("/add/{productId}")public SaResult add(@PathVariable("productId") String productId){return ordersService.add(productId);}@Operation(summary = "测试调用产品的 test() 出现异常")@GetMapping("/test")public SaResult test(){return productServiceFeign.test();}// /**
// * 热点规则,必须使用 @SentinelResource,
// * @SentinelResource 优先级高于全局流控异常处理器
// */
// @GetMapping("/get/{id}")
// @SentinelResource(value = "getById",blockHandler = "hotBlockHandler")
// public String getById(@PathVariable("id") String id){
// return "正常访问";
// }
//
// public String hotBlockHandler(@PathVariable("id") String id,BlockException e){
// return "热点异常处理器";
// }}