mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
nacos注册中心
This commit is contained in:
@@ -21,6 +21,13 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.gitee.sop</groupId>
|
||||
<artifactId>sop-registry-api</artifactId>
|
||||
<version>1.11.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
@@ -0,0 +1,12 @@
|
||||
package com.gitee.sop.websiteserver.config;
|
||||
|
||||
import com.gitee.sop.registryapi.config.BaseRegistryConfig;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
@Configuration
|
||||
public class RegistryConfig extends BaseRegistryConfig {
|
||||
|
||||
}
|
@@ -3,6 +3,7 @@ package com.gitee.sop.websiteserver.controller;
|
||||
import com.gitee.sop.websiteserver.sign.AlipayApiException;
|
||||
import com.gitee.sop.websiteserver.sign.AlipaySignature;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
@@ -30,6 +31,7 @@ import java.util.stream.Collectors;
|
||||
* 沙箱环境代理类
|
||||
* @author tanghc
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("sandbox")
|
||||
public class SandboxController {
|
||||
@@ -45,8 +47,8 @@ public class SandboxController {
|
||||
, @RequestParam String version
|
||||
, @RequestParam String bizContent) throws AlipayApiException {
|
||||
|
||||
Assert.isTrue(StringUtils.isNotBlank(appId), "appId不能为空");
|
||||
Assert.isTrue(StringUtils.isNotBlank(privateKey), "privateKey不能为空");
|
||||
Assert.isTrue(StringUtils.isNotBlank(appId), "AppId不能为空");
|
||||
Assert.isTrue(StringUtils.isNotBlank(privateKey), "PrivateKey不能为空");
|
||||
Assert.isTrue(StringUtils.isNotBlank(method), "method不能为空");
|
||||
|
||||
// 公共请求参数
|
||||
@@ -69,7 +71,12 @@ public class SandboxController {
|
||||
String content = AlipaySignature.getSignContent(params);
|
||||
result.beforeSign = content;
|
||||
|
||||
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
||||
String sign = null;
|
||||
try {
|
||||
sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
||||
} catch (AlipayApiException e) {
|
||||
throw new RuntimeException("构建签名失败");
|
||||
}
|
||||
result.sign = sign;
|
||||
|
||||
params.put("sign", sign);
|
||||
|
@@ -3,19 +3,13 @@ package com.gitee.sop.websiteserver.manager;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.parser.Feature;
|
||||
import com.gitee.sop.registryapi.bean.ServiceInfo;
|
||||
import com.gitee.sop.registryapi.bean.ServiceInstance;
|
||||
import com.gitee.sop.registryapi.service.RegistryService;
|
||||
import com.gitee.sop.websiteserver.bean.DocInfo;
|
||||
import com.gitee.sop.websiteserver.bean.DocItem;
|
||||
import com.gitee.sop.websiteserver.bean.EurekaApplication;
|
||||
import com.gitee.sop.websiteserver.bean.EurekaApps;
|
||||
import com.gitee.sop.websiteserver.bean.EurekaInstance;
|
||||
import com.gitee.sop.websiteserver.bean.EurekaUri;
|
||||
import com.gitee.sop.websiteserver.bean.ZookeeperContext;
|
||||
import com.gitee.sop.websiteserver.vo.ServiceInfoVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -26,8 +20,6 @@ import org.springframework.util.DigestUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -53,8 +45,6 @@ public class DocManagerImpl implements DocManager {
|
||||
Map<String, DocItem> docItemMap = new HashMap<>();
|
||||
|
||||
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
DocParser swaggerDocParser = new SwaggerDocParser();
|
||||
@@ -70,33 +60,35 @@ public class DocManagerImpl implements DocManager {
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
private String eurekaUrl;
|
||||
@Autowired
|
||||
private RegistryService registryService;
|
||||
|
||||
private volatile boolean listenInited;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
try {
|
||||
// {"STORY-SERVICE":[{"ipAddr":"10.1.30.54","name":"STORY-SERVICE","serverPort":"2222"}],"API-GATEWAY":[{"ipAddr":"10.1.30.54","name":"API-GATEWAY","serverPort":"8081"}]}
|
||||
Map<String, List<ServiceInfoVO>> listMap = this.getAllServiceList();
|
||||
log.info("服务列表:{}", JSON.toJSONString(listMap.keySet()));
|
||||
listMap.entrySet()
|
||||
List<ServiceInfo> serviceInfoList = registryService.listAllService(1, 9999);
|
||||
log.info("服务列表:{}", serviceInfoList);
|
||||
|
||||
serviceInfoList
|
||||
.stream()
|
||||
// 网关没有文档提供,需要排除
|
||||
.filter(entry -> !"API-GATEWAY".equalsIgnoreCase(entry.getKey()))
|
||||
.forEach(entry -> {
|
||||
ServiceInfoVO serviceInfoVo = entry.getValue().get(0);
|
||||
loadDocInfo(serviceInfoVo);
|
||||
});
|
||||
.filter(serviceInfo -> !"API-GATEWAY".equalsIgnoreCase(serviceInfo.getServiceId()))
|
||||
.filter(serviceInfo -> !serviceInfo.getInstances().isEmpty())
|
||||
.map(serviceInfo -> serviceInfo.getInstances().get(0))
|
||||
.collect(Collectors.toList())
|
||||
.forEach(this::loadDocInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("加载失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void loadDocInfo(ServiceInfoVO serviceInfoVo) {
|
||||
protected void loadDocInfo(ServiceInstance serviceInstance) {
|
||||
String query = this.buildQuery();
|
||||
String url = "http://" + serviceInfoVo.getIpAddr() + ":" + serviceInfoVo.getServerPort() + "/v2/api-docs" + query;
|
||||
String url = "http://" + serviceInstance.getIpPort() + "/v2/api-docs" + query;
|
||||
try {
|
||||
log.info("读取swagger文档,url:{}", url);
|
||||
ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
|
||||
if (entity.getStatusCode() != HttpStatus.OK) {
|
||||
throw new IllegalAccessException("无权访问");
|
||||
@@ -144,55 +136,8 @@ public class DocManagerImpl implements DocManager {
|
||||
return docDefinitionMap.values();
|
||||
}
|
||||
|
||||
protected Map<String, List<ServiceInfoVO>> getAllServiceList() throws IOException {
|
||||
String json = this.requestEurekaServer(EurekaUri.QUERY_APPS);
|
||||
EurekaApps eurekaApps = JSON.parseObject(json, EurekaApps.class);
|
||||
|
||||
List<ServiceInfoVO> serviceInfoVoList = new ArrayList<>();
|
||||
List<EurekaApplication> applicationList = eurekaApps.getApplications().getApplication();
|
||||
applicationList.stream()
|
||||
.forEach(eurekaApplication -> {
|
||||
List<EurekaInstance> instanceList = eurekaApplication.getInstance();
|
||||
for (EurekaInstance instance : instanceList) {
|
||||
if ("UP".equals(instance.getStatus())) {
|
||||
ServiceInfoVO vo = new ServiceInfoVO();
|
||||
vo.setName(eurekaApplication.getName());
|
||||
vo.setIpAddr(instance.getIpAddr());
|
||||
vo.setServerPort(instance.fetchPort());
|
||||
serviceInfoVoList.add(vo);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Map<String, List<ServiceInfoVO>> listMap = serviceInfoVoList.stream()
|
||||
.collect(Collectors.groupingBy(ServiceInfoVO::getName));
|
||||
|
||||
return listMap;
|
||||
}
|
||||
|
||||
protected String requestEurekaServer(EurekaUri eurekaUri, String... args) throws IOException {
|
||||
Request request = eurekaUri.getRequest(this.eurekaUrl, args);
|
||||
Response response = client.newCall(request).execute();
|
||||
if (response.isSuccessful()) {
|
||||
return response.body().string();
|
||||
} else {
|
||||
log.error("操作失败,url:{}, msg:{}, code:{}", eurekaUri.getUri(args), response.message(), response.code());
|
||||
throw new RuntimeException("操作失败");
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
protected void after() throws Exception {
|
||||
String eurekaUrls = environment.getProperty("eureka.client.serviceUrl.defaultZone");
|
||||
if (StringUtils.isBlank(eurekaUrls)) {
|
||||
throw new IllegalArgumentException("未指定eureka.client.serviceUrl.defaultZone参数");
|
||||
}
|
||||
String url = eurekaUrls.split("\\,")[0];
|
||||
if (url.endsWith("/")) {
|
||||
url = eurekaUrls.substring(0, eurekaUrls.length() - 1);
|
||||
}
|
||||
this.eurekaUrl = url;
|
||||
|
||||
this.listenServiceId();
|
||||
}
|
||||
|
||||
|
@@ -1,12 +1,12 @@
|
||||
server:
|
||||
port: 8083
|
||||
|
||||
# 注册中心
|
||||
eureka:
|
||||
serverAddr: localhost:1111
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://${eureka.serverAddr}/eureka/
|
||||
# 注册中心地址,根据实际情况改,这里只是参数,并不会去注册
|
||||
registry:
|
||||
# 使用哪个配置中心,使用eureka,填eureka;使用nacos填nacos
|
||||
name: nacos
|
||||
eureka-server-addr: http://localhost:1111/eureka/
|
||||
nacos-server-addr: 127.0.0.1:8848
|
||||
|
||||
spring:
|
||||
application:
|
||||
|
Reference in New Issue
Block a user