赞
踩
使用nacos进行服务的注册与发现
使用openfeign进行服务间的调用
使用sentinel进行消费者服务的流量控制
大体逻辑:
到github release下载sentinel dashboard 的jar包,在自己电脑cmd启动起来然后最小化即可
java -Dserver.port=8718 -jar sentinel-dashboard-1.8.0.jar
网上搜一下吧,云服务器记得开8848,9848,9849端口
spring boot 2.3.3.RELEASE
spring-cloud-dependencies Hoxton.SR9
spring-cloud-alibaba-dependencies 2.2.6.RELEASE
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in class path resource
<dependencies> <!-- OpenFeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- 注册中心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- Sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies>
spring-cloud-starter-openfeign是由spring-cloud-dependencies进行版本管理的,下面nacos注册和sentinel的版本由spring-cloud-alibaba-dependencies管理
注意不能是application.yml
server: port: 9200 spring: application: # 应用名称 name: nacos-consumer cloud: nacos: discovery: # 服务注册地址 server-addr: nacos服务器IP:8848 sentinel: transport: dashboard: localhost:8718 feign: sentinel: enabled: true
到时候就直接访问localhost:9200就是consumer
@FeignClient(value = "nacos-provider", fallback = RemoteServiceFallback.class)
public interface RemoteService
{
@GetMapping(value = "/echo")
String echo();
}
用在上面@FeignClient的fallback中,当sentinel发现流量超过后面自己设置的流控规则后,就会让feign走这个fallback
@Service
public class RemoteServiceFallback implements RemoteService
{
@Override
public String echo()
{
return "系统繁忙,请稍后";
}
}
@RestController
public class ConsumerController
{
@Resource
private RemoteService remoteService;
@GetMapping(value = "/echo")
public String echo() {
return remoteService.echo();
}
}
加两个注解
//使用注册中心
@EnableDiscoveryClient
//获取注册中心的接口
@EnableFeignClients
<dependencies>
<!-- 注册中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
server:
port: 9201
spring:
application:
# 应用名称
name: nacos-provider
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: nacos服务器IP:8848
@RestController
public class ProviderController
{
@GetMapping("/echo")
public String echo() {
System.out.println("有一个请求");
return "来自Provider Demo";
}
}
@EnableDiscoveryClient
GET:http://nacos-provider/echo
,修改一个阈值为2保存即可Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。