init
This commit is contained in:
51
dimp-common/dimp-common-sentinel/pom.xml
Normal file
51
dimp-common/dimp-common-sentinel/pom.xml
Normal file
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>dimp-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>dimp-common-sentinel</artifactId>
|
||||
|
||||
<description>
|
||||
dimp-common-sentinel 限流模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sentinel Datasource Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-apache-dubbo3-adapter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>dimp-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,52 @@
|
||||
package org.dromara.common.sentinel.config;
|
||||
|
||||
import com.alibaba.cloud.commons.lang.StringUtils;
|
||||
import com.alibaba.cloud.sentinel.SentinelProperties;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.csp.sentinel.init.InitExecutor;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
import org.dromara.common.core.utils.StreamUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Lion Li
|
||||
*/
|
||||
@AutoConfiguration(before = SentinelAutoConfiguration.class)
|
||||
@EnableConfigurationProperties({SentinelProperties.class, SentinelCustomProperties.class})
|
||||
public class SentinelCustomAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private SentinelProperties properties;
|
||||
@Autowired
|
||||
private SentinelCustomProperties customProperties;
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
@Bean
|
||||
public Object sentinelInit() {
|
||||
if (StringUtils.isNotBlank(customProperties.getServerName())) {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(customProperties.getServerName());
|
||||
String serverList = StreamUtils.join(instances, instance ->
|
||||
String.format("http://%s:%s", instance.getHost(), instance.getPort()));
|
||||
System.setProperty(TransportConfig.CONSOLE_SERVER, serverList);
|
||||
} else {
|
||||
if (StringUtils.isEmpty(System.getProperty(TransportConfig.CONSOLE_SERVER))
|
||||
&& StringUtils.isNotBlank(properties.getTransport().getDashboard())) {
|
||||
System.setProperty(TransportConfig.CONSOLE_SERVER,
|
||||
properties.getTransport().getDashboard());
|
||||
}
|
||||
}
|
||||
// 手动初始化 sentinel
|
||||
InitExecutor.doInit();
|
||||
return new Object();
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package org.dromara.common.sentinel.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* sentinel自定义配置类
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "spring.cloud.sentinel.transport")
|
||||
public class SentinelCustomProperties {
|
||||
|
||||
private String serverName;
|
||||
|
||||
}
|
@@ -0,0 +1 @@
|
||||
org.dromara.common.sentinel.config.SentinelCustomAutoConfiguration
|
Reference in New Issue
Block a user