1、使用nacos(注册中心)
1、下载
2、启动
在\nacos\bin目录下鼠标双击startup.cmd启动
3、访问测试
http://127.0.0.1:8848/nacos/index.html
账号密码都是nacos
登录成功就可以了
4、配置maven依赖
<!--Spring Cloud 其中springcloud的版本要和springboot版本相对应-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
5、在application.properties中添加服务发现地址和自己都服务名
spring.application.name=service-vod
#nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
6、在启动类上加注解
@EnableDiscoveryClient //nocas服务注册发现
@EnableDiscoveryClient //nocas服务注册发现
public class VodApplication {
public static void main(String[] args) {
SpringApplication.run(VodApplication.class, args);
}
}
2、使用feign
使用feign可以拿到注册在nacos的微服务
1、导入maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--服务调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
2、在启动类上加注解
@EnableFeignClients //调用nacos注册的微服务
3、使用
1、创建接口
创建接口,里面调用的远程接口跟远程微服务的控制层方法一模一样
import com.tuzhi.utilcommon.result.Result;
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;
/**
* @program: guli_parent
* @description: 调用远程的Vod微服务
* @author: 兔子
* @create: 2022-04-16 15:24
**/
//name=服务名称
@FeignClient(name = "service-vod")
@Component
public interface VodClient {
//定义调用的方法路径,方法路径已经要写全
//根据视频id删除阿里云视频
//@PathVariable注解一定要指定参数名称,否则出错
@DeleteMapping("/eduvod/{videoId}")
public Result deleteVideo(@PathVariable("videoId") String videoId);
}
2、使用@Autowired注入正常使用
3、hystrix使用
熔断器,用于服务器宕机和延长请求时间
1、导入maven依赖
<!--hystrix依赖,主要是用 @HystrixCommand -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
2、添加application.properties配置
#开启熔断机制
feign.circuitbreaker.enabled=true
# 设置hystrix超时时间,默认1000ms
#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
3、使用
1.在feign接口的直接上加fallback
//name=服务名称
@FeignClient(name = "service-vod", fallback = VodFileDegradeFeignClient.class)
@Component
public interface VodClient {
//定义调用的方法路径,方法路径已经要写全
//根据视频id删除阿里云视频
//@PathVariable注解一定要指定参数名称,否则出错
//删除单个视频
@DeleteMapping("/eduvod/{videoId}")
Result deleteVideo(@PathVariable("videoId") String videoId);
//删除多个视频
@DeleteMapping("/eduvod/deleteBatch")
public Result deleteVideoBatch(@RequestParam("list") List<String> list);
2.创建一个类基础frign的接口
@Component
public class VodFileDegradeFeignClient implements VodClient {
@Override
public Result deleteVideo(String videoId) {
System.out.println("执行了熔断删除视频出错了");
return Result.error().message("删除视频出错了");
}
@Override
public Result deleteVideoBatch(List<String> list) {
System.out.println("执行了熔断删除多个视频出错了");
return Result.error().message("删除多个视频出错了");
}
}