mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
26
README.md
26
README.md
@@ -1,3 +1,14 @@
|
||||
一个涵盖四个主流技术栈的**正经**仓库:
|
||||
* [《Spring Boot 专栏》](https://github.com/YunaiV/SpringBoot-Labs#spring-boot-%E4%B8%93%E6%A0%8F)
|
||||
* [《Spring Cloud Alibaba 专栏》](https://github.com/YunaiV/SpringBoot-Labs#spring-cloud-alibaba-%E4%B8%93%E6%A0%8F)
|
||||
* [《Spring Cloud 专栏》](https://github.com/YunaiV/SpringBoot-Labs#spring-cloud-%E4%B8%93%E6%A0%8F)
|
||||
* [《Dubbo 专栏》](https://github.com/YunaiV/SpringBoot-Labs#Dubbo-%E4%B8%93%E6%A0%8F)
|
||||
|
||||
作为一个热爱**深夜**撸码的 18 岁秃头的可爱小男孩,希望大佬能够**一键三连**。
|
||||
|
||||

|
||||
|
||||
> ~~交流~~**装逼群**,请点击[传送门](http://www.iocoder.cn/mall-user-group/?vip&gitee),进行引导加入。**亲,一键三连啊**
|
||||
|
||||
# Spring Boot 专栏
|
||||
|
||||
@@ -136,9 +147,8 @@
|
||||
|
||||
* [《芋道 Spring Cloud Netflix 负载均衡 Ribbon 入门》](http://www.iocoder.cn/Spring-Cloud-Netflix/Ribbon/?github) 对应 [labx-02](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-02)
|
||||
* [《芋道 Spring Cloud 声明式调用 Feign 入门》](http://www.iocoder.cn/Spring-Cloud/Feign/?github) 对应 [labx-03](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-03)
|
||||
* 《芋道 Spring Cloud 服务网关 Spring Cloud Gateway 入门》
|
||||
* [《芋道 Spring Cloud 服务网关 Spring Cloud Gateway 入门》](http://www.iocoder.cn/Spring-Cloud/Spring-Cloud-Gateway/?github) 对应 [labx-08](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-08)
|
||||
* 《芋道 Spring Cloud 链路追踪 SkyWalking 入门》
|
||||
|
||||
* TODO 监控
|
||||
|
||||
# Spring Cloud 专栏
|
||||
@@ -166,19 +176,25 @@
|
||||
## 配置中心
|
||||
|
||||
* [《芋道 Spring Cloud Alibaba 配置中心 Nacos 入门》](http://www.iocoder.cn/Spring-Cloud-Alibaba/Nacos-Config/?github) 对应 [labx-05](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-05)
|
||||
* 《芋道 Spring Cloud 配置中心 Apollo 入门》
|
||||
* [《芋道 Spring Cloud 配置中心 Apollo 入门》](http://www.iocoder.cn/Spring-Cloud/Apollo/?github) 对应 [labx-09](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-09)
|
||||
|
||||
|
||||
## 消息队列
|
||||
|
||||
* [《芋道 Spring Cloud Alibaba 消息队列 RocketMQ 入门》](http://www.iocoder.cn/Spring-Cloud-Alibaba/RocketMQ/?github) 对应 [labx-06](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-06)
|
||||
* 《芋道 Spring Cloud Alibaba 消息队列 RabbitMQ 入门》
|
||||
* 《芋道 Spring Cloud Alibaba 消息队列 Kafka 入门》
|
||||
* [《芋道 Spring Cloud 消息队列 RabbitMQ 入门》](http://www.iocoder.cn/Spring-Cloud/RabbitMQ/?github) 对应 [lab10-06](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-10)
|
||||
* 《芋道 Spring Cloud 消息队列 Kafka 入门》
|
||||
|
||||
## 链路追踪
|
||||
|
||||
* 《芋道 Spring Cloud 链路追踪 Sleuth 入门》
|
||||
* 《芋道 Spring Cloud 链路追踪 SkyWalking 入门》
|
||||
|
||||
# Dubbo 专栏
|
||||
|
||||
* [《芋道 Spring Boot Dubbo 入门》](http://www.iocoder.cn/Spring-Boot/Dubbo/?github) 对应 [lab-30](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-30)
|
||||
* [《芋道 Spring Cloud Alibaba 服务调用 Dubbo 入门》](http://www.iocoder.cn/Spring-Cloud-Alibaba/Dubbo/?github) 对应 [labx-07](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-07)
|
||||
|
||||
---------
|
||||
|
||||
如下是草稿目录,未来需要整理下
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
server:
|
||||
port: 7070 # 避免和本地的 Apollo Portal 端口冲突
|
||||
|
||||
app:
|
||||
id: demo-application # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer-application
|
||||
cloud:
|
||||
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
|
||||
stream:
|
||||
# Binding 配置项,对应 BindingProperties Map
|
||||
bindings:
|
||||
demo01-input:
|
||||
destination: DEMO-TOPIC-01 # 目的地。这里使用 RocketMQ Topic
|
||||
content-type: application/json # 内容格式。这里使用 JSON
|
||||
group: demo01-consumer-group-DEMO-TOPIC-01 # 消费者分组
|
||||
# Spring Cloud Stream RocketMQ 配置项
|
||||
rocketmq:
|
||||
# RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类
|
||||
binder:
|
||||
name-server: 127.0.0.1:9876 # RocketMQ Namesrv 地址
|
||||
# RocketMQ 自定义 Binding 配置项,对应 RocketMQBindingProperties Map
|
||||
bindings:
|
||||
demo01-input:
|
||||
# RocketMQ Consumer 配置项,对应 RocketMQConsumerProperties 类
|
||||
consumer:
|
||||
enabled: true # 是否开启消费,默认为 true
|
||||
broadcasting: false # 是否使用广播消费,默认为 false 使用集群消费
|
||||
|
||||
server:
|
||||
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
endpoint:
|
||||
# Health 端点配置项,对应 HealthProperties 配置类
|
||||
health:
|
||||
enabled: true # 是否开启。默认为 true 开启。
|
||||
show-details: ALWAYS # 何时显示完整的健康信息。默认为 NEVER 都不展示。可选 WHEN_AUTHORIZED 当经过授权的用户;可选 ALWAYS 总是展示。
|
||||
@@ -1,31 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer-application
|
||||
cloud:
|
||||
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
|
||||
stream:
|
||||
# Binding 配置项,对应 BindingProperties Map
|
||||
bindings:
|
||||
demo01-input:
|
||||
destination: DEMO-TOPIC-01 # 目的地。这里使用 RocketMQ Topic
|
||||
content-type: application/json # 内容格式。这里使用 JSON
|
||||
group: demo01-consumer-group-DEMO-TOPIC-01 # 消费者分组
|
||||
# Consumer 配置项,对应 ConsumerProperties 类
|
||||
consumer:
|
||||
max-attempts: 1
|
||||
# Spring Cloud Stream RocketMQ 配置项
|
||||
rocketmq:
|
||||
# RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类
|
||||
binder:
|
||||
name-server: 127.0.0.1:9876 # RocketMQ Namesrv 地址
|
||||
# RocketMQ 自定义 Binding 配置项,对应 RocketMQBindingProperties Map
|
||||
bindings:
|
||||
demo01-input:
|
||||
# RocketMQ Consumer 配置项,对应 RocketMQConsumerProperties 类
|
||||
consumer:
|
||||
enabled: true # 是否开启消费,默认为 true
|
||||
broadcasting: false # 是否使用广播消费,默认为 false 使用集群消费
|
||||
delay-level-when-next-consume: 0 # 异步消费消息模式下消费失败重试策略,默认为 0
|
||||
|
||||
server:
|
||||
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者
|
||||
@@ -1,28 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer-application
|
||||
cloud:
|
||||
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
|
||||
stream:
|
||||
# Binding 配置项,对应 BindingProperties Map
|
||||
bindings:
|
||||
demo01-input:
|
||||
destination: DEMO-TOPIC-01 # 目的地。这里使用 RocketMQ Topic
|
||||
content-type: application/json # 内容格式。这里使用 JSON
|
||||
group: demo01-consumer-group-DEMO-TOPIC-01 # 消费者分组
|
||||
# Spring Cloud Stream RocketMQ 配置项
|
||||
rocketmq:
|
||||
# RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类
|
||||
binder:
|
||||
name-server: 127.0.0.1:9876 # RocketMQ Namesrv 地址
|
||||
# RocketMQ 自定义 Binding 配置项,对应 RocketMQBindingProperties Map
|
||||
bindings:
|
||||
demo01-input:
|
||||
# RocketMQ Consumer 配置项,对应 RocketMQConsumerProperties 类
|
||||
consumer:
|
||||
enabled: true # 是否开启消费,默认为 true
|
||||
broadcasting: false # 是否使用广播消费,默认为 false 使用集群消费
|
||||
orderly: true # 是否顺序消费,默认为 false 并发消费。
|
||||
|
||||
server:
|
||||
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者
|
||||
@@ -1,29 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-producer-application
|
||||
cloud:
|
||||
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
|
||||
stream:
|
||||
# Binding 配置项,对应 BindingProperties Map
|
||||
bindings:
|
||||
demo01-output:
|
||||
destination: DEMO-TOPIC-01 # 目的地。这里使用 RocketMQ Topic
|
||||
content-type: application/json # 内容格式。这里使用 JSON
|
||||
# Producer 配置项,对应 ProducerProperties 类
|
||||
producer:
|
||||
partition-key-expression: payload['id'] # 分区 key 表达式。该表达式基于 Spring EL,从消息中获得分区 key。
|
||||
# Spring Cloud Stream RocketMQ 配置项
|
||||
rocketmq:
|
||||
# RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类
|
||||
binder:
|
||||
name-server: 127.0.0.1:9876 # RocketMQ Namesrv 地址
|
||||
# RocketMQ 自定义 Binding 配置项,对应 RocketMQBindingProperties Map
|
||||
bindings:
|
||||
demo01-output:
|
||||
# RocketMQ Producer 配置项,对应 RocketMQProducerProperties 类
|
||||
producer:
|
||||
group: test # 生产者分组
|
||||
sync: true # 是否同步发送消息,默认为 false 异步。
|
||||
|
||||
server:
|
||||
port: 18080
|
||||
60
labx-08/labx-08-sc-gateway-demo01-test/pom.xml
Normal file
60
labx-08/labx-08-sc-gateway-demo01-test/pom.xml
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo01-test</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo.config;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
//@Configuration
|
||||
public class GatewayConfig {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(GatewayConfig.class);
|
||||
|
||||
@Bean
|
||||
@Order(1)
|
||||
public GlobalFilter firstGlobalFilter() {
|
||||
return (exchange, chain) -> {
|
||||
logger.info("[first][pre]");
|
||||
return chain.filter(exchange)
|
||||
.then(Mono.<Void>fromRunnable(() -> logger.info("[first][post]")));
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(2)
|
||||
public GlobalFilter secondGatewayFilter() {
|
||||
return (exchange, chain) -> {
|
||||
logger.info("[second][pre]");
|
||||
return chain.filter(exchange)
|
||||
.then(Mono.<Void>fromRunnable(() -> logger.info("[second][post]")));
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(3)
|
||||
public GlobalFilter thirdGlobalFilter() {
|
||||
return (exchange, chain) -> {
|
||||
logger.info("[third][pre]");
|
||||
return chain.filter(exchange)
|
||||
.then(Mono.<Void>fromRunnable(() -> logger.info("[third][post]")));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=100
|
||||
# - StripPrefix=200
|
||||
# default-filters:
|
||||
# - StripPrefix=1
|
||||
# - StripPrefix=2
|
||||
# - StripPrefix=3
|
||||
|
||||
# httpserver:
|
||||
# wiretap: true
|
||||
# httpclient:
|
||||
# wiretap: true
|
||||
|
||||
logging:
|
||||
level:
|
||||
reactor.netty: DEBUG
|
||||
org.springframework.cloud.gateway: TRACE
|
||||
# org.springframework.web.reactive: TRACE
|
||||
58
labx-08/labx-08-sc-gateway-demo01/pom.xml
Normal file
58
labx-08/labx-08-sc-gateway-demo01/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo01</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=1
|
||||
64
labx-08/labx-08-sc-gateway-demo02-registry/pom.xml
Normal file
64
labx-08/labx-08-sc-gateway-demo02-registry/pom.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo02-registry</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=1
|
||||
# 与 Spring Cloud 注册中心的集成,对应 DiscoveryLocatorProperties 类
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true # 是否开启,默认为 false 关闭
|
||||
url-expression: "'lb://' + serviceId" # 路由的目标地址的表达式,默认为 "'lb://' + serviceId"
|
||||
|
||||
# Nacos 作为注册中心的配置项
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
73
labx-08/labx-08-sc-gateway-demo03-config-apollo/pom.xml
Normal file
73
labx-08/labx-08-sc-gateway-demo03-config-apollo/pom.xml
Normal file
@@ -0,0 +1,73 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo03-config-apollo</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId>
|
||||
<version>1.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import com.ctrip.framework.apollo.enums.PropertyChangeType;
|
||||
import com.ctrip.framework.apollo.model.ConfigChange;
|
||||
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
|
||||
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||
import org.springframework.cloud.gateway.config.GatewayProperties;
|
||||
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* 由 https://github.com/ctripcorp/apollo-use-cases/tree/master/spring-cloud-gateway 提供代码,感谢~
|
||||
*/
|
||||
@Component
|
||||
public class GatewayPropertiesRefresher implements ApplicationContextAware, ApplicationEventPublisherAware {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GatewayPropertiesRefresher.class);
|
||||
|
||||
private static final String ID_PATTERN = "spring\\.cloud\\.gateway\\.routes\\[\\d+\\]\\.id";
|
||||
|
||||
private static final String DEFAULT_FILTER_PATTERN = "spring\\.cloud\\.gateway\\.default-filters\\[\\d+\\]\\.name";
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
@Autowired
|
||||
private GatewayProperties gatewayProperties;
|
||||
|
||||
@Autowired
|
||||
private RouteDefinitionWriter routeDefinitionWriter;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.publisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
@ApolloConfigChangeListener(interestedKeyPrefixes = "spring.cloud.gateway.")
|
||||
public void onChange(ConfigChangeEvent changeEvent) {
|
||||
refreshGatewayProperties(changeEvent);
|
||||
}
|
||||
|
||||
/***
|
||||
* 刷新org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator中定义的routes
|
||||
*
|
||||
* @param changeEvent
|
||||
* @return void
|
||||
* @author ksewen
|
||||
* @date 2019/5/21 2:13 PM
|
||||
*/
|
||||
private void refreshGatewayProperties(ConfigChangeEvent changeEvent) {
|
||||
logger.info("Refreshing GatewayProperties!");
|
||||
// <1>
|
||||
preDestroyGatewayProperties(changeEvent);
|
||||
// <2>
|
||||
this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
|
||||
// <3>
|
||||
refreshGatewayRouteDefinition();
|
||||
logger.info("GatewayProperties refreshed!");
|
||||
}
|
||||
|
||||
/***
|
||||
* GatewayProperties没有@PreDestroy和destroy方法
|
||||
* org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder#rebind(java.lang.String)中destroyBean时不会销毁当前对象
|
||||
* 如果把spring.cloud.gateway.前缀的配置项全部删除(例如需要动态删除最后一个路由的场景),initializeBean时也无法创建新的bean,则return当前bean
|
||||
* 若仍保留有spring.cloud.gateway.routes[n]或spring.cloud.gateway.default-filters[n]等配置,initializeBean时会注入新的属性替换已有的bean
|
||||
* 这个方法提供了类似@PreDestroy的操作,根据配置文件的实际情况把org.springframework.cloud.gateway.config.GatewayProperties#routes
|
||||
* 和org.springframework.cloud.gateway.config.GatewayProperties#defaultFilters两个集合清空
|
||||
*
|
||||
* @param
|
||||
* @return void
|
||||
* @author ksewen
|
||||
* @date 2019/5/21 2:13 PM
|
||||
*/
|
||||
private synchronized void preDestroyGatewayProperties(ConfigChangeEvent changeEvent) {
|
||||
logger.info("Pre Destroy GatewayProperties!");
|
||||
// 判断 `spring.cloud.gateway.routes` 配置项,是否被全部删除。如果是,则置空 GatewayProperties 的 `routes` 属性
|
||||
final boolean needClearRoutes = this.checkNeedClear(changeEvent, ID_PATTERN, this.gatewayProperties.getRoutes().size());
|
||||
if (needClearRoutes) {
|
||||
this.gatewayProperties.setRoutes(new ArrayList<>());
|
||||
}
|
||||
// 判断 `spring.cloud.gateway.default-filters` 配置项,是否被全部删除。如果是,则置空 GatewayProperties 的 `defaultFilters` 属性
|
||||
final boolean needClearDefaultFilters = this.checkNeedClear(changeEvent, DEFAULT_FILTER_PATTERN, this.gatewayProperties.getDefaultFilters().size());
|
||||
if (needClearDefaultFilters) {
|
||||
this.gatewayProperties.setRoutes(new ArrayList<>());
|
||||
}
|
||||
logger.info("Pre Destroy GatewayProperties finished!");
|
||||
}
|
||||
|
||||
private void refreshGatewayRouteDefinition() {
|
||||
logger.info("Refreshing Gateway RouteDefinition!");
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
logger.info("Gateway RouteDefinition refreshed!");
|
||||
}
|
||||
|
||||
/***
|
||||
* 根据changeEvent和定义的pattern匹配key,如果所有对应PropertyChangeType为DELETED则需要清空GatewayProperties里相关集合
|
||||
*
|
||||
* @param changeEvent
|
||||
* @param pattern
|
||||
* @param existSize
|
||||
* @return boolean
|
||||
* @author ksewen
|
||||
* @date 2019/5/23 2:18 PM
|
||||
*/
|
||||
// 判断是否清除的标准,是通过指定配置项被删除的数量,是否和内存中的该配置项的数量一样。如果一样,说明被清空了
|
||||
private boolean checkNeedClear(ConfigChangeEvent changeEvent, String pattern, int existSize) {
|
||||
return changeEvent.changedKeys().stream().filter(key -> key.matches(pattern))
|
||||
.filter(key -> {
|
||||
ConfigChange change = changeEvent.getChange(key);
|
||||
return PropertyChangeType.DELETED.equals(change.getChangeType());
|
||||
}).count() == existSize;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,全部配置在 Apollo 中
|
||||
# gateway:
|
||||
|
||||
# Nacos 作为注册中心的配置项
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
|
||||
# Apollo 相关配置项
|
||||
app:
|
||||
id: ${spring.application.name} # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
meta: http://127.0.0.1:8080 # Apollo Meta Server 地址
|
||||
bootstrap:
|
||||
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
|
||||
eagerLoad:
|
||||
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
|
||||
namespaces: application # 使用的 Apollo 的命名空间,默认为 application。
|
||||
72
labx-08/labx-08-sc-gateway-demo03-config-nacos/pom.xml
Normal file
72
labx-08/labx-08-sc-gateway-demo03-config-nacos/pom.xml
Normal file
@@ -0,0 +1,72 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo03-config-nacos</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Config 相关依赖,将 Nacos 作为配置中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,全部配置在 Nacos 中
|
||||
# gateway:
|
||||
|
||||
# Nacos 作为注册中心的配置项
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
@@ -1,6 +1,6 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-application
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
58
labx-08/labx-08-sc-gateway-demo04/pom.xml
Normal file
58
labx-08/labx-08-sc-gateway-demo04/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo04</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: user-service-prod
|
||||
uri: http://www.iocoder.cn
|
||||
predicates:
|
||||
- Path=/**
|
||||
- Weight=user-service, 90
|
||||
- id: user-service-canary
|
||||
uri: https://www.oschina.net
|
||||
predicates:
|
||||
- Path=/**
|
||||
- Weight=user-service, 10
|
||||
@@ -0,0 +1,60 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo05-custom-gateway-filter</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo.filter;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<AuthGatewayFilterFactory.Config> {
|
||||
|
||||
public AuthGatewayFilterFactory() {
|
||||
super(AuthGatewayFilterFactory.Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
// token 和 userId 的映射
|
||||
Map<String, Integer> tokenMap = new HashMap<>();
|
||||
tokenMap.put("yunai", 1);
|
||||
|
||||
// 创建 GatewayFilter 对象
|
||||
return new GatewayFilter() {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
// 获得 token
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
String token = headers.getFirst(config.getTokenHeaderName());
|
||||
|
||||
// 如果没有 token,则不进行认证。因为可能是无需认证的 API 接口
|
||||
if (!StringUtils.hasText(token)) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
// 进行认证
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
Integer userId = tokenMap.get(token);
|
||||
|
||||
// 通过 token 获取不到 userId,说明认证不通过
|
||||
if (userId == null) {
|
||||
// 响应 401 状态码
|
||||
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
// 响应提示
|
||||
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap("认证不通过".getBytes());
|
||||
return response.writeWith(Flux.just(buffer));
|
||||
}
|
||||
|
||||
// 认证通过,将 userId 添加到 Header 中
|
||||
request = request.mutate().header(config.getUserIdHeaderName(), String.valueOf(userId))
|
||||
.build();
|
||||
return chain.filter(exchange.mutate().request(request).build());
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
|
||||
private static final String DEFAULT_TOKEN_HEADER_NAME = "token";
|
||||
private static final String DEFAULT_HEADER_NAME = "user-id";
|
||||
|
||||
private String tokenHeaderName = DEFAULT_TOKEN_HEADER_NAME;
|
||||
private String userIdHeaderName = DEFAULT_HEADER_NAME;
|
||||
|
||||
public String getTokenHeaderName() {
|
||||
return tokenHeaderName;
|
||||
}
|
||||
|
||||
public String getUserIdHeaderName() {
|
||||
return userIdHeaderName;
|
||||
}
|
||||
|
||||
public Config setTokenHeaderName(String tokenHeaderName) {
|
||||
this.tokenHeaderName = tokenHeaderName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Config setUserIdHeaderName(String userIdHeaderName) {
|
||||
this.userIdHeaderName = userIdHeaderName;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=1
|
||||
# 默认过滤器,对应 FilterDefinition 数组
|
||||
default-filters:
|
||||
- name: Auth
|
||||
args:
|
||||
token-header-name: access-token
|
||||
@@ -0,0 +1,29 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=1
|
||||
# 默认过滤器,对应 FilterDefinition 数组
|
||||
default-filters:
|
||||
- name: Auth
|
||||
args:
|
||||
token-header-name: access-token
|
||||
67
labx-08/labx-08-sc-gateway-demo06-rate-limiter/pom.xml
Normal file
67
labx-08/labx-08-sc-gateway-demo06-rate-limiter/pom.xml
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo06-rate-limiter</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Spring Data Redis 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo.config;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Configuration
|
||||
public class GatewayConfig {
|
||||
|
||||
@Bean
|
||||
public KeyResolver ipKeyResolver() {
|
||||
return new KeyResolver() {
|
||||
|
||||
@Override
|
||||
public Mono<String> resolve(ServerWebExchange exchange) {
|
||||
// 获取请求的 IP
|
||||
return Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- name: RequestRateLimiter
|
||||
args:
|
||||
redis-rate-limiter.replenishRate: 1 # 令牌桶的每秒放的数量
|
||||
redis-rate-limiter.burstCapacity: 2 # 令牌桶的最大令牌数
|
||||
key-resolver: "#{@ipKeyResolver}" # 获取限流 KEY 的 Bean 的名字
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=1
|
||||
|
||||
# Redis 配置项
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
64
labx-08/labx-08-sc-gateway-demo07-hystrix/pom.xml
Normal file
64
labx-08/labx-08-sc-gateway-demo07-hystrix/pom.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo07-hystrix</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Hystrix 相关依赖,使用它实现服务容错,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo.controller;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
@RestController
|
||||
public class FallbackController {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(FallbackController.class);
|
||||
|
||||
@GetMapping("/fallback")
|
||||
public String fallback(ServerWebExchange exchange) {
|
||||
// URI requestUrl = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR);
|
||||
Throwable executionException = exchange.getAttribute(ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR);
|
||||
logger.error("[fallback][发生异常]", executionException);
|
||||
|
||||
return "服务降级..." + executionException.getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: hystrix_test
|
||||
uri: http://127.0.0.1:18181
|
||||
predicates:
|
||||
- Path=/**
|
||||
filters:
|
||||
- name: Hystrix
|
||||
args:
|
||||
name: fallbackcmd # 对应的 Hystrix Command 名字
|
||||
fallbackUri: forward:/fallback # 处理 Hystrix fallback 的情况,重定向到指定地址
|
||||
70
labx-08/labx-08-sc-gateway-demo07-sentinel/pom.xml
Normal file
70
labx-08/labx-08-sc-gateway-demo07-sentinel/pom.xml
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo07-sentinel</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Sentinel 相关依赖,使用 Sentinel 提供服务保障,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,23 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class CustomBlockRequestHandler implements BlockRequestHandler {
|
||||
|
||||
private static final String DEFAULT_BLOCK_MSG_PREFIX = "HAHAHA ~ Blocked by Sentinel: ";
|
||||
|
||||
@Override
|
||||
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable ex) {
|
||||
return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS) // 状态码
|
||||
.contentType(MediaType.TEXT_PLAIN) // 内容类型为 text/plain 纯文本
|
||||
.bodyValue(DEFAULT_BLOCK_MSG_PREFIX + ex.getClass().getSimpleName()); // 错误提示
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// System.setProperty(SentinelConfig.APP_TYPE, ConfigConstants.APP_TYPE_SCG_GATEWAY);
|
||||
// System.setProperty(SentinelConfig.APP_TYPE, "1");
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/**
|
||||
|
||||
sentinel:
|
||||
eager: true # 是否饥饿加载。默认为 false 关闭
|
||||
transport:
|
||||
dashboard: localhost:7070 # 是否饥饿加载。默认为 false 关闭
|
||||
# # 数据源的配置项
|
||||
# datasource:
|
||||
# ds1.file:
|
||||
# file: "classpath: sentinel-gw-flow.json"
|
||||
# ruleType: gw-flow
|
||||
# ds2.file:
|
||||
# file: "classpath: sentinel-gw-api-group.json"
|
||||
# ruleType: gw-api-group
|
||||
# Sentinel 对 Spring Cloud Gateway 的专属配置项,对应 SentinelGatewayProperties 类
|
||||
scg:
|
||||
order: -2147483648 # 过滤器顺序,默认为 -2147483648 最高优先级
|
||||
fallback:
|
||||
mode: # fallback 模式,目前有三种:response、redirect、空
|
||||
# 专属 response 模式
|
||||
response-status: 429 # 响应状态码,默认为 429
|
||||
response-body: 你被 block 了... # 响应内容,默认为空
|
||||
content-type: application/json # 内容类型,默认为 application/json
|
||||
# 专属 redirect 模式
|
||||
redirect: http://www.baidu.com
|
||||
@@ -0,0 +1,23 @@
|
||||
[
|
||||
{
|
||||
"apiName": "yudaoyuanma_customized_api",
|
||||
"predicateItems": [
|
||||
{
|
||||
"pattern": "/categories/**",
|
||||
"matchStrategy": 1
|
||||
},
|
||||
{
|
||||
"items": [
|
||||
{
|
||||
"pattern": "/Dubbo/good-collection/",
|
||||
"matchStrategy": 0
|
||||
},
|
||||
{
|
||||
"pattern": "/SkyWalking/**",
|
||||
"matchStrategy": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"resource": "yudaoyuanma",
|
||||
"count": 3
|
||||
},
|
||||
{
|
||||
"resource": "yudaoyuanma_customized_api",
|
||||
"count": 1
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,79 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo08-custom-global-filter</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入定义的 Dubbo API 接口 -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>cn.iocoder.springboot.labs</groupId>-->
|
||||
<!-- <artifactId>labx-07-sca-dubbo-demo02-api</artifactId>-->
|
||||
<!-- <version>1.0-SNAPSHOT</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Dubbo 相关依赖,实现呢 Dubbo 进行远程调用,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user03")
|
||||
public class User03Controller {
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
// @GetMapping("/get")
|
||||
// public UserDTO get(@RequestParam("id") Integer id) {
|
||||
// String url = String.format("http://%s/user/get?id=%d", "demo-provider", id);
|
||||
// return restTemplate.getForObject(url, UserDTO.class);
|
||||
// }
|
||||
//
|
||||
// @PostMapping("/add")
|
||||
// public Integer add(UserAddDTO addDTO) {
|
||||
// // 请求头
|
||||
// HttpHeaders headers = new HttpHeaders();
|
||||
// headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
// // 请求体
|
||||
// String body = JSON.toJSONString(addDTO);
|
||||
// // 创建 HttpEntity 对象
|
||||
// HttpEntity<String> entity = new HttpEntity<>(body, headers);
|
||||
// // 执行请求
|
||||
// String url = String.format("http://%s/user/add", "demo-provider");
|
||||
// return restTemplate.postForObject(url, entity, Integer.class);
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo.config;
|
||||
|
||||
import com.alibaba.cloud.dubbo.annotation.DubboTransported;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
@DubboTransported(protocol = "dubbo")
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo.filter;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR;
|
||||
|
||||
@Component
|
||||
public class DubboFilter implements GlobalFilter, Ordered {
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
URI url = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
String schemePrefix = exchange.getAttribute(GATEWAY_SCHEME_PREFIX_ATTR);
|
||||
if (url == null
|
||||
|| (!"dubbo".equals(url.getScheme()) && !"dubbo".equals(schemePrefix))) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
String urlx = String.format("http://%s/user/get?id=%d", "demo-provider", 1);
|
||||
// UserDTO result = restTemplate.getForObject(url, UserDTO.class);
|
||||
String result = restTemplate.getForObject(url, String.class);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return 10100;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
# - id: yudaoyuanma # 路由的编号
|
||||
# uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
# predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
# - Path=/blog
|
||||
# filters:
|
||||
# - StripPrefix=1
|
||||
# - id: oschina # 路由的编号
|
||||
# uri: https://www.oschina.net # 路由的目标地址
|
||||
# predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
# - Path=/oschina
|
||||
# filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
# - StripPrefix=1
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: dubbo://demo-provider # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/user/**
|
||||
# filters:
|
||||
# - StripPrefix=1
|
||||
|
||||
# Dubbo 配置项,对应 DubboConfigurationProperties 类
|
||||
dubbo:
|
||||
# Dubbo 服务注册中心配置,对应 RegistryConfig 类
|
||||
registry:
|
||||
address: spring-cloud://127.0.0.1:8848 # 指定 Dubbo 服务注册中心的地址
|
||||
# Spring Cloud Alibaba Dubbo 专属配置项,对应 DubboCloudProperties 类
|
||||
cloud:
|
||||
subscribed-services: demo-provider # 设置订阅的应用列表,默认为 * 订阅所有应用。
|
||||
64
labx-08/labx-08-sc-gateway-demo09-actuator/pom.xml
Normal file
64
labx-08/labx-08-sc-gateway-demo09-actuator/pom.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo09-actuator</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Actuator 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
# cloud:
|
||||
# # Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
# gateway:
|
||||
# # 路由配置项,对应 RouteDefinition 数组
|
||||
# routes:
|
||||
# - id: yudaoyuanma # 路由的编号
|
||||
# uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
# predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
# - Path=/blog
|
||||
# filters:
|
||||
# - StripPrefix=1
|
||||
# - id: oschina # 路由的编号
|
||||
# uri: https://www.oschina.net # 路由的目标地址
|
||||
# predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
# - Path=/oschina
|
||||
# filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
# - StripPrefix=1
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
endpoint:
|
||||
# Health 端点配置项,对应 HealthProperties 配置类
|
||||
health:
|
||||
enabled: true # 是否开启。默认为 true 开启。
|
||||
show-details: ALWAYS # 何时显示完整的健康信息。默认为 NEVER 都不展示。可选 WHEN_AUTHORIZED 当经过授权的用户;可选 ALWAYS 总是展示。
|
||||
58
labx-08/labx-08-sc-gateway-demo10-troubleshooting/pom.xml
Normal file
58
labx-08/labx-08-sc-gateway-demo10-troubleshooting/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-gateway-demo10-troubleshooting</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.gatewaydemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gateway-application
|
||||
|
||||
cloud:
|
||||
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
|
||||
gateway:
|
||||
# 路由配置项,对应 RouteDefinition 数组
|
||||
routes:
|
||||
- id: yudaoyuanma # 路由的编号
|
||||
uri: http://www.iocoder.cn # 路由到的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/blog
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: oschina # 路由的编号
|
||||
uri: https://www.oschina.net # 路由的目标地址
|
||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||
- Path=/oschina
|
||||
filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
|
||||
- StripPrefix=1
|
||||
|
||||
# Reactor Netty 相关配置
|
||||
httpserver:
|
||||
wiretap: true
|
||||
httpclient:
|
||||
wiretap: true
|
||||
|
||||
logging:
|
||||
level:
|
||||
reactor.netty: DEBUG
|
||||
org.springframework.cloud.gateway: TRACE
|
||||
64
labx-08/labx-08-sc-user-service/pom.xml
Normal file
64
labx-08/labx-08-sc-user-service/pom.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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>labx-08</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08-sc-user-service</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx08.userservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class UserServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserServiceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cn.iocoder.springcloud.labx08.userservice.controller;
|
||||
|
||||
import cn.iocoder.springcloud.labx08.userservice.dto.UserAddDTO;
|
||||
import cn.iocoder.springcloud.labx08.userservice.dto.UserDTO;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@GetMapping("/get")
|
||||
public UserDTO get(@RequestParam("id") Integer id) {
|
||||
return new UserDTO().setId(id)
|
||||
.setName("没有昵称:" + id)
|
||||
.setGender(id % 2 + 1); // 1 - 男;2 - 女
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public Integer add(UserAddDTO addDTO) {
|
||||
return (int) (System.currentTimeMillis() / 1000); // 嘿嘿,随便返回一个 id
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.springcloud.labx08.userservice.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户添加 DTO
|
||||
*/
|
||||
public class UserAddDTO implements Serializable {
|
||||
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public UserAddDTO setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public UserAddDTO setGender(Integer gender) {
|
||||
this.gender = gender;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package cn.iocoder.springcloud.labx08.userservice.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户信息 DTO
|
||||
*/
|
||||
public class UserDTO implements Serializable {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UserDTO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public UserDTO setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public UserDTO setGender(Integer gender) {
|
||||
this.gender = gender;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
spring:
|
||||
application:
|
||||
name: user-service # Spring 应用名
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos 作为注册中心的配置项
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址 "'lb://'+serviceId"
|
||||
|
||||
server:
|
||||
port: ${random.int[10000,19999]} # 服务器端口。默认为 8080
|
||||
# port: 18080 # 服务器端口。默认为 8080
|
||||
42
labx-08/pom.xml
Normal file
42
labx-08/pom.xml
Normal file
@@ -0,0 +1,42 @@
|
||||
<?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>labs-parent</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-08</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>labx-08-sc-gateway-demo01</module>
|
||||
<module>labx-08-sc-gateway-demo01-test</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo02-registry</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo03-config-apollo</module>
|
||||
<module>labx-08-sc-gateway-demo03-config-nacos</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo04</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo05-custom-gateway-filter</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo06-rate-limiter</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo07-hystrix</module>
|
||||
<module>labx-08-sc-gateway-demo07-sentinel</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo08-custom-global-filter</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo09-actuator</module>
|
||||
|
||||
<module>labx-08-sc-gateway-demo10-troubleshooting</module>
|
||||
|
||||
<module>labx-08-sc-user-service</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
69
labx-09/labx-09-sc-apollo-demo-auto-refresh/pom.xml
Normal file
69
labx-09/labx-09-sc-apollo-demo-auto-refresh/pom.xml
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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>labx-09</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-09-sc-apollo-demo-auto-refresh</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud 基础依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId>
|
||||
<version>1.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "order")
|
||||
public class OrderProperties {
|
||||
|
||||
/**
|
||||
* 订单支付超时时长,单位:秒。
|
||||
*/
|
||||
private Integer payTimeoutSeconds;
|
||||
|
||||
/**
|
||||
* 订单创建频率,单位:秒
|
||||
*/
|
||||
private Integer createFrequencySeconds;
|
||||
|
||||
// /**
|
||||
// * 配置描述
|
||||
// */
|
||||
// private String desc;
|
||||
|
||||
public Integer getPayTimeoutSeconds() {
|
||||
return payTimeoutSeconds;
|
||||
}
|
||||
|
||||
public OrderProperties setPayTimeoutSeconds(Integer payTimeoutSeconds) {
|
||||
this.payTimeoutSeconds = payTimeoutSeconds;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getCreateFrequencySeconds() {
|
||||
return createFrequencySeconds;
|
||||
}
|
||||
|
||||
public OrderProperties setCreateFrequencySeconds(Integer createFrequencySeconds) {
|
||||
this.createFrequencySeconds = createFrequencySeconds;
|
||||
return this;
|
||||
}
|
||||
|
||||
// public String getDesc() {
|
||||
// return desc;
|
||||
// }
|
||||
//
|
||||
// public OrderProperties setDesc(String desc) {
|
||||
// this.desc = desc;
|
||||
// return this;
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo.controller;
|
||||
|
||||
import cn.iocoder.springcloud.labx09.apollodemo.config.OrderProperties;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private OrderProperties orderProperties;
|
||||
|
||||
/**
|
||||
* 测试 @ConfigurationProperties 注解的配置属性类
|
||||
*/
|
||||
@GetMapping("/test01")
|
||||
public OrderProperties test01() {
|
||||
return orderProperties;
|
||||
}
|
||||
|
||||
@Value(value = "${order.pay-timeout-seconds}")
|
||||
private Integer payTimeoutSeconds;
|
||||
@Value(value = "${order.create-frequency-seconds}")
|
||||
private Integer createFrequencySeconds;
|
||||
|
||||
/**
|
||||
* 测试 @Value 注解的属性
|
||||
*/
|
||||
@GetMapping("/test02")
|
||||
public Map<String, Object> test02() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("payTimeoutSeconds", payTimeoutSeconds);
|
||||
result.put("createFrequencySeconds", createFrequencySeconds);
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/logger")
|
||||
public void logger() {
|
||||
logger.debug("[logger][测试一下]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo.listener;
|
||||
|
||||
import com.ctrip.framework.apollo.core.ConfigConsts;
|
||||
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
|
||||
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ApolloPropertiesRefresher implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@ApolloConfigChangeListener(value = ConfigConsts.NAMESPACE_APPLICATION)
|
||||
public void onChange(ConfigChangeEvent changeEvent) {
|
||||
this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 7070 # 避免和本地的 Apollo Portal 端口冲突
|
||||
|
||||
app:
|
||||
id: demo-application # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
meta: http://127.0.0.1:8080 # Apollo Meta Server 地址
|
||||
bootstrap:
|
||||
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
|
||||
eagerLoad:
|
||||
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
|
||||
namespaces: application # 使用的 Apollo 的命名空间,默认为 application。
|
||||
85
labx-09/labx-09-sc-apollo-demo-jasypt/pom.xml
Normal file
85
labx-09/labx-09-sc-apollo-demo-jasypt/pom.xml
Normal file
@@ -0,0 +1,85 @@
|
||||
<?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>labx-09</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-09-sc-apollo-demo-jasypt</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud 基础依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId>
|
||||
<version>1.5.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Jasypt 实现自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.github.ulisesbocchio</groupId>
|
||||
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<!-- <version>2.1.2</version>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
</dependency>
|
||||
|
||||
<!-- 方便等会写单元测试 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
@Value("${xxx-password:}")
|
||||
private String xxxPassword;
|
||||
|
||||
@GetMapping("/test")
|
||||
public String test() {
|
||||
return xxxPassword;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 7070 # 避免和本地的 Apollo Portal 端口冲突
|
||||
|
||||
app:
|
||||
id: demo-application-jasypt # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
meta: http://127.0.0.1:8080 # Apollo Meta Server 地址
|
||||
bootstrap:
|
||||
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
|
||||
eagerLoad:
|
||||
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
|
||||
namespaces: application # 使用的 Apollo 的命名空间,默认为 application。
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo;
|
||||
|
||||
import org.jasypt.encryption.StringEncryptor;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class JasyptTest {
|
||||
|
||||
@Autowired
|
||||
private StringEncryptor encryptor;
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
// 第一个加密
|
||||
String password = "woshimima";
|
||||
System.out.println(encryptor.encrypt(password));
|
||||
|
||||
// 第二个加密
|
||||
password = "bushimima";
|
||||
System.out.println(encryptor.encrypt(password));
|
||||
}
|
||||
|
||||
@Value("${xxx-password:}")
|
||||
private String xxxPassword;
|
||||
|
||||
@Test
|
||||
public void print() {
|
||||
System.out.println(xxxPassword);
|
||||
}
|
||||
|
||||
// @Value("${jasypt.encryptor.password}")
|
||||
// private String password;
|
||||
|
||||
}
|
||||
69
labx-09/labx-09-sc-apollo-demo-multi/pom.xml
Normal file
69
labx-09/labx-09-sc-apollo-demo-multi/pom.xml
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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>labx-09</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-09-sc-apollo-demo-multi</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud 基础依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId>
|
||||
<version>1.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 启动 Spring Boot 应用
|
||||
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class);
|
||||
|
||||
// 查看 Environment
|
||||
Environment environment = context.getEnvironment();
|
||||
System.out.println(environment);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 7070 # 避免和本地的 Apollo Portal 端口冲突
|
||||
|
||||
app:
|
||||
id: demo-application-multi # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
meta: http://127.0.0.1:8080 # Apollo Meta Server 地址
|
||||
bootstrap:
|
||||
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
|
||||
eagerLoad:
|
||||
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
|
||||
namespaces: application, db # 使用的 Apollo 的命名空间,默认为 application。
|
||||
69
labx-09/labx-09-sc-apollo-demo-profiles/pom.xml
Normal file
69
labx-09/labx-09-sc-apollo-demo-profiles/pom.xml
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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>labx-09</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-09-sc-apollo-demo-profiles</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud 基础依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId>
|
||||
<version>1.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
app:
|
||||
id: demo-application-profiles # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
meta: http://127.0.0.1:8080 # Apollo Meta Server 地址
|
||||
bootstrap:
|
||||
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
|
||||
eagerLoad:
|
||||
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
|
||||
namespaces: application # 使用的 Apollo 的命名空间,默认为 application。
|
||||
@@ -0,0 +1,9 @@
|
||||
app:
|
||||
id: demo-application-profiles # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
meta: http://127.0.0.1:18080 # Apollo Meta Server 地址
|
||||
bootstrap:
|
||||
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
|
||||
eagerLoad:
|
||||
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
|
||||
namespaces: application # 使用的 Apollo 的命名空间,默认为 application。
|
||||
@@ -0,0 +1,6 @@
|
||||
#server:
|
||||
# port: 7070
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: demo-application
|
||||
69
labx-09/labx-09-sc-apollo-demo/pom.xml
Normal file
69
labx-09/labx-09-sc-apollo-demo/pom.xml
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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>labx-09</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-09-sc-apollo-demo</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud 基础依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId>
|
||||
<version>1.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "order")
|
||||
public class OrderProperties {
|
||||
|
||||
/**
|
||||
* 订单支付超时时长,单位:秒。
|
||||
*/
|
||||
private Integer payTimeoutSeconds;
|
||||
|
||||
/**
|
||||
* 订单创建频率,单位:秒
|
||||
*/
|
||||
private Integer createFrequencySeconds;
|
||||
|
||||
// /**
|
||||
// * 配置描述
|
||||
// */
|
||||
// private String desc;
|
||||
|
||||
public Integer getPayTimeoutSeconds() {
|
||||
return payTimeoutSeconds;
|
||||
}
|
||||
|
||||
public OrderProperties setPayTimeoutSeconds(Integer payTimeoutSeconds) {
|
||||
this.payTimeoutSeconds = payTimeoutSeconds;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getCreateFrequencySeconds() {
|
||||
return createFrequencySeconds;
|
||||
}
|
||||
|
||||
public OrderProperties setCreateFrequencySeconds(Integer createFrequencySeconds) {
|
||||
this.createFrequencySeconds = createFrequencySeconds;
|
||||
return this;
|
||||
}
|
||||
|
||||
// public String getDesc() {
|
||||
// return desc;
|
||||
// }
|
||||
//
|
||||
// public OrderProperties setDesc(String desc) {
|
||||
// this.desc = desc;
|
||||
// return this;
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package cn.iocoder.springcloud.labx09.apollodemo.controller;
|
||||
|
||||
import cn.iocoder.springcloud.labx09.apollodemo.config.OrderProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
private OrderProperties orderProperties;
|
||||
|
||||
/**
|
||||
* 测试 @ConfigurationProperties 注解的配置属性类
|
||||
*/
|
||||
@GetMapping("/test01")
|
||||
public OrderProperties test01() {
|
||||
return orderProperties;
|
||||
}
|
||||
|
||||
@Value(value = "${order.pay-timeout-seconds}")
|
||||
private Integer payTimeoutSeconds;
|
||||
@Value(value = "${order.create-frequency-seconds}")
|
||||
private Integer createFrequencySeconds;
|
||||
|
||||
/**
|
||||
* 测试 @Value 注解的属性
|
||||
*/
|
||||
@GetMapping("/test02")
|
||||
public Map<String, Object> test02() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("payTimeoutSeconds", payTimeoutSeconds);
|
||||
result.put("createFrequencySeconds", createFrequencySeconds);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 7070 # 避免和本地的 Apollo Portal 端口冲突
|
||||
|
||||
app:
|
||||
id: demo-application # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
meta: http://127.0.0.1:8080 # Apollo Meta Server 地址
|
||||
bootstrap:
|
||||
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
|
||||
eagerLoad:
|
||||
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
|
||||
namespaces: application # 使用的 Apollo 的命名空间,默认为 application。
|
||||
22
labx-09/pom.xml
Normal file
22
labx-09/pom.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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>labs-parent</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-09</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>labx-09-sc-apollo-demo</module>
|
||||
<module>labx-09-sc-apollo-demo-profiles</module>
|
||||
<module>labx-09-sc-apollo-demo-auto-refresh</module>
|
||||
<module>labx-09-sc-apollo-demo-jasypt</module>
|
||||
<module>labx-09-sc-apollo-demo-multi</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
58
labx-10/labx-10-sc-stream-rabbitmq-consumer-delay/pom.xml
Normal file
58
labx-10/labx-10-sc-stream-rabbitmq-consumer-delay/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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>labx-10</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-10-sc-stream-rabbitmq-consumer-delay</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Stream RabbitMQ 相关依赖,将 RabbitMQ 作为消息队列,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo;
|
||||
|
||||
import cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener.MySink;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableBinding(MySink.class)
|
||||
public class ConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener;
|
||||
|
||||
import cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.message.Demo01Message;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Demo01Consumer {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@StreamListener(MySink.DEMO01_INPUT)
|
||||
public void onMessage(@Payload Demo01Message message) {
|
||||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener;
|
||||
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
public interface MySink {
|
||||
|
||||
String DEMO01_INPUT = "demo01-input";
|
||||
|
||||
@Input(DEMO01_INPUT)
|
||||
SubscribableChannel demo01Input();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.message;
|
||||
|
||||
/**
|
||||
* 示例 01 的 Message 消息
|
||||
*/
|
||||
public class Demo01Message {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
public Demo01Message setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Demo01Message{" +
|
||||
"id=" + id +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer-application
|
||||
cloud:
|
||||
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
|
||||
stream:
|
||||
# Binder 配置项,对应 BinderProperties Map
|
||||
binders:
|
||||
rabbit001:
|
||||
type: rabbit # 设置 Binder 的类型
|
||||
environment: # 设置 Binder 的环境配置
|
||||
# 如果是 RabbitMQ 类型的时候,则对应的是 RabbitProperties 类
|
||||
spring:
|
||||
rabbitmq:
|
||||
host: 127.0.0.1 # RabbitMQ 服务的地址
|
||||
port: 5672 # RabbitMQ 服务的端口
|
||||
username: guest # RabbitMQ 服务的账号
|
||||
password: guest # RabbitMQ 服务的密码
|
||||
# Binding 配置项,对应 BindingProperties Map
|
||||
bindings:
|
||||
demo01-input:
|
||||
destination: DEMO-TOPIC-02 # 目的地。这里使用 RabbitMQ Exchange
|
||||
content-type: application/json # 内容格式。这里使用 JSON
|
||||
group: demo01-consumer-group-DEMO-TOPIC-01 # 消费者分组
|
||||
binder: rabbit001 # 设置使用的 Binder 名字
|
||||
# RabbitMQ 自定义 Binding 配置项,对应 RabbitBindingProperties Map
|
||||
rabbit:
|
||||
bindings:
|
||||
demo01-input:
|
||||
# RabbitMQ Consumer 配置项,对应 RabbitConsumerProperties 类
|
||||
consumer:
|
||||
delayed-exchange: true # 是否使用 x-delayed-message 类型的 Exchange,即延迟消息,默认为 false
|
||||
|
||||
server:
|
||||
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者
|
||||
@@ -0,0 +1,35 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer-application
|
||||
cloud:
|
||||
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
|
||||
stream:
|
||||
# Binder 配置项,对应 BinderProperties Map
|
||||
binders:
|
||||
rabbit001:
|
||||
type: rabbit # 设置 Binder 的类型
|
||||
environment: # 设置 Binder 的环境配置
|
||||
# 如果是 RabbitMQ 类型的时候,则对应的是 RabbitProperties 类
|
||||
spring:
|
||||
rabbitmq:
|
||||
host: 127.0.0.1 # RabbitMQ 服务的地址
|
||||
port: 5672 # RabbitMQ 服务的端口
|
||||
username: guest # RabbitMQ 服务的账号
|
||||
password: guest # RabbitMQ 服务的密码
|
||||
# Binding 配置项,对应 BindingProperties Map
|
||||
bindings:
|
||||
demo01-input:
|
||||
destination: DEMO-TOPIC-02 # 目的地。这里使用 RabbitMQ Exchange
|
||||
content-type: application/json # 内容格式。这里使用 JSON
|
||||
group: demo01-consumer-group-DEMO-TOPIC-01 # 消费者分组
|
||||
binder: rabbit001 # 设置使用的 Binder 名字
|
||||
# RabbitMQ 自定义 Binding 配置项,对应 RabbitBindingProperties Map
|
||||
rabbit:
|
||||
bindings:
|
||||
demo01-input:
|
||||
# RabbitMQ Consumer 配置项,对应 RabbitConsumerProperties 类
|
||||
consumer:
|
||||
delayed-exchange: true # 是否使用 x-delayed-message 类型的 Exchange,即延迟消息,默认为 false
|
||||
|
||||
server:
|
||||
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者
|
||||
58
labx-10/labx-10-sc-stream-rabbitmq-consumer-demo/pom.xml
Normal file
58
labx-10/labx-10-sc-stream-rabbitmq-consumer-demo/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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>labx-10</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-10-sc-stream-rabbitmq-consumer-demo</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Stream RabbitMQ 相关依赖,将 RabbitMQ 作为消息队列,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo;
|
||||
|
||||
import cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener.MySink;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableBinding(MySink.class)
|
||||
public class ConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener;
|
||||
|
||||
import cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.message.Demo01Message;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Demo01Consumer {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@StreamListener(MySink.DEMO01_INPUT)
|
||||
public void onMessage(@Payload Demo01Message message) {
|
||||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user