Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
discovery
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
谢捷峰
discovery
Commits
0e435533
Commit
0e435533
authored
Apr 29, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加传递Header的用例
parent
938843f4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
2 deletions
+81
-2
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/rest/ARestImpl.java
+30
-1
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/rest/BRestImpl.java
+30
-1
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/rest/CRestImpl.java
+21
-0
No files found.
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/rest/ARestImpl.java
View file @
0e435533
...
@@ -9,19 +9,25 @@ package com.nepxion.discovery.plugin.example.service.rest;
...
@@ -9,19 +9,25 @@ package com.nepxion.discovery.plugin.example.service.rest;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Enumeration
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.plugin.strategy.service.context.ServiceStrategyContextHolder
;
@RestController
@RestController
@ConditionalOnProperty
(
name
=
DiscoveryConstant
.
SPRING_APPLICATION_NAME
,
havingValue
=
"discovery-springcloud-example-a"
)
@ConditionalOnProperty
(
name
=
DiscoveryConstant
.
SPRING_APPLICATION_NAME
,
havingValue
=
"discovery-springcloud-example-a"
)
...
@@ -31,11 +37,34 @@ public class ARestImpl extends AbstractRestImpl {
...
@@ -31,11 +37,34 @@ public class ARestImpl extends AbstractRestImpl {
@Autowired
@Autowired
private
RestTemplate
restTemplate
;
private
RestTemplate
restTemplate
;
@Autowired
private
ServiceStrategyContextHolder
serviceStrategyContextHolder
;
@RequestMapping
(
path
=
"/rest"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
path
=
"/rest"
,
method
=
RequestMethod
.
POST
)
@SentinelResource
(
value
=
"sentinel-resource"
,
blockHandler
=
"handleBlock"
,
fallback
=
"handleFallback"
)
@SentinelResource
(
value
=
"sentinel-resource"
,
blockHandler
=
"handleBlock"
,
fallback
=
"handleFallback"
)
public
String
rest
(
@RequestBody
String
value
)
{
public
String
rest
(
@RequestBody
String
value
)
{
value
=
doRest
(
value
);
value
=
doRest
(
value
);
value
=
restTemplate
.
postForEntity
(
"http://discovery-springcloud-example-b/rest"
,
value
,
String
.
class
).
getBody
();
// Just for testing
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
Enumeration
<
String
>
headerNames
=
attributes
.
getRequest
().
getHeaderNames
();
System
.
out
.
println
(
"Header name list:"
);
while
(
headerNames
.
hasMoreElements
())
{
String
headerName
=
headerNames
.
nextElement
();
System
.
out
.
println
(
"* "
+
headerName
);
}
String
token
=
attributes
.
getRequest
().
getHeader
(
"token"
);
System
.
out
.
println
(
"Old token="
+
token
);
System
.
out
.
println
(
"New token=Token-A"
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"token"
,
"Token-A"
);
HttpEntity
<
String
>
entity
=
new
HttpEntity
<
String
>(
value
,
headers
);
value
=
restTemplate
.
postForEntity
(
"http://discovery-springcloud-example-b/rest"
,
entity
,
String
.
class
).
getBody
();
LOG
.
info
(
"调用路径:{}"
,
value
);
LOG
.
info
(
"调用路径:{}"
,
value
);
...
...
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/rest/BRestImpl.java
View file @
0e435533
...
@@ -9,19 +9,25 @@ package com.nepxion.discovery.plugin.example.service.rest;
...
@@ -9,19 +9,25 @@ package com.nepxion.discovery.plugin.example.service.rest;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Enumeration
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.plugin.strategy.service.context.ServiceStrategyContextHolder
;
@RestController
@RestController
@ConditionalOnProperty
(
name
=
DiscoveryConstant
.
SPRING_APPLICATION_NAME
,
havingValue
=
"discovery-springcloud-example-b"
)
@ConditionalOnProperty
(
name
=
DiscoveryConstant
.
SPRING_APPLICATION_NAME
,
havingValue
=
"discovery-springcloud-example-b"
)
...
@@ -31,11 +37,34 @@ public class BRestImpl extends AbstractRestImpl {
...
@@ -31,11 +37,34 @@ public class BRestImpl extends AbstractRestImpl {
@Autowired
@Autowired
private
RestTemplate
restTemplate
;
private
RestTemplate
restTemplate
;
@Autowired
private
ServiceStrategyContextHolder
serviceStrategyContextHolder
;
@RequestMapping
(
path
=
"/rest"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
path
=
"/rest"
,
method
=
RequestMethod
.
POST
)
@SentinelResource
(
value
=
"sentinel-resource"
,
blockHandler
=
"handleBlock"
,
fallback
=
"handleFallback"
)
@SentinelResource
(
value
=
"sentinel-resource"
,
blockHandler
=
"handleBlock"
,
fallback
=
"handleFallback"
)
public
String
rest
(
@RequestBody
String
value
)
{
public
String
rest
(
@RequestBody
String
value
)
{
value
=
doRest
(
value
);
value
=
doRest
(
value
);
value
=
restTemplate
.
postForEntity
(
"http://discovery-springcloud-example-c/rest"
,
value
,
String
.
class
).
getBody
();
// Just for testing
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
Enumeration
<
String
>
headerNames
=
attributes
.
getRequest
().
getHeaderNames
();
System
.
out
.
println
(
"Header name list:"
);
while
(
headerNames
.
hasMoreElements
())
{
String
headerName
=
headerNames
.
nextElement
();
System
.
out
.
println
(
"* "
+
headerName
);
}
String
token
=
attributes
.
getRequest
().
getHeader
(
"token"
);
System
.
out
.
println
(
"Old token="
+
token
);
System
.
out
.
println
(
"New token=Token-B"
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"token"
,
"Token-B"
);
HttpEntity
<
String
>
entity
=
new
HttpEntity
<
String
>(
value
,
headers
);
value
=
restTemplate
.
postForEntity
(
"http://discovery-springcloud-example-c/rest"
,
entity
,
String
.
class
).
getBody
();
LOG
.
info
(
"调用路径:{}"
,
value
);
LOG
.
info
(
"调用路径:{}"
,
value
);
...
...
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/rest/CRestImpl.java
View file @
0e435533
...
@@ -9,28 +9,49 @@ package com.nepxion.discovery.plugin.example.service.rest;
...
@@ -9,28 +9,49 @@ package com.nepxion.discovery.plugin.example.service.rest;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Enumeration
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.plugin.strategy.service.context.ServiceStrategyContextHolder
;
@RestController
@RestController
@ConditionalOnProperty
(
name
=
DiscoveryConstant
.
SPRING_APPLICATION_NAME
,
havingValue
=
"discovery-springcloud-example-c"
)
@ConditionalOnProperty
(
name
=
DiscoveryConstant
.
SPRING_APPLICATION_NAME
,
havingValue
=
"discovery-springcloud-example-c"
)
public
class
CRestImpl
extends
AbstractRestImpl
{
public
class
CRestImpl
extends
AbstractRestImpl
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
CRestImpl
.
class
);
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
CRestImpl
.
class
);
@Autowired
private
ServiceStrategyContextHolder
serviceStrategyContextHolder
;
@RequestMapping
(
path
=
"/rest"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
path
=
"/rest"
,
method
=
RequestMethod
.
POST
)
@SentinelResource
(
value
=
"sentinel-resource"
,
blockHandler
=
"handleBlock"
,
fallback
=
"handleFallback"
)
@SentinelResource
(
value
=
"sentinel-resource"
,
blockHandler
=
"handleBlock"
,
fallback
=
"handleFallback"
)
public
String
rest
(
@RequestBody
String
value
)
{
public
String
rest
(
@RequestBody
String
value
)
{
value
=
doRest
(
value
);
value
=
doRest
(
value
);
// Just for testing
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
Enumeration
<
String
>
headerNames
=
attributes
.
getRequest
().
getHeaderNames
();
System
.
out
.
println
(
"Header name list:"
);
while
(
headerNames
.
hasMoreElements
())
{
String
headerName
=
headerNames
.
nextElement
();
System
.
out
.
println
(
"* "
+
headerName
);
}
String
token
=
attributes
.
getRequest
().
getHeader
(
"token"
);
System
.
out
.
println
(
"Token="
+
token
);
LOG
.
info
(
"调用路径:{}"
,
value
);
LOG
.
info
(
"调用路径:{}"
,
value
);
return
value
;
return
value
;
...
...
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