请参考 : docker 安装 nacos 1.X
请参考 : SpringCloud H系列 alibaba 2.1.1 (一) nacos1.X 注册中心 服务端
pom.xml
com.alibaba
spring-cloud-hoxton
1.0
4.0.0
alibaba-sentinel-dubbo-api
jar
TestService.java 创建测试接口
/**
* @author Lion Li
*/
public interface TestService {
String test(String name);
}
首先在nacos创建配置文件 application-sentinel-dubbo-server.yml
application-sentinel-dubbo-server.yml
dubbo:
protocol:
# 使用dubbo协议通信
name: dubbo
# dubbo 协议端口(-1表示自增端口,从20880开始)
port: -1
# 挂载到 Spring Cloud 注册中心
registry:
address: spring-cloud://localhost
scan:
# 指定 Dubbo 服务实现类的扫描基准包
base-packages: com.alibaba.sentinel.dubbo.server.service
server:
port: 8000
pom.xml
com.alibaba
spring-cloud-hoxton
1.0
4.0.0
alibaba-sentinel-dubbo-server
jar
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
com.alibaba
alibaba-sentinel-dubbo-api
${project.version}
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
com.alibaba.csp
sentinel-apache-dubbo-adapter
SentinelDubboServerApplication.java 启动类
/**
* @author Lion Li
*/
@EnableDiscoveryClient // 开启注册中心客户端
@SpringBootApplication
public class SentinelDubboServerApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelDubboServerApplication.class, args);
}
}
TestServiceImpl.java api实现类
/**
* @author Lion Li
*/
@Service // 这里使用的是dubbo的 @Service 注解 将注册到注册中心
@RefreshScope // nacos 配置自动刷新
public class TestServiceImpl implements TestService {
@Value("${server.port:}")
private String port;
@Override
public String test(String name) {
return "服务器::端口:"+port+"::返回值 => " + name;
}
}
bootstrap.yml 配置文件
spring:
application:
name: alibaba-sentinel-dubbo-server
cloud:
# Nacos 服务发现与注册配置
nacos:
discovery:
server-addr: 192.168.101.11:8848
config:
# 配置中心地址
server-addr: 192.168.101.11:8848
# 文件后缀
file-extension: yml
# 文件前缀
prefix: application-sentinel-dubbo-server
# 命名空间ID
namespace: f799e2c7-3ab0-4e95-a7db-2150a91ec744
启动服务 查看nacos
启动成功
首先在nacos创建配置文件 application-sentinel-dubbo-client.yml
dubbo:
cloud:
# 订阅服务名
subscribed-services: alibaba-sentinel-dubbo-server
protocol:
# 使用dubbo协议通信
name: dubbo
# dubbo 协议端口(-1表示自增端口,从20880开始)
port: -1
# 挂载到 Spring Cloud 注册中心
registry:
address: spring-cloud://localhost
server:
port: 8002
pom.xml
com.alibaba
spring-cloud-hoxton
1.0
4.0.0
alibaba-sentinel-dubbo-client
jar
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba
alibaba-sentinel-dubbo-api
${project.version}
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
com.alibaba.csp
sentinel-apache-dubbo-adapter
SentinelDubboClientApplication.java 启动类
/**
* @author Lion Li
*/
@EnableDiscoveryClient // 开启注册中心客户端
@SpringBootApplication
public class SentinelDubboClientApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelDubboClientApplication.class, args);
}
}
TestController.java 消费接口
/**
* @author Lion Li
*/
@RestController
public class TestController {
@Reference // dubbo注入接口 从nacos注册中心获取服务器地址
private TestService testService;
@RequestMapping("/test")
public String test(String name) {
return testService.test(name);
}
}
FlowRuleConfig.java 代码配置限流规则 将接口使用QPS规则限流为1(QPS代表查询量) 查询数量大于1则限流
/**
* @author Lion Li
*/
@Configuration
public class FlowRuleConfig {
public FlowRuleConfig(){
// 代码配置限流
FlowRule flowRule = new FlowRule();
// 限流资源接口
flowRule.setResource("com.alibaba.sentinel.dubbo.api.TestService:test(java.lang.String)");
// 限流数峰值
flowRule.setCount(1);
// QPS限流
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setLimitApp("default");
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
}
bootstrap.yml 配置文件
spring:
application:
name: alibaba-sentinel-dubbo-client
cloud:
# Nacos 服务发现与注册配置
nacos:
discovery:
server-addr: 192.168.101.11:8848
config:
# 配置中心地址
server-addr: 192.168.101.11:8848
# 文件后缀
file-extension: yml
# 文件前缀
prefix: application-sentinel-dubbo-client
# 命名空间ID
namespace: f799e2c7-3ab0-4e95-a7db-2150a91ec744
启动消费端服务 并查看nacos
启动成功
请求消费端接口 localhost:8002/test?name=Lion Li
服务响应正常
连续两次快速请求消费端接口 localhost:8002/test?name=Lion Li
第一次返回结果
第二次返回结果
限流成功
项目已上传到gitee
地址: spring-cloud-alibaba-H-demo
如果帮到您了,请帮忙点个star