添加注册中心获取注册服务的restful接口
This commit is contained in:
-2
@@ -1,7 +1,5 @@
|
||||
package com.micro.fast.common;
|
||||
|
||||
import com.micro.fast.common.config.param.MybatisParamConfig;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ public class WebConfig{
|
||||
//允许cookie跨域,在cookie跨域的时候,服务端进行设置,浏览器端也要进行显式设置是否发送。
|
||||
// corsConfiguration.setAllowCredentials(true);
|
||||
//能给这个服务器发起请求的url,当这个值设置为*的时候,cookie就不能随着请求进行发送了。因为cookie必须是同源的。
|
||||
corsConfiguration.addAllowedOrigin("http://fthipw.natappfree.cc");
|
||||
corsConfiguration.addAllowedOrigin("*");
|
||||
//允许的请求头为全部
|
||||
corsConfiguration.addAllowedHeader("*");
|
||||
//允许的请求类型
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ package com.micro.fast.eureka.server;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
/**
|
||||
* 注册中心启动类
|
||||
@@ -13,6 +14,7 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableEurekaServer //开启注册中心相关服务,默认配置注册中心会注册自己
|
||||
@EnableSwagger2
|
||||
public class EurekaServerApplication {
|
||||
/**
|
||||
* 注册中心方法
|
||||
|
||||
+298
@@ -0,0 +1,298 @@
|
||||
package com.micro.fast.eureka.server.controller;
|
||||
|
||||
import com.netflix.appinfo.AmazonInfo;
|
||||
import com.netflix.appinfo.ApplicationInfoManager;
|
||||
import com.netflix.appinfo.DataCenterInfo;
|
||||
import com.netflix.appinfo.InstanceInfo;
|
||||
import com.netflix.config.ConfigurationManager;
|
||||
import com.netflix.discovery.shared.Application;
|
||||
import com.netflix.discovery.shared.Pair;
|
||||
import com.netflix.eureka.EurekaServerContext;
|
||||
import com.netflix.eureka.EurekaServerContextHolder;
|
||||
import com.netflix.eureka.cluster.PeerEurekaNode;
|
||||
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
|
||||
import com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl;
|
||||
import com.netflix.eureka.resources.StatusResource;
|
||||
import com.netflix.eureka.util.StatusInfo;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URI;
|
||||
import java.util.*;
|
||||
/**
|
||||
* restful操作eureka的接口
|
||||
* @author lsy
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping({"${eureka.restful.path:/restful/eureka}"})
|
||||
public class RestfulEurekaController {
|
||||
|
||||
|
||||
@Value("${eureka.restful.path:/restful/eureka}")
|
||||
private String restfulPath = "";
|
||||
/**
|
||||
* 实例管理对象
|
||||
*/
|
||||
private ApplicationInfoManager applicationInfoManager;
|
||||
|
||||
public RestfulEurekaController(ApplicationInfoManager applicationInfoManager) {
|
||||
this.applicationInfoManager = applicationInfoManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详细信息
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping
|
||||
@ResponseBody
|
||||
public Map<String, Object> status(HttpServletRequest request, HttpServletResponse response) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
populateBase(request, map);
|
||||
populateApps(map);
|
||||
StatusInfo statusInfo;
|
||||
try {
|
||||
statusInfo = new StatusResource().getStatusInfo();
|
||||
}
|
||||
catch (Exception e) {
|
||||
statusInfo = StatusInfo.Builder.newBuilder().isHealthy(false).build();
|
||||
}
|
||||
map.put("statusInfo", statusInfo);
|
||||
populateInstanceInfo(map, statusInfo);
|
||||
filterReplicas(map, statusInfo);
|
||||
response.setStatus(HttpStatus.SC_OK);
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后注册的实例
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/lastn")
|
||||
public Map<String, Object> lastn(HttpServletRequest request) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
populateBase(request, map);
|
||||
PeerAwareInstanceRegistryImpl registry = (PeerAwareInstanceRegistryImpl) getRegistry();
|
||||
ArrayList<Map<String, Object>> lastNCanceled = new ArrayList<>();
|
||||
List<Pair<Long, String>> list = registry.getLastNCanceledInstances();
|
||||
for (Pair<Long, String> entry : list) {
|
||||
lastNCanceled.add(registeredInstance(entry.second(), entry.first()));
|
||||
}
|
||||
map.put("lastNCanceled", lastNCanceled);
|
||||
list = registry.getLastNRegisteredInstances();
|
||||
ArrayList<Map<String, Object>> lastNRegistered = new ArrayList<>();
|
||||
for (Pair<Long, String> entry : list) {
|
||||
lastNRegistered.add(registeredInstance(entry.second(), entry.first()));
|
||||
}
|
||||
map.put("lastNRegistered", lastNRegistered);
|
||||
return map;
|
||||
}
|
||||
|
||||
private Map<String, Object> registeredInstance(String id, long date) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
map.put("date", new Date(date));
|
||||
return map;
|
||||
}
|
||||
|
||||
protected void populateBase(HttpServletRequest request, Map<String, Object> model) {
|
||||
model.put("time", new Date());
|
||||
model.put("basePath", "/");
|
||||
model.put("restfulPath", this.restfulPath.equals("/") ? ""
|
||||
: this.restfulPath);
|
||||
populateHeader(model);
|
||||
populateNavbar(request, model);
|
||||
}
|
||||
|
||||
private void populateHeader(Map<String, Object> model) {
|
||||
model.put("currentTime", StatusResource.getCurrentTimeAsString());
|
||||
model.put("upTime", StatusInfo.getUpTime());
|
||||
model.put("environment", ConfigurationManager.getDeploymentContext()
|
||||
.getDeploymentEnvironment());
|
||||
model.put("datacenter", ConfigurationManager.getDeploymentContext()
|
||||
.getDeploymentDatacenter());
|
||||
PeerAwareInstanceRegistry registry = getRegistry();
|
||||
model.put("registry", registry);
|
||||
model.put("isBelowRenewThresold", registry.isBelowRenewThresold() == 1);
|
||||
DataCenterInfo info = applicationInfoManager.getInfo().getDataCenterInfo();
|
||||
if (info.getName() == DataCenterInfo.Name.Amazon) {
|
||||
AmazonInfo amazonInfo = (AmazonInfo) info;
|
||||
model.put("amazonInfo", amazonInfo);
|
||||
model.put("amiId", amazonInfo.get(AmazonInfo.MetaDataKey.amiId));
|
||||
model.put("availabilityZone",
|
||||
amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone));
|
||||
model.put("instanceId", amazonInfo.get(AmazonInfo.MetaDataKey.instanceId));
|
||||
}
|
||||
}
|
||||
|
||||
private PeerAwareInstanceRegistry getRegistry() {
|
||||
return getServerContext().getRegistry();
|
||||
}
|
||||
|
||||
private EurekaServerContext getServerContext() {
|
||||
return EurekaServerContextHolder.getInstance().getServerContext();
|
||||
}
|
||||
|
||||
private void populateNavbar(HttpServletRequest request, Map<String, Object> model) {
|
||||
Map<String, String> replicas = new LinkedHashMap<>();
|
||||
List<PeerEurekaNode> list = getServerContext().getPeerEurekaNodes().getPeerNodesView();
|
||||
for (PeerEurekaNode node : list) {
|
||||
try {
|
||||
URI uri = new URI(node.getServiceUrl());
|
||||
String href = scrubBasicAuth(node.getServiceUrl());
|
||||
replicas.put(uri.getHost(), href);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// ignore?
|
||||
}
|
||||
}
|
||||
model.put("replicas", replicas.entrySet());
|
||||
}
|
||||
|
||||
private void populateApps(Map<String, Object> model) {
|
||||
List<Application> sortedApplications = getRegistry().getSortedApplications();
|
||||
ArrayList<Map<String, Object>> apps = new ArrayList<>();
|
||||
for (Application app : sortedApplications) {
|
||||
LinkedHashMap<String, Object> appData = new LinkedHashMap<>();
|
||||
apps.add(appData);
|
||||
appData.put("name", app.getName());
|
||||
Map<String, Integer> amiCounts = new HashMap<>();
|
||||
Map<InstanceInfo.InstanceStatus, List<Pair<String, String>>> instancesByStatus = new HashMap<>();
|
||||
Map<String, Integer> zoneCounts = new HashMap<>();
|
||||
for (InstanceInfo info : app.getInstances()) {
|
||||
String id = info.getId();
|
||||
String url = info.getStatusPageUrl();
|
||||
InstanceInfo.InstanceStatus status = info.getStatus();
|
||||
String ami = "n/a";
|
||||
String zone = "";
|
||||
if (info.getDataCenterInfo().getName() == DataCenterInfo.Name.Amazon) {
|
||||
AmazonInfo dcInfo = (AmazonInfo) info.getDataCenterInfo();
|
||||
ami = dcInfo.get(AmazonInfo.MetaDataKey.amiId);
|
||||
zone = dcInfo.get(AmazonInfo.MetaDataKey.availabilityZone);
|
||||
}
|
||||
Integer count = amiCounts.get(ami);
|
||||
if (count != null) {
|
||||
amiCounts.put(ami, count + 1);
|
||||
}
|
||||
else {
|
||||
amiCounts.put(ami, 1);
|
||||
}
|
||||
count = zoneCounts.get(zone);
|
||||
if (count != null) {
|
||||
zoneCounts.put(zone, count + 1);
|
||||
}
|
||||
else {
|
||||
zoneCounts.put(zone, 1);
|
||||
}
|
||||
List<Pair<String, String>> list = instancesByStatus.get(status);
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
instancesByStatus.put(status, list);
|
||||
}
|
||||
list.add(new Pair<>(id, url));
|
||||
}
|
||||
appData.put("amiCounts", amiCounts.entrySet());
|
||||
appData.put("zoneCounts", zoneCounts.entrySet());
|
||||
ArrayList<Map<String, Object>> instanceInfos = new ArrayList<>();
|
||||
appData.put("instanceInfos", instanceInfos);
|
||||
for (Iterator<Map.Entry<InstanceInfo.InstanceStatus, List<Pair<String, String>>>> iter = instancesByStatus
|
||||
.entrySet().iterator(); iter.hasNext();) {
|
||||
Map.Entry<InstanceInfo.InstanceStatus, List<Pair<String, String>>> entry = iter
|
||||
.next();
|
||||
List<Pair<String, String>> value = entry.getValue();
|
||||
InstanceInfo.InstanceStatus status = entry.getKey();
|
||||
LinkedHashMap<String, Object> instanceData = new LinkedHashMap<>();
|
||||
instanceInfos.add(instanceData);
|
||||
instanceData.put("status", entry.getKey());
|
||||
ArrayList<Map<String, Object>> instances = new ArrayList<>();
|
||||
instanceData.put("instances", instances);
|
||||
instanceData.put("isNotUp", status != InstanceInfo.InstanceStatus.UP);
|
||||
|
||||
// TODO
|
||||
|
||||
/*
|
||||
* if(status != InstanceInfo.InstanceStatus.UP){
|
||||
* buf.append("<font color=red size=+1><b>"); }
|
||||
* buf.append("<b>").append(status
|
||||
* .name()).append("</b> (").append(value.size()).append(") - ");
|
||||
* if(status != InstanceInfo.InstanceStatus.UP){
|
||||
* buf.append("</font></b>"); }
|
||||
*/
|
||||
|
||||
for (Pair<String, String> p : value) {
|
||||
LinkedHashMap<String, Object> instance = new LinkedHashMap<>();
|
||||
instances.add(instance);
|
||||
instance.put("id", p.first());
|
||||
String url = p.second();
|
||||
instance.put("url", url);
|
||||
boolean isHref = url != null && url.startsWith("http");
|
||||
instance.put("isHref", isHref);
|
||||
/*
|
||||
* String id = p.first(); String url = p.second(); if(url != null &&
|
||||
* url.startsWith("http")){
|
||||
* buf.append("<a href=\"").append(url).append("\">"); }else { url =
|
||||
* null; } buf.append(id); if(url != null){ buf.append("</a>"); }
|
||||
* buf.append(", ");
|
||||
*/
|
||||
}
|
||||
}
|
||||
// out.println("<td>" + buf.toString() + "</td></tr>");
|
||||
}
|
||||
model.put("apps", apps);
|
||||
}
|
||||
|
||||
private void populateInstanceInfo(Map<String, Object> model, StatusInfo statusInfo) {
|
||||
InstanceInfo instanceInfo = statusInfo.getInstanceInfo();
|
||||
Map<String, String> instanceMap = new HashMap<>();
|
||||
instanceMap.put("ipAddr", instanceInfo.getIPAddr());
|
||||
instanceMap.put("status", instanceInfo.getStatus().toString());
|
||||
if (instanceInfo.getDataCenterInfo().getName() == DataCenterInfo.Name.Amazon) {
|
||||
AmazonInfo info = (AmazonInfo) instanceInfo.getDataCenterInfo();
|
||||
instanceMap.put("availability-zone",
|
||||
info.get(AmazonInfo.MetaDataKey.availabilityZone));
|
||||
instanceMap.put("public-ipv4", info.get(AmazonInfo.MetaDataKey.publicIpv4));
|
||||
instanceMap.put("instance-id", info.get(AmazonInfo.MetaDataKey.instanceId));
|
||||
instanceMap.put("public-hostname",
|
||||
info.get(AmazonInfo.MetaDataKey.publicHostname));
|
||||
instanceMap.put("ami-id", info.get(AmazonInfo.MetaDataKey.amiId));
|
||||
instanceMap.put("instance-type",
|
||||
info.get(AmazonInfo.MetaDataKey.instanceType));
|
||||
}
|
||||
model.put("instanceInfo", instanceMap);
|
||||
}
|
||||
|
||||
protected void filterReplicas(Map<String, Object> model, StatusInfo statusInfo) {
|
||||
Map<String, String> applicationStats = statusInfo.getApplicationStats();
|
||||
if(applicationStats.get("registered-replicas").contains("@")){
|
||||
applicationStats.put("registered-replicas", scrubBasicAuth(applicationStats.get("registered-replicas")));
|
||||
}
|
||||
if(applicationStats.get("unavailable-replicas").contains("@")){
|
||||
applicationStats.put("unavailable-replicas",scrubBasicAuth(applicationStats.get("unavailable-replicas")));
|
||||
}
|
||||
if(applicationStats.get("available-replicas").contains("@")){
|
||||
applicationStats.put("available-replicas",scrubBasicAuth(applicationStats.get("available-replicas")));
|
||||
}
|
||||
model.put("applicationStats", applicationStats);
|
||||
}
|
||||
|
||||
private String scrubBasicAuth(String urlList){
|
||||
String[] urls=urlList.split(",");
|
||||
String filteredUrls="";
|
||||
for(String u : urls){
|
||||
if(u.contains("@")){
|
||||
filteredUrls+=u.substring(0,u.indexOf("//")+2)+u.substring(u.indexOf("@")+1,u.length())+",";
|
||||
}else{
|
||||
filteredUrls+=u+",";
|
||||
}
|
||||
}
|
||||
return filteredUrls.substring(0,filteredUrls.length()-1);
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -3,6 +3,7 @@ package com.micro.fast.eureka.server2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
/**
|
||||
* 注册中心启动类
|
||||
@@ -12,6 +13,7 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableEurekaServer //开启注册中心的相关服务
|
||||
@EnableSwagger2
|
||||
public class EurekaServer2Application {
|
||||
/**
|
||||
* 注册中心启动方法
|
||||
|
||||
+4
-1
@@ -8,4 +8,7 @@
|
||||
## 服务消费,服务熔断,服务降级,线程隔离
|
||||
## 服务熔断,降级情况的统计
|
||||
监控界面访问地址`/hystrix`,监控数据的获取地址`hystrix.stream`
|
||||
## 服务调用的监控数据生成
|
||||
## 服务调用的监控数据生成
|
||||
|
||||
`@PostConstruct`注解修饰的方法会在tomcat加载servlet之前运行
|
||||
`@PreConstruct`注解修饰的方法会在tomcat卸载servlet的时候运行
|
||||
@@ -63,7 +63,6 @@ public class UpmsUserController {
|
||||
if (upmsUser.getLocked() == 1){
|
||||
return ServerResponse.errorMsg("账户被锁定");
|
||||
}
|
||||
upmsUser.setPassword(null);
|
||||
return ServerResponse.successData(upmsUser);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ eureka:
|
||||
# 配置熔断监控的集群名称
|
||||
metadata-map:
|
||||
cluster: ms-upms
|
||||
instance-id: www.upms.com
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user