执行流程:
nacos注册=>
openfeign 服务调用=>
hystrix(熔断:被调用的服务挂了,执行)=>
ribbon(负载均衡)=>
http client(根据服务名找到路径最后执行方法)
要用到的依赖:
<!--spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--服务注册 alibaba-nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--服务调用 openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<!--hystrix依赖,主要是用 @HystrixCommand -->
<!--熔断器 hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<!--负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
windows中使用
一、下载nacos (github 上下载 1.1.4版本)
说明:nacos服务注册中心,实现服务与服务之间的访问
二、访问
localhost:8848/nacos
默认登入账号密码:nacos nacos
三、maven中引入spring-cloud和nacos依赖
spring-cloud的版本选择查看spring官网(要适合spring boot的版本)
我的spring-boot版本是 2.3.X
<!--spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--服务注册-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
四、配置文件 application.yaml 配置
spring:
application: #配置服务的名称
name: admin-service
cloud: #连接nacos
nacos:
discovery:
server-addr: 127.0.0.1:8848
五、启动类中加入注解
@EnableDiscoveryClient
最后、启动服务 在nacos页面中可以看到
一、服务调用 openfeign (实现服务之间的调用)
引入 openfeign 依赖
<!--服务调用 openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
二、在调用的启动类中加入注解
@EnableFeignClients //服务调用 openfeign
三、创建一个 调用者 的interface接口
package com.xiyue.admin.client;
import com.xiyue.publicbase.ReJson;
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("vod-service") //被调用的 服务名称
@Component
public interface VodClient {
//定义方法路径(被调用服务的方法路径)
//删除视频根据视频id
@DeleteMapping("/vod/video/deleteAliyunVod/{videoId}")
public ReJson deleteAliyunVod(@PathVariable("videoId") String videoId);
}
四、在控制器中使用
@Autowired
VodClient vodClient;
@DeleteMapping("deleteVideo/{id}")
public ReJson deleteVideo(@PathVariable String id){
//注入刚刚创建的interface接口,调用方法
vodClient.deleteAliyunVod("视频id");
}
一、hystrix(熔断器使用)
1、引入依赖
2、配置yaml
feign: #开启熔断机制
hystrix:
enabled: true
command: # 设置hystrix超时时间,默认1000ms
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000
3、创建一个类,实现interface接口(接口是上面配置openfeign时创建的)
package com.xiyue.admin.client.HystrixConfig;
import com.xiyue.admin.client.VodClient;
import com.xiyue.publicbase.ReJson;
import org.springframework.stereotype.Component;
@Component
public class VodHystrix implements VodClient {
@Override
public ReJson deleteAliyunVod(String videoId) {
//当连接服务器失败 会执行这个方法
return ReJson.error().message("熔断");
}
}
接口中要在注解里加上参数(@FeignClient(name = "vod-service",fallback = VodHystrix.class))
package com.xiyue.admin.client;
import com.xiyue.admin.client.HystrixConfig.VodHystrix;
import com.xiyue.publicbase.ReJson;
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(name = "vod-service",fallback = VodHystrix.class) //被调用的 服务名称
@Component
public interface VodClient {
//定义方法路径(被调用服务的方法路径)
//删除视频根据视频id
@DeleteMapping("/vod/video/deleteAliyunVod/{videoId}")
public ReJson deleteAliyunVod(@PathVariable("videoId") String videoId);
}