bootstrap的优先级高于application(bootstrap一般用于微服务的使用)
bootstrap(首先在注册中心Nacos)
server:
port: 8086
spring:
application:
//pom.xml文件中该目录下名称的引用
//boyun-open-gateway
// ${project.artifactId}
// ${boyun-open.version}
//jar
// 博云开放API网关
name: @artifactId@
cloud:
nacos:
discovery:
//Nacos的地址 (vim /etc/host修改服务器的端口号),${NACOS-PORT:8848}当${NACOS-PORT}获取不到数据是是8848,否则是${NACOS-PORT}
server-addr: 11.22.33.44:${NACOS-PORT:8848}
//服务器的配置账号和密码
config:
username: 111
password: 1111
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml
shared-configs: -
//去找配置文件 - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
extension-configs:
- data-id: clusterDemo-gateway-flow-rules.json
group: DEFAULT_GROUP
refresh: true
# - data-id: shareconfig4.yml
# group: SHARE4_GROUP
# refresh: true
profiles:
active: dev
application(如果在Nacos上找不到application配置默认读取本地的配置文件)
#logging:
# level:
# root: trace
spring:
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
dynamic:
druid:
initial-size: 1
min-idle: 1
max-active: 2
primary: openplatform #设置默认的数据源或者数据源组,默认值即为master
# strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
datasource:
boyunbase: #数据源名称可随意取
mapper: com.bsj.openBoot.mapper.bsj #当前数据源对应的mapper目录不能多个数据源相同
url: jdbc:mysql://地址?allowMultiQueries=true&tinyInt1isBit=false
driverClassName: com.mysql.cj.jdbc.Driver
username: 1111
password: 11111
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
# druid 配置: https://github.com/alibaba/druid/wiki/
openplatform: #数据源名称可随意取
mapper: com.bsj.openBoot.mapper.openPlatform #当前数据源对应的mapper目录不能多个数据源相同
url: jdbc:mysql://“数据库地址”?allowMultiQueries=true&tinyInt1isBit=false
driverClassName: com.mysql.cj.jdbc.Driver
username: 111
password: 111
mvc:
throw-exception-if-no-handler-found: true
redis:
# host: 1111
# port: 111
# password: 11
password: 11
cluster:
nodes: null,null,null
max-redirects: 5
application:
name: boyun-open-gateway
cloud:
gateway:
enabled: true
discovery:
locator:
lower-case-service-id: true
//说明boyun-open-car这个下面的请求路径包含v1/open/car的都要先走RequestRateLimiter这个方法
routes:
- id: car
uri: lb://boyun-open-car
predicates:
- Path=/v1/open/car/**
filters:
- name: RequestRateLimiter
stream:
bindings:
limitInput:
destination: ${boyun.rocketmq.limit.topic}
group: ${boyun.rocketmq.limit.pid}
comboInput:
destination: ${boyun.rocketmq.combo.topic}
group: ${boyun.rocketmq.combo.pid}
boyun:
rocketmq:
limit:
tag: 1
topic: 2
pid: 3
combo:
tag: 4
topic: 5
pid: 6
redis:
maxIdle: 20
maxTotal: 30
maxWaitMillis: 5000
parallelNum: 6
host: null;null;null
port: 111
password: 1111
这里使用了SpringCloud rocketMq:
实例:
//@SpringCloudApplication
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.bsj.boyun.open.gateway.mapper")
//启动类上加上这个注解
@EnableBinding(LimitCustomBinding.class)
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
LimitCustomBinding.class
消费者的接口
public interface LimitCustomBinding {
String LIMIT_INPUT = "limitInput";
String COMBO_INPUT = "comboInput";
@Input(LIMIT_INPUT)
SubscribableChannel limitInput();
@Input(COMBO_INPUT)
SubscribableChannel comboInput();
}
消费者接口的实现:
@Slf4j
@Component
public class LimitConsumer {
@StreamListener(LimitCustomBinding.LIMIT_INPUT)
public void inputConsumerForLimit(GatewayLimitDTO message) {
log.info("从Binding-{}收到信息-{}", LimitCustomBinding.LIMIT_INPUT, message);
RateLimitCached.putLimit(message);
}
@StreamListener(LimitCustomBinding.COMBO_INPUT)
public void inputConsumerForCombo(KeyComboDTO message) {
log.info("从Binding-{}收到信息-{}", LimitCustomBinding.COMBO_INPUT, message);
RateLimitCached.putCombo(message);
}
}
生产者的接口:
public interface LimitCustomBinding {
String LIMIT_OUTPUT = "limitOutput";
String COMBO_OUTPUT = "comboOutput";
@Output(LIMIT_OUTPUT)
MessageChannel limitOutput();
@Output(COMBO_OUTPUT)
MessageChannel comboOutput();
}
生产者接口的实现:
@Slf4j
@Component
public class LimitProducer {
@Value("${boyun.rocketmq.limit.tag:limitTag}")
String limitTag;
@Value("${boyun.rocketmq.combo.tag:comboTag}")
String comboTag;
@Autowired
LimitCustomBinding limitCustomBinding;
public void sendMessageForLimit(GatewayLimitDTO msg){
log.info("发送消息:[{}]", JsonUtil.toJson(msg));
Map<String, Object> headers = new HashMap<>();
headers.put(MessageConst.PROPERTY_TAGS, limitTag);
MessageHeaders messageHeaders = new MessageHeaders(headers);
final Message<GatewayLimitDTO> message = MessageBuilder.createMessage(msg, messageHeaders);
this.limitCustomBinding.limitOutput().send(message);
}
public void sendMessageForCombo(KeyComboDTO msg){
log.info("发送消息:[{}]", JsonUtil.toJson(msg));
Map<String, Object> headers = new HashMap<>();
headers.put(MessageConst.PROPERTY_TAGS, comboTag);
MessageHeaders messageHeaders = new MessageHeaders(headers);
final Message<KeyComboDTO> message = MessageBuilder.createMessage(msg, messageHeaders);
this.limitCustomBinding.comboOutput().send(message);
}
}