Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
GatewaywithDynamicRoute
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhang 张号彬
GatewaywithDynamicRoute
Commits
0737f607
Commit
0737f607
authored
Feb 21, 2021
by
zhang 张号彬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改NacosConfigListener
parent
6db69bca
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
47 additions
and
100 deletions
+47
-100
YstGatewayApplication.java
...y/src/main/java/com/elitesland/YstGatewayApplication.java
+0
-5
GatewayRouteDefinition.java
...va/com/elitesland/router/bean/GatewayRouteDefinition.java
+0
-1
NacosConfigListener.java
...ava/com/elitesland/router/config/NacosConfigListener.java
+10
-23
RouteController.java
...ava/com/elitesland/router/controller/RouteController.java
+21
-23
DynamicRouteService.java
...va/com/elitesland/router/service/DynamicRouteService.java
+8
-5
GatewayRoutesInNacosConfigService.java
...and/router/service/GatewayRoutesInNacosConfigService.java
+3
-21
GateWayNacosUtils.java
...in/java/com/elitesland/router/util/GateWayNacosUtils.java
+3
-2
application-dev.yml
yst-base-gateway/src/main/resources/application-dev.yml
+0
-13
application.yml
yst-base-gateway/src/main/resources/application.yml
+1
-5
bootstrap.yml
yst-base-gateway/src/main/resources/bootstrap.yml
+1
-2
No files found.
yst-base-gateway/src/main/java/com/elitesland/YstGatewayApplication.java
View file @
0737f607
package
com
.
elitesland
;
package
com
.
elitesland
;
import
com.elitesland.router.config.NacosConfigListener
;
import
com.elitesland.router.service.DynamicRouteService
;
import
com.elitesland.router.util.GateWayNacosUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
;
import
org.springframework.cloud.client.discovery.EnableDiscoveryClient
;
import
org.springframework.cloud.client.discovery.EnableDiscoveryClient
;
import
org.springframework.context.annotation.Bean
;
/**
/**
* <p>
* <p>
...
...
yst-base-gateway/src/main/java/com/elitesland/router/bean/GatewayRouteDefinition.java
View file @
0737f607
...
@@ -21,5 +21,4 @@ public class GatewayRouteDefinition{
...
@@ -21,5 +21,4 @@ public class GatewayRouteDefinition{
private
String
uri
;
private
String
uri
;
//路由执行的顺序
//路由执行的顺序
private
int
order
=
0
;
private
int
order
=
0
;
//此处省略get和set方法
}
}
\ No newline at end of file
yst-base-gateway/src/main/java/com/elitesland/router/config/NacosConfigListener.java
View file @
0737f607
...
@@ -18,11 +18,8 @@ import org.springframework.cloud.gateway.route.RouteDefinition;
...
@@ -18,11 +18,8 @@ import org.springframework.cloud.gateway.route.RouteDefinition;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
import
java.net.URI
;
import
java.net.URI
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.concurrent.Executor
;
import
java.util.concurrent.Executor
;
/*
/*
...
@@ -39,13 +36,9 @@ public class NacosConfigListener implements CommandLineRunner {
...
@@ -39,13 +36,9 @@ public class NacosConfigListener implements CommandLineRunner {
private
DynamicRouteService
dynamicRouteService
;
private
DynamicRouteService
dynamicRouteService
;
@Autowired
@Autowired
private
InMemoryRouteDefinitionRepository
inMemoryRouteDefinitionRepository
;
private
InMemoryRouteDefinitionRepository
inMemoryRouteDefinitionRepository
;
//由于可能根据需要在定时任务中引用该list,因此使用线程安全的SynchronizedListlist
public
static
List
<
GatewayRouteDefinition
>
list
=
Collections
.
synchronizedList
(
new
ArrayList
<>());
@Override
@Override
public
void
run
(
String
...
args
)
{
public
void
run
(
String
...
args
)
{
getConfig
();
getConfig
();
}
}
private
void
getConfig
()
{
private
void
getConfig
()
{
...
@@ -54,14 +47,13 @@ public class NacosConfigListener implements CommandLineRunner {
...
@@ -54,14 +47,13 @@ public class NacosConfigListener implements CommandLineRunner {
ConfigService
configService
=
gateWayNacosUtils
.
getNacosConfigService
();
ConfigService
configService
=
gateWayNacosUtils
.
getNacosConfigService
();
Gson
gson
=
new
Gson
();
Gson
gson
=
new
Gson
();
//注册nacos配置监听,当nacos配置信息发生改变时刷新本地内存中Gateway Route规则信息
//注册nacos配置监听,当nacos配置信息发生改变时刷新本地内存中Gateway Route规则信息
refreshLocalRoute
(
routeDefinitions
,
gson
,
true
);
refreshLocalRoute
(
routeDefinitions
,
gson
,
true
);
String
group
=
gateWayNacosUtils
.
getNacosConfigGroup
();
String
group
=
gateWayNacosUtils
.
getNacosConfigGroup
();
String
routeConfigDataId
=
gateWayNacosUtils
.
getNacosConfigDataIdForGateWayDefinition
();
String
routeConfigDataId
=
gateWayNacosUtils
.
getNacosConfigDataIdForGateWayDefinition
();
configService
.
addListener
(
routeConfigDataId
,
group
,
new
Listener
()
{
configService
.
addListener
(
routeConfigDataId
,
group
,
new
Listener
()
{
@Override
@Override
public
void
receiveConfigInfo
(
String
configInfo
)
{
public
void
receiveConfigInfo
(
String
configInfo
)
{
System
.
out
.
println
(
configInfo
+
"------->"
);
refreshLocalRoute
(
configInfo
,
gson
,
false
);
refreshLocalRoute
(
configInfo
,
gson
,
false
);
}
}
@Override
@Override
public
Executor
getExecutor
()
{
public
Executor
getExecutor
()
{
...
@@ -73,24 +65,22 @@ public class NacosConfigListener implements CommandLineRunner {
...
@@ -73,24 +65,22 @@ public class NacosConfigListener implements CommandLineRunner {
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
/**
/**
* 根据nacos配置信息,刷新本地Gateway RouteDefinition
* 根据nacos配置信息,刷新本地Gateway RouteDefinition
*/
*/
private
void
refreshLocalRoute
(
String
initConfigInfo
,
Gson
gson
,
boolean
t
)
{
private
void
refreshLocalRoute
(
String
initConfigInfo
,
Gson
gson
,
boolean
t
)
{
//删除内存中已存在的动态路由配置信息
//删除内存中已存在的动态路由配置信息
if
(
list
.
size
()
>
0
){
Flux
<
RouteDefinition
>
routeDefinitions
=
inMemoryRouteDefinitionRepository
.
getRouteDefinitions
();
list
.
forEach
(
gatewayRouteDefinition
->
{
//由于不存在空指针情况,因此无需空指针判断
dynamicRouteService
.
delete
(
gatewayRouteDefinition
.
getId
());
routeDefinitions
.
subscribe
(
routeDefinition
->
{
});
dynamicRouteService
.
delete
(
routeDefinition
.
getId
());
list
=
Collections
.
synchronizedList
(
new
ArrayList
<>());
});
}
JsonArray
jsonArray
=
gson
.
fromJson
(
initConfigInfo
,
JsonArray
.
class
);
JsonArray
jsonArray
=
gson
.
fromJson
(
initConfigInfo
,
JsonArray
.
class
);
jsonArray
.
forEach
(
routeDefinitionAsJsonObject
->
{
jsonArray
.
forEach
(
routeDefinitionAsJsonObject
->
{
GatewayRouteDefinition
gatewayRouteDefinition
=
gson
.
fromJson
(
routeDefinitionAsJsonObject
,
GatewayRouteDefinition
.
class
);
GatewayRouteDefinition
gatewayRouteDefinition
=
gson
.
fromJson
(
routeDefinitionAsJsonObject
,
GatewayRouteDefinition
.
class
);
RouteDefinition
definition
=
convertRouteDefinition
(
gatewayRouteDefinition
);
RouteDefinition
definition
=
convertRouteDefinition
(
gatewayRouteDefinition
);
dynamicRouteService
.
add
(
definition
);
dynamicRouteService
.
add
(
definition
);
list
.
add
(
gatewayRouteDefinition
);
});
});
}
}
...
@@ -101,7 +91,6 @@ public class NacosConfigListener implements CommandLineRunner {
...
@@ -101,7 +91,6 @@ public class NacosConfigListener implements CommandLineRunner {
RouteDefinition
definition
=
new
RouteDefinition
();
RouteDefinition
definition
=
new
RouteDefinition
();
definition
.
setId
(
gwdefinition
.
getId
());
definition
.
setId
(
gwdefinition
.
getId
());
definition
.
setOrder
(
gwdefinition
.
getOrder
());
definition
.
setOrder
(
gwdefinition
.
getOrder
());
//设置断言
//设置断言
ArrayList
<
PredicateDefinition
>
pdList
=
new
ArrayList
<>();
ArrayList
<
PredicateDefinition
>
pdList
=
new
ArrayList
<>();
ArrayList
<
GatewayPredicateDefinition
>
gatewayPredicateDefinitionList
=
gwdefinition
.
getPredicates
();
ArrayList
<
GatewayPredicateDefinition
>
gatewayPredicateDefinitionList
=
gwdefinition
.
getPredicates
();
...
@@ -112,7 +101,6 @@ public class NacosConfigListener implements CommandLineRunner {
...
@@ -112,7 +101,6 @@ public class NacosConfigListener implements CommandLineRunner {
pdList
.
add
(
predicate
);
pdList
.
add
(
predicate
);
});
});
definition
.
setPredicates
(
pdList
);
definition
.
setPredicates
(
pdList
);
//设置过滤器
//设置过滤器
ArrayList
<
FilterDefinition
>
filters
=
new
ArrayList
();
ArrayList
<
FilterDefinition
>
filters
=
new
ArrayList
();
ArrayList
<
GatewayFilterDefinition
>
gatewayFilters
=
gwdefinition
.
getFilters
();
ArrayList
<
GatewayFilterDefinition
>
gatewayFilters
=
gwdefinition
.
getFilters
();
...
@@ -123,7 +111,6 @@ public class NacosConfigListener implements CommandLineRunner {
...
@@ -123,7 +111,6 @@ public class NacosConfigListener implements CommandLineRunner {
filters
.
add
(
filter
);
filters
.
add
(
filter
);
});
});
definition
.
setFilters
(
filters
);
definition
.
setFilters
(
filters
);
URI
uri
=
null
;
URI
uri
=
null
;
if
(
gwdefinition
.
getUri
().
startsWith
(
"http"
))
{
if
(
gwdefinition
.
getUri
().
startsWith
(
"http"
))
{
uri
=
UriComponentsBuilder
.
fromHttpUrl
(
gwdefinition
.
getUri
()).
build
().
toUri
();
uri
=
UriComponentsBuilder
.
fromHttpUrl
(
gwdefinition
.
getUri
()).
build
().
toUri
();
...
...
yst-base-gateway/src/main/java/com/elitesland/router/controller/RouteController.java
View file @
0737f607
package
com
.
elitesland
.
router
.
controller
;
package
com
.
elitesland
.
router
.
controller
;
import
com.elitesland.router.bean.GatewayRouteDefinition
;
import
com.elitesland.router.bean.GatewayRouteDefinition
;
import
com.elitesland.router.service.DynamicRouteService
;
import
com.elitesland.router.service.DynamicRouteService
;
import
com.elitesland.router.service.GatewayRoutesInNacosConfigService
;
import
com.elitesland.router.service.GatewayRoutesInNacosConfigService
;
...
@@ -8,12 +7,16 @@ import com.google.gson.Gson;
...
@@ -8,12 +7,16 @@ import com.google.gson.Gson;
import
com.google.gson.JsonArray
;
import
com.google.gson.JsonArray
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator
;
import
org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator
;
import
org.springframework.cloud.gateway.route.InMemoryRouteDefinitionRepository
;
import
org.springframework.cloud.gateway.route.*
;
import
org.springframework.cloud.gateway.route.Route
;
import
org.springframework.cloud.gateway.route.RouteDefinition
;
import
org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder
;
import
org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder
;
import
org.springframework.cloud.gateway.support.ServerWebExchangeUtils
;
import
org.springframework.context.ApplicationEventPublisher
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.server.ServerWebExchangeDecorator
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
/*
/*
...
@@ -21,8 +24,8 @@ import java.util.ArrayList;
...
@@ -21,8 +24,8 @@ import java.util.ArrayList;
*@Date 2021-02-10 19:17
*@Date 2021-02-10 19:17
*@Description
*@Description
* http://localhost:9040/actuator/gateway/routes 通过acutator查询route配置信息
* http://localhost:9040/actuator/gateway/routes 通过acutator查询route配置信息
* 在生产环境中配置信息将通过redis进行查询及持久化操作
*/
*/
@RestController
@RestController
@RequestMapping
(
"/route"
)
@RequestMapping
(
"/route"
)
public
class
RouteController
{
public
class
RouteController
{
...
@@ -31,31 +34,27 @@ public class RouteController {
...
@@ -31,31 +34,27 @@ public class RouteController {
@Autowired
@Autowired
PropertiesRouteDefinitionLocator
PropertiesRouteDefinitionLocator
;
PropertiesRouteDefinitionLocator
PropertiesRouteDefinitionLocator
;
@Autowired
private
DynamicRouteService
dynamicRouteService
;
/**
/**
*增加路由
*增加路由
*/
*/
@PostMapping
(
"/addOrUpdate"
)
@PostMapping
(
"/addOrUpdate"
)
public
GatewayRouteDefinition
addOrUpdate
(
@RequestBody
GatewayRouteDefinition
gwdefinition
)
{
public
Mono
<
GatewayRouteDefinition
>
addOrUpdate
(
@RequestBody
GatewayRouteDefinition
gwdefinition
)
{
return
gatewayRoutesInNacosConfigService
.
addOrUpdateRouteDefinitiToNacosServer
(
gwdefinition
);
return
Mono
.
just
(
gatewayRoutesInNacosConfigService
.
addOrUpdateRouteDefinitiToNacosServer
(
gwdefinition
)
);
}
}
/**
/**
*删除路由
*删除路由
*/
*/
@DeleteMapping
(
"/routes/{id}"
)
@DeleteMapping
(
"/routes/{id}"
)
public
String
delete
(
@PathVariable
String
id
)
{
public
Mono
<
String
>
delete
(
@PathVariable
String
id
)
{
return
gatewayRoutesInNacosConfigService
.
deleteRouteDefinitiToNacosServer
(
id
);
return
Mono
.
just
(
gatewayRoutesInNacosConfigService
.
deleteRouteDefinitiToNacosServer
(
id
)
);
}
}
/**
/**
* 获取nacos里配置的动态路由配置信息
* 获取nacos里配置的动态路由配置信息
*
*
/
*/
@GetMapping
(
"/routes/all"
)
@GetMapping
(
"/routes/all"
)
public
Flux
<
ArrayList
<
GatewayRouteDefinition
>>
all
()
{
public
Flux
<
GatewayRouteDefinition
>
routes
()
{
Gson
g
=
new
Gson
();
Gson
g
=
new
Gson
();
ArrayList
<
GatewayRouteDefinition
>
list
=
new
ArrayList
<>();
ArrayList
<
GatewayRouteDefinition
>
list
=
new
ArrayList
<>();
JsonArray
gatewayDefinitionsFromNacosServer
=
gatewayRoutesInNacosConfigService
.
getGatewayDefinitionsFromNacosServer
();
JsonArray
gatewayDefinitionsFromNacosServer
=
gatewayRoutesInNacosConfigService
.
getGatewayDefinitionsFromNacosServer
();
...
@@ -63,15 +62,14 @@ public class RouteController {
...
@@ -63,15 +62,14 @@ public class RouteController {
GatewayRouteDefinition
gatewayRouteDefinition
=
g
.
fromJson
(
o
,
GatewayRouteDefinition
.
class
);
GatewayRouteDefinition
gatewayRouteDefinition
=
g
.
fromJson
(
o
,
GatewayRouteDefinition
.
class
);
list
.
add
(
gatewayRouteDefinition
);
list
.
add
(
gatewayRouteDefinition
);
});
});
return
Flux
.
just
(
list
);
return
Flux
.
fromIterable
(
list
);
}
}
/**
/**
* 获取本地所有的动态路由配置信息
* 获取本地所配置文件中所有的静态路由配置信息
* */
*/
@GetMapping
(
"/all"
)
@GetMapping
(
"staticRoutes/all"
)
public
Flux
<
RouteDefinition
>
all1
()
{
public
Flux
<
RouteDefinition
>
staticRoutes
()
{
Flux
<
RouteDefinition
>
routeDefinitions
=
PropertiesRouteDefinitionLocator
.
getRouteDefinitions
();
return
PropertiesRouteDefinitionLocator
.
getRouteDefinitions
();
routeDefinitions
.
subscribe
();
return
routeDefinitions
;
}
}
}
}
\ No newline at end of file
yst-base-gateway/src/main/java/com/elitesland/router/service/DynamicRouteService.java
View file @
0737f607
package
com
.
elitesland
.
router
.
service
;
package
com
.
elitesland
.
router
.
service
;
import
com.elitesland.router.util.GateWayNacosUtils
;
import
com.elitesland.router.util.GateWayNacosUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.gateway.config.GatewayProperties
;
import
org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator
;
import
org.springframework.cloud.gateway.event.RefreshRoutesEvent
;
import
org.springframework.cloud.gateway.event.RefreshRoutesEvent
;
import
org.springframework.cloud.gateway.route.RouteDefinition
;
import
org.springframework.cloud.gateway.route.RouteDefinition
;
import
org.springframework.cloud.gateway.route.RouteDefinitionRepository
;
import
org.springframework.cloud.gateway.route.RouteDefinitionWriter
;
import
org.springframework.cloud.gateway.route.RouteDefinitionWriter
;
import
org.springframework.cloud.gateway.support.NotFoundException
;
import
org.springframework.cloud.gateway.support.NotFoundException
;
import
org.springframework.context.ApplicationEventPublisher
;
import
org.springframework.context.ApplicationEventPublisher
;
...
@@ -14,7 +16,6 @@ import org.springframework.stereotype.Service;
...
@@ -14,7 +16,6 @@ import org.springframework.stereotype.Service;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
/*
/*
*@Author Horbin.Zhang
*@Author Horbin.Zhang
*@Date 2021-02-10 19:29
*@Date 2021-02-10 19:29
...
@@ -30,10 +31,12 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
...
@@ -30,10 +31,12 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
@Autowired
@Autowired
private
RouteDefinitionWriter
routeDefinitionWriter
;
private
RouteDefinitionWriter
routeDefinitionWriter
;
private
ApplicationEventPublisher
publisher
;
@Autowired
GatewayProperties
gatewayProperties
;
private
ApplicationEventPublisher
publisher
;
private
ArrayList
<
String
>
routeIdInMemory
=
new
ArrayList
<>();
private
ArrayList
<
String
>
routeIdInMemory
=
new
ArrayList
<>();
@Override
@Override
public
void
setApplicationEventPublisher
(
ApplicationEventPublisher
applicationEventPublisher
)
{
public
void
setApplicationEventPublisher
(
ApplicationEventPublisher
applicationEventPublisher
)
{
...
@@ -46,7 +49,7 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
...
@@ -46,7 +49,7 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
public
String
add
(
RouteDefinition
definition
)
{
public
String
add
(
RouteDefinition
definition
)
{
routeDefinitionWriter
.
save
(
Mono
.
just
(
definition
)).
subscribe
();
routeDefinitionWriter
.
save
(
Mono
.
just
(
definition
)).
subscribe
();
this
.
publisher
.
publishEvent
(
new
RefreshRoutesEvent
(
this
));
this
.
publisher
.
publishEvent
(
new
RefreshRoutesEvent
(
this
));
return
"
删除
成功!"
;
return
"
添加
成功!"
;
}
}
/**
/**
...
...
yst-base-gateway/src/main/java/com/elitesland/router/service/GatewayRoutesInNacosConfigService.java
View file @
0737f607
...
@@ -6,20 +6,10 @@ import com.elitesland.router.util.GateWayNacosUtils;
...
@@ -6,20 +6,10 @@ import com.elitesland.router.util.GateWayNacosUtils;
import
com.google.gson.Gson
;
import
com.google.gson.Gson
;
import
com.google.gson.JsonArray
;
import
com.google.gson.JsonArray
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonObject
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator
;
import
org.springframework.cloud.gateway.event.RefreshRoutesEvent
;
import
org.springframework.cloud.gateway.route.CachingRouteDefinitionLocator
;
import
org.springframework.cloud.gateway.route.InMemoryRouteDefinitionRepository
;
import
org.springframework.cloud.gateway.route.RouteDefinition
;
import
org.springframework.cloud.gateway.route.RouteDefinitionRepository
;
import
org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder
;
import
org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
reactor.core.Disposable
;
import
reactor.core.publisher.Flux
;
import
java.util.List
;
/*
/*
*@Author Horbin.Zhang
*@Author Horbin.Zhang
...
@@ -28,10 +18,13 @@ import java.util.List;
...
@@ -28,10 +18,13 @@ import java.util.List;
*/
*/
@Service
@Service
public
class
GatewayRoutesInNacosConfigService
{
public
class
GatewayRoutesInNacosConfigService
{
@Autowired
@Autowired
private
GateWayNacosUtils
gateWayNacosUtils
;
private
GateWayNacosUtils
gateWayNacosUtils
;
@Autowired
@Autowired
org
.
springframework
.
cloud
.
gateway
.
config
.
PropertiesRouteDefinitionLocator
propertiesRouteDefinitionLocator
;
org
.
springframework
.
cloud
.
gateway
.
config
.
PropertiesRouteDefinitionLocator
propertiesRouteDefinitionLocator
;
@Autowired
@Autowired
private
DynamicRouteService
dynamicRouteService
;
private
DynamicRouteService
dynamicRouteService
;
...
@@ -40,16 +33,6 @@ public class GatewayRoutesInNacosConfigService {
...
@@ -40,16 +33,6 @@ public class GatewayRoutesInNacosConfigService {
public
GatewayRouteDefinition
addOrUpdateRouteDefinitiToNacosServer
(
GatewayRouteDefinition
gwdefinition
)
{
public
GatewayRouteDefinition
addOrUpdateRouteDefinitiToNacosServer
(
GatewayRouteDefinition
gwdefinition
)
{
try
{
try
{
//清除静态配置路由信息中存在的动态路由信息
// Flux<RouteDefinition> routeDefinitions = propertiesRouteDefinitionLocator.getRouteDefinitions();
// routeDefinitions.subscribe( routeDefinition ->{
// if (routeDefinition.getId().equalsIgnoreCase(gwdefinition.getId())) {
// System.out.println(routeDefinition.getId()+"++++++++++++++++++++++++++++++++++++++++++========");
// dynamicRouteService.delete(routeDefinition.getId());
// }
// });
Gson
gson
=
new
Gson
();
Gson
gson
=
new
Gson
();
String
gateWayDefinitions
=
gateWayNacosUtils
.
getRouteDefinitionsFromNacosServer
();
String
gateWayDefinitions
=
gateWayNacosUtils
.
getRouteDefinitionsFromNacosServer
();
boolean
flag
=
false
;
boolean
flag
=
false
;
...
@@ -68,7 +51,6 @@ public class GatewayRoutesInNacosConfigService {
...
@@ -68,7 +51,6 @@ public class GatewayRoutesInNacosConfigService {
}
}
JsonElement
jsonElement
=
gson
.
toJsonTree
(
gwdefinition
,
GatewayRouteDefinition
.
class
);
JsonElement
jsonElement
=
gson
.
toJsonTree
(
gwdefinition
,
GatewayRouteDefinition
.
class
);
newConfig
.
add
(
jsonElement
);
newConfig
.
add
(
jsonElement
);
ConfigService
configService
=
gateWayNacosUtils
.
getNacosConfigService
();
ConfigService
configService
=
gateWayNacosUtils
.
getNacosConfigService
();
String
group
=
gateWayNacosUtils
.
getNacosConfigGroup
();
String
group
=
gateWayNacosUtils
.
getNacosConfigGroup
();
String
routeConfigDataId
=
gateWayNacosUtils
.
getNacosConfigDataIdForGateWayDefinition
();
String
routeConfigDataId
=
gateWayNacosUtils
.
getNacosConfigDataIdForGateWayDefinition
();
...
...
yst-base-gateway/src/main/java/com/elitesland/router/util/GateWayNacosUtils.java
View file @
0737f607
package
com
.
elitesland
.
router
.
util
;
package
com
.
elitesland
.
router
.
util
;
import
com.alibaba.nacos.api.NacosFactory
;
import
com.alibaba.nacos.api.NacosFactory
;
import
com.alibaba.nacos.api.config.ConfigService
;
import
com.alibaba.nacos.api.config.ConfigService
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
...
@@ -31,17 +30,19 @@ public class GateWayNacosUtils {
...
@@ -31,17 +30,19 @@ public class GateWayNacosUtils {
String
initConfigInfo
=
configService
.
getConfig
(
routeConfigDataId
,
group
,
5000
);
String
initConfigInfo
=
configService
.
getConfig
(
routeConfigDataId
,
group
,
5000
);
return
initConfigInfo
;
return
initConfigInfo
;
}
}
public
ConfigService
getNacosConfigService
()
throws
Exception
{
public
ConfigService
getNacosConfigService
()
throws
Exception
{
Properties
properties
=
new
Properties
();
Properties
properties
=
new
Properties
();
properties
.
put
(
"serverAddr"
,
serverAddr
);
properties
.
put
(
"serverAddr"
,
serverAddr
);
ConfigService
configService
=
NacosFactory
.
createConfigService
(
properties
);
ConfigService
configService
=
NacosFactory
.
createConfigService
(
properties
);
return
configService
;
return
configService
;
}
}
public
String
getNacosConfigGroup
(){
public
String
getNacosConfigGroup
(){
return
this
.
group
;
return
this
.
group
;
}
}
public
String
getNacosConfigDataIdForGateWayDefinition
(){
public
String
getNacosConfigDataIdForGateWayDefinition
(){
return
this
.
routeConfigDataId
;
return
this
.
routeConfigDataId
;
}
}
}
}
yst-base-gateway/src/main/resources/application-dev.yml
View file @
0737f607
...
@@ -75,24 +75,16 @@ spring:
...
@@ -75,24 +75,16 @@ spring:
# - Header=orgCode,zz00001
# - Header=orgCode,zz00001
# filters:
# filters:
# - RewritePath=/mdxt/(?<segment>.*),/$\{segment}
# - RewritePath=/mdxt/(?<segment>.*),/$\{segment}
nacos
:
nacos
:
discovery
:
discovery
:
server-addr
:
frps.elitescloud.com:25145
server-addr
:
frps.elitescloud.com:25145
# enabled: true
# enabled: true
# register-enabled: true
# register-enabled: true
gateway
:
gateway
:
nacos
:
nacos
:
routeinfo
:
routeinfo
:
dataid
:
gateway.routeinfo.json
dataid
:
gateway.routeinfo.json
group
:
DEFAULT_GROUP
group
:
DEFAULT_GROUP
# 内部端点暴露
# 内部端点暴露
management
:
management
:
endpoints
:
endpoints
:
...
@@ -102,17 +94,12 @@ management:
...
@@ -102,17 +94,12 @@ management:
endpoint
:
endpoint
:
health
:
health
:
show-details
:
always
show-details
:
always
# datasource:
# datasource:
# url: jdbc:mysql://frps.elitescloud.com:25148/jyj_fc?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
# url: jdbc:mysql://frps.elitescloud.com:25148/jyj_fc?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
# username: root
# username: root
# password: 123456
# password: 123456
# tomcat:
# tomcat:
# ignore-exception-on-pre-load:
# ignore-exception-on-pre-load:
jwt
:
jwt
:
header
:
Authorization
header
:
Authorization
# 令牌前缀
# 令牌前缀
...
...
yst-base-gateway/src/main/resources/application.yml
View file @
0737f607
...
@@ -3,9 +3,5 @@ spring:
...
@@ -3,9 +3,5 @@ spring:
#active: ${profileActive}
#active: ${profileActive}
application
:
application
:
name
:
yst-gateway
name
:
yst-gateway
server
:
server
:
port
:
9040
port
:
9040
# servlet:
\ No newline at end of file
# context-path: ""
yst-base-gateway/src/main/resources/bootstrap.yml
View file @
0737f607
spring
:
spring
:
cloud
:
cloud
:
nacos
:
nacos
:
config
:
config
:
server-addr
:
frps.elitescloud.com:25145
server-addr
:
frps.elitescloud.com:25145
\ 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