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
283a1453
Commit
283a1453
authored
Jul 11, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化Console接口
parent
61101852
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
278 additions
and
89 deletions
+278
-89
discovery-console/src/main/java/com/nepxion/discovery/console/endpoint/ConsoleEndpoint.java
+9
-89
discovery-console/src/main/java/com/nepxion/discovery/console/entity/ResultEntity.java
+65
-0
discovery-console/src/main/java/com/nepxion/discovery/console/rest/AbstractRestInvoker.java
+79
-0
discovery-console/src/main/java/com/nepxion/discovery/console/rest/ConfigUpdateRestInvoker.java
+43
-0
discovery-console/src/main/java/com/nepxion/discovery/console/rest/VersionClearRestInvoker.java
+41
-0
discovery-console/src/main/java/com/nepxion/discovery/console/rest/VersionUpdateRestInvoker.java
+41
-0
No files found.
discovery-console/src/main/java/com/nepxion/discovery/console/endpoint/ConsoleEndpoint.java
View file @
283a1453
...
@@ -18,16 +18,11 @@ import java.util.LinkedHashMap;
...
@@ -18,16 +18,11 @@ import java.util.LinkedHashMap;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.actuate.endpoint.Endpoint
;
import
org.springframework.boot.actuate.endpoint.Endpoint
;
import
org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint
;
import
org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.jmx.export.annotation.ManagedOperation
;
import
org.springframework.jmx.export.annotation.ManagedOperation
;
import
org.springframework.jmx.export.annotation.ManagedResource
;
import
org.springframework.jmx.export.annotation.ManagedResource
;
...
@@ -40,14 +35,14 @@ import org.springframework.web.bind.annotation.RestController;
...
@@ -40,14 +35,14 @@ import org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.client.RestTemplate
;
import
com.nepxion.discovery.console.entity.InstanceEntity
;
import
com.nepxion.discovery.console.entity.InstanceEntity
;
import
com.nepxion.discovery.console.handler.ConsoleErrorHandler
;
import
com.nepxion.discovery.console.rest.ConfigUpdateRestInvoker
;
import
com.nepxion.discovery.console.rest.VersionClearRestInvoker
;
import
com.nepxion.discovery.console.rest.VersionUpdateRestInvoker
;
@RestController
@RestController
@Api
(
tags
=
{
"控制台接口"
})
@Api
(
tags
=
{
"控制台接口"
})
@ManagedResource
(
description
=
"Console Endpoint"
)
@ManagedResource
(
description
=
"Console Endpoint"
)
public
class
ConsoleEndpoint
implements
MvcEndpoint
{
public
class
ConsoleEndpoint
implements
MvcEndpoint
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
ConsoleEndpoint
.
class
);
@Autowired
@Autowired
private
DiscoveryClient
discoveryClient
;
private
DiscoveryClient
discoveryClient
;
...
@@ -160,101 +155,26 @@ public class ConsoleEndpoint implements MvcEndpoint {
...
@@ -160,101 +155,26 @@ public class ConsoleEndpoint implements MvcEndpoint {
private
ResponseEntity
<?>
executeConfigUpdate
(
String
serviceId
,
String
config
,
boolean
async
)
{
private
ResponseEntity
<?>
executeConfigUpdate
(
String
serviceId
,
String
config
,
boolean
async
)
{
List
<
ServiceInstance
>
serviceInstances
=
getInstances
(
serviceId
);
List
<
ServiceInstance
>
serviceInstances
=
getInstances
(
serviceId
);
if
(
CollectionUtils
.
isEmpty
(
serviceInstances
))
{
LOG
.
warn
(
"No service instances found"
);
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
body
(
"No service instances found"
);
}
StringBuilder
stringBuilder
=
new
StringBuilder
();
ConfigUpdateRestInvoker
configUpdateRestInvoker
=
new
ConfigUpdateRestInvoker
(
serviceInstances
,
consoleRestTemplate
,
config
,
async
);
for
(
ServiceInstance
serviceInstance
:
serviceInstances
)
{
String
host
=
serviceInstance
.
getHost
();
int
port
=
serviceInstance
.
getPort
();
String
url
=
"http://"
+
host
+
":"
+
port
+
"/config/update-"
+
(
async
?
"async"
:
"sync"
);
String
result
=
consoleRestTemplate
.
postForEntity
(
url
,
config
,
String
.
class
).
getBody
();
if
(!
StringUtils
.
equals
(
result
,
"OK"
))
{
ConsoleErrorHandler
errorHandler
=
(
ConsoleErrorHandler
)
consoleRestTemplate
.
getErrorHandler
();
result
=
errorHandler
.
getCause
();
}
stringBuilder
.
append
(
"Result : serviceId="
).
append
(
serviceId
).
append
(
", url="
).
append
(
url
).
append
(
", result="
).
append
(
result
).
append
(
"\n"
);
}
String
result
=
stringBuilder
.
toString
();
return
configUpdateRestInvoker
.
invoke
();
if
(
result
.
contains
(
"\n"
))
{
result
=
result
.
substring
(
0
,
result
.
lastIndexOf
(
"\n"
));
}
LOG
.
info
(
"Config updated results :\n{}"
,
result
);
return
ResponseEntity
.
ok
().
body
(
result
);
}
}
private
ResponseEntity
<?>
executeVersionUpdate
(
String
serviceId
,
String
version
)
{
private
ResponseEntity
<?>
executeVersionUpdate
(
String
serviceId
,
String
version
)
{
List
<
ServiceInstance
>
serviceInstances
=
getInstances
(
serviceId
);
List
<
ServiceInstance
>
serviceInstances
=
getInstances
(
serviceId
);
if
(
CollectionUtils
.
isEmpty
(
serviceInstances
))
{
LOG
.
warn
(
"No service instances found"
);
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
body
(
"No service instances found"
);
}
StringBuilder
stringBuilder
=
new
StringBuilder
();
for
(
ServiceInstance
serviceInstance
:
serviceInstances
)
{
String
host
=
serviceInstance
.
getHost
();
int
port
=
serviceInstance
.
getPort
();
String
url
=
"http://"
+
host
+
":"
+
port
+
"/version/update"
;
String
result
=
consoleRestTemplate
.
postForEntity
(
url
,
version
,
String
.
class
).
getBody
();
if
(!
StringUtils
.
equals
(
result
,
"OK"
))
{
VersionUpdateRestInvoker
versionUpdateRestInvoker
=
new
VersionUpdateRestInvoker
(
serviceInstances
,
consoleRestTemplate
,
version
);
ConsoleErrorHandler
errorHandler
=
(
ConsoleErrorHandler
)
consoleRestTemplate
.
getErrorHandler
();
result
=
errorHandler
.
getCause
();
}
stringBuilder
.
append
(
"Result : serviceId="
).
append
(
serviceId
).
append
(
", url="
).
append
(
url
).
append
(
", result="
).
append
(
result
).
append
(
"\n"
);
}
String
result
=
stringBuilder
.
toString
();
if
(
result
.
contains
(
"\n"
))
{
result
=
result
.
substring
(
0
,
result
.
lastIndexOf
(
"\n"
));
}
LOG
.
info
(
"Version updated results :\n{}"
,
result
);
return
versionUpdateRestInvoker
.
invoke
();
return
ResponseEntity
.
ok
().
body
(
result
);
}
}
private
ResponseEntity
<?>
executeVersionClear
(
String
serviceId
,
String
version
)
{
private
ResponseEntity
<?>
executeVersionClear
(
String
serviceId
,
String
version
)
{
List
<
ServiceInstance
>
serviceInstances
=
getInstances
(
serviceId
);
List
<
ServiceInstance
>
serviceInstances
=
getInstances
(
serviceId
);
if
(
CollectionUtils
.
isEmpty
(
serviceInstances
))
{
LOG
.
warn
(
"No service instances found"
);
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
body
(
"No service instances found"
);
}
StringBuilder
stringBuilder
=
new
StringBuilder
();
for
(
ServiceInstance
serviceInstance
:
serviceInstances
)
{
String
host
=
serviceInstance
.
getHost
();
int
port
=
serviceInstance
.
getPort
();
String
url
=
"http://"
+
host
+
":"
+
port
+
"/version/clear"
;
String
result
=
consoleRestTemplate
.
postForEntity
(
url
,
version
,
String
.
class
).
getBody
();
if
(!
StringUtils
.
equals
(
result
,
"OK"
))
{
ConsoleErrorHandler
errorHandler
=
(
ConsoleErrorHandler
)
consoleRestTemplate
.
getErrorHandler
();
result
=
errorHandler
.
getCause
();
}
stringBuilder
.
append
(
"Result : serviceId="
).
append
(
serviceId
).
append
(
", url="
).
append
(
url
).
append
(
", result="
).
append
(
result
).
append
(
"\n"
);
}
String
result
=
stringBuilder
.
toString
();
if
(
result
.
contains
(
"\n"
))
{
result
=
result
.
substring
(
0
,
result
.
lastIndexOf
(
"\n"
));
}
LOG
.
info
(
"Version cleared results :\n{}"
,
result
);
VersionClearRestInvoker
versionClearRestInvoker
=
new
VersionClearRestInvoker
(
serviceInstances
,
consoleRestTemplate
,
version
);
return
ResponseEntity
.
ok
().
body
(
result
);
return
versionClearRestInvoker
.
invoke
(
);
}
}
@Override
@Override
...
...
discovery-console/src/main/java/com/nepxion/discovery/console/entity/ResultEntity.java
0 → 100644
View file @
283a1453
package
com
.
nepxion
.
discovery
.
console
.
entity
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
java.io.Serializable
;
import
org.apache.commons.lang3.builder.EqualsBuilder
;
import
org.apache.commons.lang3.builder.HashCodeBuilder
;
import
org.apache.commons.lang3.builder.ToStringBuilder
;
import
org.apache.commons.lang3.builder.ToStringStyle
;
public
class
ResultEntity
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
3322655604556025836L
;
private
String
serviceId
;
private
String
url
;
private
String
result
;
public
String
getServiceId
()
{
return
serviceId
;
}
public
void
setServiceId
(
String
serviceId
)
{
this
.
serviceId
=
serviceId
;
}
public
String
getUrl
()
{
return
url
;
}
public
void
setUrl
(
String
url
)
{
this
.
url
=
url
;
}
public
String
getResult
()
{
return
result
;
}
public
void
setResult
(
String
result
)
{
this
.
result
=
result
;
}
@Override
public
int
hashCode
()
{
return
HashCodeBuilder
.
reflectionHashCode
(
this
);
}
@Override
public
boolean
equals
(
Object
object
)
{
return
EqualsBuilder
.
reflectionEquals
(
this
,
object
);
}
@Override
public
String
toString
()
{
return
ToStringBuilder
.
reflectionToString
(
this
,
ToStringStyle
.
MULTI_LINE_STYLE
);
}
}
\ No newline at end of file
discovery-console/src/main/java/com/nepxion/discovery/console/rest/AbstractRestInvoker.java
0 → 100644
View file @
283a1453
package
com
.
nepxion
.
discovery
.
console
.
rest
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
java.util.ArrayList
;
import
java.util.List
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.client.RestTemplate
;
import
com.nepxion.discovery.console.entity.ResultEntity
;
import
com.nepxion.discovery.console.handler.ConsoleErrorHandler
;
public
abstract
class
AbstractRestInvoker
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
AbstractRestInvoker
.
class
);
protected
List
<
ServiceInstance
>
serviceInstances
;
protected
RestTemplate
restTemplate
;
public
AbstractRestInvoker
(
List
<
ServiceInstance
>
serviceInstances
,
RestTemplate
restTemplate
)
{
this
.
serviceInstances
=
serviceInstances
;
this
.
restTemplate
=
restTemplate
;
}
public
ResponseEntity
<?>
invoke
()
{
if
(
CollectionUtils
.
isEmpty
(
serviceInstances
))
{
LOG
.
warn
(
"No service instances found"
);
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
body
(
"No service instances found"
);
}
List
<
ResultEntity
>
resultEntityList
=
new
ArrayList
<
ResultEntity
>();
for
(
ServiceInstance
serviceInstance
:
serviceInstances
)
{
String
serviceId
=
serviceInstance
.
getServiceId
().
toLowerCase
();
String
host
=
serviceInstance
.
getHost
();
int
port
=
serviceInstance
.
getPort
();
String
url
=
getUrl
(
host
,
port
);
String
result
=
doRest
(
url
);
if
(!
StringUtils
.
equals
(
result
,
"OK"
))
{
ConsoleErrorHandler
errorHandler
=
(
ConsoleErrorHandler
)
restTemplate
.
getErrorHandler
();
result
=
errorHandler
.
getCause
();
}
ResultEntity
resultEntity
=
new
ResultEntity
();
resultEntity
.
setServiceId
(
serviceId
);
resultEntity
.
setUrl
(
url
);
resultEntity
.
setResult
(
result
);
resultEntityList
.
add
(
resultEntity
);
}
String
info
=
getInfo
();
LOG
.
info
(
info
+
" results=\n{}"
,
resultEntityList
);
return
ResponseEntity
.
ok
().
body
(
resultEntityList
);
}
protected
abstract
String
getInfo
();
protected
abstract
String
getUrl
(
String
host
,
int
port
);
protected
abstract
String
doRest
(
String
url
);
}
\ No newline at end of file
discovery-console/src/main/java/com/nepxion/discovery/console/rest/ConfigUpdateRestInvoker.java
0 → 100644
View file @
283a1453
package
com
.
nepxion
.
discovery
.
console
.
rest
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
java.util.List
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.web.client.RestTemplate
;
public
class
ConfigUpdateRestInvoker
extends
AbstractRestInvoker
{
private
String
config
;
private
boolean
async
;
public
ConfigUpdateRestInvoker
(
List
<
ServiceInstance
>
serviceInstances
,
RestTemplate
restTemplate
,
String
config
,
boolean
async
)
{
super
(
serviceInstances
,
restTemplate
);
this
.
config
=
config
;
this
.
async
=
async
;
}
@Override
protected
String
getInfo
()
{
return
"Config updated"
;
}
@Override
protected
String
getUrl
(
String
host
,
int
port
)
{
return
"http://"
+
host
+
":"
+
port
+
"/config/update-"
+
(
async
?
"async"
:
"sync"
);
}
@Override
protected
String
doRest
(
String
url
)
{
return
restTemplate
.
postForEntity
(
url
,
config
,
String
.
class
).
getBody
();
}
}
\ No newline at end of file
discovery-console/src/main/java/com/nepxion/discovery/console/rest/VersionClearRestInvoker.java
0 → 100644
View file @
283a1453
package
com
.
nepxion
.
discovery
.
console
.
rest
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
java.util.List
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.web.client.RestTemplate
;
public
class
VersionClearRestInvoker
extends
AbstractRestInvoker
{
private
String
version
;
public
VersionClearRestInvoker
(
List
<
ServiceInstance
>
serviceInstances
,
RestTemplate
restTemplate
,
String
version
)
{
super
(
serviceInstances
,
restTemplate
);
this
.
version
=
version
;
}
@Override
protected
String
getInfo
()
{
return
"Version cleared"
;
}
@Override
protected
String
getUrl
(
String
host
,
int
port
)
{
return
"http://"
+
host
+
":"
+
port
+
"/version/clear"
;
}
@Override
protected
String
doRest
(
String
url
)
{
return
restTemplate
.
postForEntity
(
url
,
version
,
String
.
class
).
getBody
();
}
}
\ No newline at end of file
discovery-console/src/main/java/com/nepxion/discovery/console/rest/VersionUpdateRestInvoker.java
0 → 100644
View file @
283a1453
package
com
.
nepxion
.
discovery
.
console
.
rest
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
java.util.List
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.web.client.RestTemplate
;
public
class
VersionUpdateRestInvoker
extends
AbstractRestInvoker
{
private
String
version
;
public
VersionUpdateRestInvoker
(
List
<
ServiceInstance
>
serviceInstances
,
RestTemplate
restTemplate
,
String
version
)
{
super
(
serviceInstances
,
restTemplate
);
this
.
version
=
version
;
}
@Override
protected
String
getInfo
()
{
return
"Version updated"
;
}
@Override
protected
String
getUrl
(
String
host
,
int
port
)
{
return
"http://"
+
host
+
":"
+
port
+
"/version/update"
;
}
@Override
protected
String
doRest
(
String
url
)
{
return
restTemplate
.
postForEntity
(
url
,
version
,
String
.
class
).
getBody
();
}
}
\ 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