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、编写配置文件
# Nacos 配置 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
3、添加注解
// @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、导入依赖
<!-- 添加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、配置中开启支持
# 开启熔断机制 feign.hystrix.enabled=true
3、编写降级方法
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、失败回调对应的方法
// 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 方法!实现服务降级!