nacos
概念
Nacos 是阿里巴巴一个开源项目,服务注册与发现的,统一配置,服务管理的平台!
官网地址:https://nacos.io/en-us/docs/quick-start.html (学习地方)
github地址:https://github.com/alibaba/nacos (环境搭建)
直接从github下载即可!
1、解压到你自己的环境目录
2、启动服务
3、启动成功!
访问:http://localhost:8848/nacos/index.html
默认的用户名和密码都是 nacos : nacos
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.9.0.RELEASE</version> </dependency>
2、编写配置文件
1 2 | # Nacos 配置 spring.cloud.nacos.discovery.server-addr= 127.0 . 0.1 : 8848 |
3、添加注解
1 2 3 4 5 6 7 8 9 10 | // @EnableEurekaClient // 只有Eureka可以用 // 通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能 @SpringBootApplication @ComponentScan (basePackages = { "com.coding.edu" , "com.coding.common" }) @EnableDiscoveryClient public class EduApplication { public static void main(String[] args) { SpringApplication.run(EduApplication. class ,args); } } |
4、启动测试,成功注册!
回顾Feign:
Feign:声明式、模板化、Http客户端! 作用: 微服务之间调用HTTP api!
Feign:Ribbon(负载均衡,客户端),Hystrix (服务降级!) 无缝集成这两个!
1、在调用端集成Feign
<!-- 添加feign支持 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.2.RELEASE</version> </dependency>
2、启动类增加feign的支持
@SpringBootApplication @ComponentScan(basePackages = {"com.coding.edu","com.coding.common"}) @EnableDiscoveryClient @EnableFeignClients // feign的支持 public class EduApplication { public static void main(String[] args) { SpringApplication.run(EduApplication.class,args); } }
3、编写 调用接口
-
推荐习惯 client 包
-
在这里编写远程的调用接口
package com.coding.edu.client; import com.coding.common.vo.R; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient("coding-vod") @Component public interface VodClient { // 直接调用服务中的请求 @DeleteMapping("/admin/vod/video/{videoId}") public R removeVideo(@PathVariable("videoId") String videoId); }
4、完善业务的调用
package com.coding.edu.service.impl;
import com.coding.common.constants.ResultCodeEnum;
import com.coding.common.handler.CodingException;
import com.coding.edu.client.VodClient;
import com.coding.edu.entity.Video;
import com.coding.edu.form.VideoInfoForm;
import com.coding.edu.mapper.VideoMapper;
import com.coding.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.netflix.client.ClientException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
/**
* <p>
* 课程视频 服务实现类
* </p>
*
* @author Coding
* @since 2020-04-05
*/
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
// 远程接口
@Autowired
private VodClient vodClient;
// ..
@Override
public void removeVideoById(String id) {
//删除视频资源
Video video = baseMapper.selectById(id);
// 获取远端的视频id
String sourceId = video.getVideoSourceId();
if (!StringUtils.isEmpty(sourceId)){
vodClient.removeVideo(sourceId); // 调用vod微服务的方法,删除云端视频
}
baseMapper.deleteById(id);
}
}
1、导入依赖
1 2 3 4 5 6 7 8 9 10 11 12 | <!-- 添加hystrix支持 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version> 2.2 . 2 .RELEASE</version> </dependency> <!-- 添加ribbon支持 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version> 2.2 . 2 .RELEASE</version> </dependency> |
2、配置中开启支持
1 2 | # 开启熔断机制 feign.hystrix.enabled= true |
3、编写降级方法
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.coding.edu.client; import com.coding.common.vo.R; import org.springframework.stereotype.Component; // Hystrix的Fallback处理 @Component public class VodClientHystrixFallback implements VodClient { @Override public R removeVideo(String videoId) { return R.error().message( "VodClientHystrixFallback...执行中" ); } } |
4、失败回调对应的方法
1 2 3 4 5 6 7 8 9 10 | // fallback 失败调用的类 @FeignClient (name = "coding-vod" ,fallback = VodClientHystrixFallback. class ) @Component public interface VodClient { // 直接调用服务中的请求 @DeleteMapping ( "/admin/vod/video/{videoId}" ) public R removeVideo( @PathVariable ( "videoId" ) String videoId); } |
5、如果程序在预期的结果出现异常,则会调用 hystrix 的 fallback 方法!实现服务降级!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?