Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
springcloud-sentinel
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
senthink-project-origin
springcloud-sentinel
Commits
5e268211
Commit
5e268211
authored
Apr 02, 2020
by
李根
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加第二种方式实现FunInit接口,在初始化的时候从nacos获取配置
parent
c011fa5b
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
97 additions
and
46 deletions
+97
-46
sentinel-demo-springboot-nacos/src/main/java/com/alibaba/csp/sentinel/demo/springboot/nacos/config/AopConfiguration.java
+1
-0
sentinel-demo-springboot-nacos/src/main/java/com/alibaba/csp/sentinel/demo/springboot/nacos/config/ApplicationRunnerImpl.java
+3
-46
sentinel-demo-springboot-nacos/src/main/java/com/alibaba/csp/sentinel/demo/springboot/nacos/config/NacosStartService.java
+68
-0
sentinel-demo-springboot-nacos/src/main/java/com/alibaba/csp/sentinel/demo/springboot/nacos/inti/DemoNacosInitFunc.java
+23
-0
sentinel-demo-springboot-nacos/src/main/resources/services/com.alibaba.csp.sentinel.init.InitFunc
+2
-0
No files found.
sentinel-demo-springboot-nacos/src/main/java/com/alibaba/csp/sentinel/demo/springboot/nacos/config/AopConfiguration.java
View file @
5e268211
...
...
@@ -23,6 +23,7 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public
class
AopConfiguration
{
// spring-colud-alibaba-sentinel 引入sentinel 不需要手动注入下面Bean 可以直接使用@SentinelResource
// @Bean
// public SentinelResourceAspect sentinelResourceAspect() {
...
...
sentinel-demo-springboot-nacos/src/main/java/com/alibaba/csp/sentinel/demo/springboot/nacos/config/ApplicationRunnerImpl.java
View file @
5e268211
package
com
.
alibaba
.
csp
.
sentinel
.
demo
.
springboot
.
nacos
.
config
;
import
com.alibaba.cloud.sentinel.SentinelProperties
;
import
com.alibaba.cloud.sentinel.datasource.RuleType
;
import
com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties
;
import
com.alibaba.csp.sentinel.datasource.ReadableDataSource
;
import
com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource
;
import
com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule
;
import
com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowRule
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager
;
import
com.alibaba.fastjson.JSON
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.Properties
;
/**
* @author lnj
* createTime 2018-11-07 22:37
...
...
@@ -28,39 +12,11 @@ import java.util.Properties;
@Component
public
class
ApplicationRunnerImpl
implements
ApplicationRunner
{
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
@Autowired
SentinelProperties
sentinelProperties
;
private
NacosStartService
nacosStartService
;
@Override
public
void
run
(
ApplicationArguments
args
)
throws
Exception
{
logger
.
info
(
"[NacosSource初始化,从Nacos中获取熔断规则]"
);
sentinelProperties
.
getDatasource
().
entrySet
().
stream
().
filter
(
map
->
{
return
map
.
getValue
().
getNacos
()
!=
null
;
}).
forEach
(
map
->
{
NacosDataSourceProperties
nacos
=
map
.
getValue
().
getNacos
();
Properties
properties
=
new
Properties
()
{{
setProperty
(
"serverAddr"
,
nacos
.
getServerAddr
());
setProperty
(
"namespace"
,
"31b168cb-51ee-4b8b-adfa-29b42d4afa0b"
);
}};
if
(
Objects
.
equals
(
nacos
.
getRuleType
(),
RuleType
.
FLOW
))
{
ReadableDataSource
<
String
,
List
<
FlowRule
>>
flowRuleDataSource
=
new
NacosDataSource
<>(
properties
,
nacos
.
getGroupId
(),
nacos
.
getDataId
(),
source
->
JSON
.
parseObject
(
source
,
new
com
.
alibaba
.
fastjson
.
TypeReference
<
List
<
FlowRule
>>()
{
}));
FlowRuleManager
.
register2Property
(
flowRuleDataSource
.
getProperty
());
}
if
(
Objects
.
equals
(
nacos
.
getRuleType
(),
RuleType
.
DEGRADE
))
{
ReadableDataSource
<
String
,
List
<
DegradeRule
>>
degradeRuleDataSource
=
new
NacosDataSource
<>(
properties
,
nacos
.
getGroupId
(),
nacos
.
getDataId
(),
source
->
JSON
.
parseObject
(
source
,
new
com
.
alibaba
.
fastjson
.
TypeReference
<
List
<
DegradeRule
>>()
{
}));
DegradeRuleManager
.
register2Property
(
degradeRuleDataSource
.
getProperty
());
}
});
nacosStartService
.
initNacosConfigFromNacos
();
}
}
\ No newline at end of file
sentinel-demo-springboot-nacos/src/main/java/com/alibaba/csp/sentinel/demo/springboot/nacos/config/NacosStartService.java
0 → 100644
View file @
5e268211
package
com
.
alibaba
.
csp
.
sentinel
.
demo
.
springboot
.
nacos
.
config
;
import
com.alibaba.cloud.sentinel.SentinelProperties
;
import
com.alibaba.cloud.sentinel.datasource.RuleType
;
import
com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties
;
import
com.alibaba.csp.sentinel.datasource.ReadableDataSource
;
import
com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource
;
import
com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule
;
import
com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowRule
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager
;
import
com.alibaba.fastjson.JSON
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.Properties
;
/**
* @author ligen
* @title: NacosStartService
* @projectName dashboardplus
* @description:
* @date 2020/4/214:08
*/
@Service
public
class
NacosStartService
{
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
@Autowired
private
SentinelProperties
sentinelProperties
;
public
void
initNacosConfigFromNacos
()
{
logger
.
info
(
"[NacosSource初始化,从Nacos中获取熔断规则]"
);
sentinelProperties
.
getDatasource
().
entrySet
().
stream
().
filter
(
map
->
{
return
map
.
getValue
().
getNacos
()
!=
null
;
}).
forEach
(
map
->
{
NacosDataSourceProperties
nacos
=
map
.
getValue
().
getNacos
();
Properties
properties
=
new
Properties
()
{{
setProperty
(
"serverAddr"
,
nacos
.
getServerAddr
());
setProperty
(
"namespace"
,
"31b168cb-51ee-4b8b-adfa-29b42d4afa0b"
);
}};
if
(
Objects
.
equals
(
nacos
.
getRuleType
(),
RuleType
.
FLOW
))
{
ReadableDataSource
<
String
,
List
<
FlowRule
>>
flowRuleDataSource
=
new
NacosDataSource
<>(
properties
,
nacos
.
getGroupId
(),
nacos
.
getDataId
(),
source
->
JSON
.
parseObject
(
source
,
new
com
.
alibaba
.
fastjson
.
TypeReference
<
List
<
FlowRule
>>()
{
}));
FlowRuleManager
.
register2Property
(
flowRuleDataSource
.
getProperty
());
}
if
(
Objects
.
equals
(
nacos
.
getRuleType
(),
RuleType
.
DEGRADE
))
{
ReadableDataSource
<
String
,
List
<
DegradeRule
>>
degradeRuleDataSource
=
new
NacosDataSource
<>(
properties
,
nacos
.
getGroupId
(),
nacos
.
getDataId
(),
source
->
JSON
.
parseObject
(
source
,
new
com
.
alibaba
.
fastjson
.
TypeReference
<
List
<
DegradeRule
>>()
{
}));
DegradeRuleManager
.
register2Property
(
degradeRuleDataSource
.
getProperty
());
}
});
}
}
sentinel-demo-springboot-nacos/src/main/java/com/alibaba/csp/sentinel/demo/springboot/nacos/inti/DemoNacosInitFunc.java
0 → 100644
View file @
5e268211
package
com
.
alibaba
.
csp
.
sentinel
.
demo
.
springboot
.
nacos
.
inti
;
import
com.alibaba.csp.sentinel.demo.springboot.nacos.config.NacosStartService
;
import
com.alibaba.csp.sentinel.init.InitFunc
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
/**
* @author lnj
* createTime 2018-11-07 22:37
**/
@Component
public
class
DemoNacosInitFunc
implements
InitFunc
{
@Autowired
private
NacosStartService
nacosStartService
;
@Override
public
void
init
()
throws
Exception
{
nacosStartService
.
initNacosConfigFromNacos
();
}
}
\ No newline at end of file
sentinel-demo-springboot-nacos/src/main/resources/services/com.alibaba.csp.sentinel.init.InitFunc
0 → 100644
View file @
5e268211
com.alibaba.csp.sentinel.demo.springboot.nacos.init.DemoNacosInitFunc
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment