springCloud nacos、sentinel demo
This commit is contained in:
parent
0b6a41fba6
commit
fcb352b230
|
|
@ -0,0 +1,63 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>nacos-sentinel-dubbo</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>nsd消费者</name>
|
||||||
|
|
||||||
|
<artifactId>nsd-consumer</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-apache-dubbo-adapter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<artifactId>service-api</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
import org.apache.dubbo.config.annotation.DubboReference;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-06-01
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("app")
|
||||||
|
public class NacosSentinelDubboConsumerApp {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(NacosSentinelDubboConsumerApp.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@DubboReference
|
||||||
|
private HelloService helloService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String test() {
|
||||||
|
return helloService.sayHello("傻逼");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
server:
|
||||||
|
port: 10091
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: nacos-sentinel-dubbo-consumer
|
||||||
|
|
||||||
|
main:
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
namespace: study001
|
||||||
|
|
||||||
|
sentinel:
|
||||||
|
transport:
|
||||||
|
#sentinel控制台端口
|
||||||
|
dashboard: 127.0.0.1:10086
|
||||||
|
#sentinel会在该端口启动http server进行一些限流规则传递,如果占用,端口号依次+1,例10081
|
||||||
|
port: 10080
|
||||||
|
|
||||||
|
#Sentinel Nacos数据源配置,nacos中的规则会自动同步到sentinel流控规则中(Sentinel规则持久化)
|
||||||
|
datasource:
|
||||||
|
#自定义的流控规则数据源名称
|
||||||
|
flow:
|
||||||
|
nacos:
|
||||||
|
serverAddr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
|
dataId: ${spring.application.name}-flow-rules
|
||||||
|
groupId: sentinel
|
||||||
|
namespace: study001
|
||||||
|
dataType: json
|
||||||
|
ruleType: FLOW
|
||||||
|
|
||||||
|
#自定义的降级规则数据源名称
|
||||||
|
degrade:
|
||||||
|
nacos:
|
||||||
|
serverAddr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
|
dataId: ${spring.application.name}-degrade-rules
|
||||||
|
groupId: sentinel
|
||||||
|
namespace: study001
|
||||||
|
dataType: json
|
||||||
|
ruleType: degrade
|
||||||
|
|
||||||
|
|
||||||
|
dubbo:
|
||||||
|
registry:
|
||||||
|
address: spring-cloud://localhost
|
||||||
|
cloud:
|
||||||
|
subscribed-services: nacos-sentinel-dubbo-provider
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>nacos-sentinel-dubbo</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>nsd生产者</name>
|
||||||
|
|
||||||
|
<artifactId>nsd-provider</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-apache-dubbo-adapter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<artifactId>service-api</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-06-01
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class NacosSentinelDubboProviderApp {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(NacosSentinelDubboProviderApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.xjs.service.impl;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
import org.apache.dubbo.config.annotation.DubboService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-06-01
|
||||||
|
*/
|
||||||
|
@DubboService
|
||||||
|
public class HelloServiceImpl implements HelloService {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String sayHello(String name) {
|
||||||
|
return "hello";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String sayHello(String name, int timeTowait) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
server:
|
||||||
|
port: 10090
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: nacos-sentinel-dubbo-provider
|
||||||
|
|
||||||
|
main:
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
namespace: study001
|
||||||
|
|
||||||
|
sentinel:
|
||||||
|
transport:
|
||||||
|
#sentinel控制台端口
|
||||||
|
dashboard: 127.0.0.1:10086
|
||||||
|
#sentinel会在该端口启动http server进行一些限流规则传递,如果占用,端口号依次+1,例10081
|
||||||
|
port: 10080
|
||||||
|
|
||||||
|
#Sentinel Nacos数据源配置,nacos中的规则会自动同步到sentinel流控规则中(Sentinel规则持久化)
|
||||||
|
datasource:
|
||||||
|
#自定义的流控规则数据源名称
|
||||||
|
flow:
|
||||||
|
nacos:
|
||||||
|
serverAddr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
|
dataId: ${spring.application.name}-flow-rules
|
||||||
|
groupId: sentinel
|
||||||
|
namespace: study001
|
||||||
|
dataType: json
|
||||||
|
ruleType: FLOW
|
||||||
|
|
||||||
|
#自定义的降级规则数据源名称
|
||||||
|
degrade:
|
||||||
|
nacos:
|
||||||
|
serverAddr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
|
dataId: ${spring.application.name}-degrade-rules
|
||||||
|
groupId: sentinel
|
||||||
|
namespace: study001
|
||||||
|
dataType: json
|
||||||
|
ruleType: degrade
|
||||||
|
|
||||||
|
|
||||||
|
dubbo:
|
||||||
|
scan:
|
||||||
|
base-packages:
|
||||||
|
- com.xjs.service.impl
|
||||||
|
protocol:
|
||||||
|
name: dubbo
|
||||||
|
port: -1
|
||||||
|
host: 127.0.0.1
|
||||||
|
registry:
|
||||||
|
address: spring-cloud://localhost
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>springcloud-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>nsd三大组件</name>
|
||||||
|
<modules>
|
||||||
|
<module>nsd-provider</module>
|
||||||
|
<module>nsd-consumer</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<artifactId>nacos-sentinel-dubbo</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>nacos</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>Nacos客户端消费者</name>
|
||||||
|
<artifactId>nacos-client-consumer</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-31
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("app")
|
||||||
|
@RefreshScope
|
||||||
|
public class NacosClientConsumerApp {
|
||||||
|
|
||||||
|
@Value("${xjs}")
|
||||||
|
private Long num;
|
||||||
|
|
||||||
|
@Value("${yyy}")
|
||||||
|
private Long yyy;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(NacosClientConsumerApp.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String test() {
|
||||||
|
return num.toString() + yyy.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
server:
|
||||||
|
port: 8521
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: nacos-service-consumer
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
#naocs集群地址
|
||||||
|
server-addr: 127.0.0.1:8848,127.0.0.1:8858,127.0.0.1:8868
|
||||||
|
namespace: study001
|
||||||
|
|
||||||
|
config:
|
||||||
|
#配置中心地址
|
||||||
|
server-addr: 127.0.0.1:8848,127.0.0.1:8858,127.0.0.1:8868
|
||||||
|
#命名空间id
|
||||||
|
namespace: study001
|
||||||
|
#组id
|
||||||
|
group: DEFAULT_GROUP
|
||||||
|
|
||||||
|
|
||||||
|
file-extension: yml
|
||||||
|
prefix: study
|
||||||
|
|
||||||
|
extension-configs[0]:
|
||||||
|
data-id: study-copy.yml
|
||||||
|
refresh: true
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>nacos</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>Nacos客户端生产者</name>
|
||||||
|
<artifactId>nacos-client-provider</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-31
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
public class NacosClientProviderApp {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(NacosClientProviderApp.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
server:
|
||||||
|
port: 8520
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: nacos-service-provider
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: 127.0.0.1:8848,127.0.0.1:8858,127.0.0.1:8868
|
||||||
|
namespace: study001
|
||||||
|
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>springcloud-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>Nacos注册中心</name>
|
||||||
|
<modules>
|
||||||
|
<module>nacos-client-provider</module>
|
||||||
|
<module>nacos-client-consumer</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<artifactId>nacos</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||||
|
<version>2021.1</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -14,6 +14,9 @@
|
||||||
<module>eureka</module>
|
<module>eureka</module>
|
||||||
<module>stream</module>
|
<module>stream</module>
|
||||||
<module>oauth2</module>
|
<module>oauth2</module>
|
||||||
|
<module>nacos</module>
|
||||||
|
<module>sentinel</module>
|
||||||
|
<module>nacos-sentinel-dubbo</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<artifactId>springcloud-project</artifactId>
|
<artifactId>springcloud-project</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>springcloud-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>Sentinel熔断降级</name>
|
||||||
|
|
||||||
|
<artifactId>sentinel</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-31
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("app")
|
||||||
|
public class SentinelApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SentinelApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Value("${server.port}")
|
||||||
|
private Integer port;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
@SentinelResource(value = "port",
|
||||||
|
blockHandler = "handlePort",
|
||||||
|
blockHandlerClass = SentinelHandler.class,
|
||||||
|
fallback = "errorPost"
|
||||||
|
|
||||||
|
)
|
||||||
|
public String port() {
|
||||||
|
return port.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String handlePort(BlockException e) {
|
||||||
|
return "异常了啊";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String errorPost(BlockException e) {
|
||||||
|
return "error了啊";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-06-01
|
||||||
|
*/
|
||||||
|
public class SentinelHandler {
|
||||||
|
|
||||||
|
public static String handlePort(BlockException e) {
|
||||||
|
return "异常了啊!!!!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
server:
|
||||||
|
port: 10088
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: sentienl-service
|
||||||
|
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
# ,127.0.0.1:8858,127.0.0.1:8868
|
||||||
|
namespace: study001
|
||||||
|
|
||||||
|
sentinel:
|
||||||
|
transport:
|
||||||
|
#sentinel控制台端口
|
||||||
|
dashboard: 127.0.0.1:10086
|
||||||
|
#sentinel会在该端口启动http server进行一些限流规则传递,如果占用,端口号依次+1,例10081
|
||||||
|
port: 10080
|
||||||
|
|
||||||
|
#Sentinel Nacos数据源配置,nacos中的规则会自动同步到sentinel流控规则中(Sentinel规则持久化)
|
||||||
|
datasource:
|
||||||
|
#自定义的流控规则数据源名称
|
||||||
|
flow:
|
||||||
|
nacos:
|
||||||
|
serverAddr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
|
dataId: ${spring.application.name}-flow-rules
|
||||||
|
groupId: sentinel
|
||||||
|
namespace: study001
|
||||||
|
dataType: json
|
||||||
|
ruleType: FLOW
|
||||||
|
|
||||||
|
#自定义的降级规则数据源名称
|
||||||
|
degrade:
|
||||||
|
nacos:
|
||||||
|
serverAddr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
|
dataId: ${spring.application.name}-degrade-rules
|
||||||
|
groupId: sentinel
|
||||||
|
namespace: study001
|
||||||
|
dataType: json
|
||||||
|
ruleType: degrade
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue