|
|
@ -1,17 +1,17 @@ |
|
|
|
package com.gitee.sop.registryapi.service.impl; |
|
|
|
package com.gitee.sop.registryapi.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.nacos.api.exception.NacosException; |
|
|
|
import com.alibaba.nacos.api.exception.NacosException; |
|
|
|
import com.alibaba.nacos.api.naming.NamingFactory; |
|
|
|
import com.alibaba.nacos.api.naming.NamingFactory; |
|
|
|
import com.alibaba.nacos.api.naming.NamingService; |
|
|
|
import com.alibaba.nacos.api.naming.NamingService; |
|
|
|
import com.alibaba.nacos.api.naming.pojo.Instance; |
|
|
|
import com.alibaba.nacos.api.naming.pojo.Instance; |
|
|
|
import com.alibaba.nacos.api.naming.pojo.ListView; |
|
|
|
import com.alibaba.nacos.api.naming.pojo.ListView; |
|
|
|
|
|
|
|
import com.gitee.sop.registryapi.bean.HttpTool; |
|
|
|
import com.gitee.sop.registryapi.bean.ServiceInfo; |
|
|
|
import com.gitee.sop.registryapi.bean.ServiceInfo; |
|
|
|
import com.gitee.sop.registryapi.bean.ServiceInstance; |
|
|
|
import com.gitee.sop.registryapi.bean.ServiceInstance; |
|
|
|
import com.gitee.sop.registryapi.service.RegistryService; |
|
|
|
import com.gitee.sop.registryapi.service.RegistryService; |
|
|
|
import okhttp3.FormBody; |
|
|
|
|
|
|
|
import okhttp3.OkHttpClient; |
|
|
|
import okhttp3.OkHttpClient; |
|
|
|
import okhttp3.Request; |
|
|
|
|
|
|
|
import okhttp3.Response; |
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
@ -24,13 +24,15 @@ import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* nacos接口实现 |
|
|
|
* nacos接口实现, https://nacos.io/zh-cn/docs/open-api.html
|
|
|
|
* @author tanghc |
|
|
|
* @author tanghc |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class RegistryServiceNacos implements RegistryService { |
|
|
|
public class RegistryServiceNacos implements RegistryService { |
|
|
|
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient(); |
|
|
|
OkHttpClient client = new OkHttpClient(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static HttpTool httpTool = new HttpTool(); |
|
|
|
|
|
|
|
|
|
|
|
@Value("${registry.nacos-server-addr:}") |
|
|
|
@Value("${registry.nacos-server-addr:}") |
|
|
|
private String nacosAddr; |
|
|
|
private String nacosAddr; |
|
|
|
|
|
|
|
|
|
|
@ -63,6 +65,7 @@ public class RegistryServiceNacos implements RegistryService { |
|
|
|
boolean isOnline = instance.getWeight() > 0; |
|
|
|
boolean isOnline = instance.getWeight() > 0; |
|
|
|
String status = isOnline ? "UP" : "OUT_OF_SERVICE"; |
|
|
|
String status = isOnline ? "UP" : "OUT_OF_SERVICE"; |
|
|
|
serviceInstance.setStatus(status); |
|
|
|
serviceInstance.setStatus(status); |
|
|
|
|
|
|
|
serviceInstance.setMetadata(instance.getMetadata()); |
|
|
|
serviceInfo.getInstances().add(serviceInstance); |
|
|
|
serviceInfo.getInstances().add(serviceInstance); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -73,34 +76,50 @@ public class RegistryServiceNacos implements RegistryService { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onlineInstance(ServiceInstance serviceInstance) throws Exception { |
|
|
|
public void onlineInstance(ServiceInstance serviceInstance) throws Exception { |
|
|
|
Map<String, String> params = new HashMap<>(8); |
|
|
|
// 查询实例
|
|
|
|
|
|
|
|
Instance instance = this.getInstance(serviceInstance); |
|
|
|
// 上线,把权重设置成1
|
|
|
|
// 上线,把权重设置成1
|
|
|
|
params.put("weight", "1"); |
|
|
|
instance.setWeight(1); |
|
|
|
this.updateInstance(serviceInstance, params); |
|
|
|
this.updateInstance(instance); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void offlineInstance(ServiceInstance serviceInstance) throws Exception { |
|
|
|
public void offlineInstance(ServiceInstance serviceInstance) throws Exception { |
|
|
|
Map<String, String> params = new HashMap<>(8); |
|
|
|
// 查询实例
|
|
|
|
|
|
|
|
Instance instance = this.getInstance(serviceInstance); |
|
|
|
// 下线,把权重设置成0
|
|
|
|
// 下线,把权重设置成0
|
|
|
|
params.put("weight", "0"); |
|
|
|
instance.setWeight(0); |
|
|
|
this.updateInstance(serviceInstance, params); |
|
|
|
this.updateInstance(instance); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Response updateInstance(ServiceInstance serviceInstance, Map<String, String> params) throws IOException { |
|
|
|
@Override |
|
|
|
FormBody.Builder builder = new FormBody.Builder(); |
|
|
|
public void setMetadata(ServiceInstance serviceInstance, String key, String value) throws Exception { |
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) { |
|
|
|
// 查询实例
|
|
|
|
builder.add(entry.getKey(), String.valueOf(entry.getValue())); |
|
|
|
Instance instance = this.getInstance(serviceInstance); |
|
|
|
} |
|
|
|
// 设置metadata
|
|
|
|
builder.add("serviceName", serviceInstance.getServiceId()) |
|
|
|
Map<String, String> metadata = instance.getMetadata(); |
|
|
|
.add("ip", serviceInstance.getIp()) |
|
|
|
metadata.put(key, value); |
|
|
|
.add("port", String.valueOf(serviceInstance.getPort())); |
|
|
|
this.updateInstance(instance); |
|
|
|
FormBody formBody = builder.build(); |
|
|
|
} |
|
|
|
final Request request = new Request.Builder() |
|
|
|
|
|
|
|
.url("http://" + nacosAddr + "/nacos/v1/ns/instance") |
|
|
|
protected void updateInstance(Instance instance) throws IOException { |
|
|
|
.put(formBody) |
|
|
|
String json = JSON.toJSONString(instance); |
|
|
|
.build(); |
|
|
|
JSONObject jsonObject = JSON.parseObject(json); |
|
|
|
|
|
|
|
httpTool.request("http://" + nacosAddr + "/nacos/v1/ns/instance", jsonObject, null, HttpTool.HTTPMethod.PUT); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return client.newCall(request).execute(); |
|
|
|
/** |
|
|
|
|
|
|
|
* 查询实例 |
|
|
|
|
|
|
|
* @param serviceInstance 实例信息 |
|
|
|
|
|
|
|
* @return 返回nacos实例 |
|
|
|
|
|
|
|
* @throws IOException 查询异常 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected Instance getInstance(ServiceInstance serviceInstance) throws IOException { |
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<>(8); |
|
|
|
|
|
|
|
params.put("serviceName", serviceInstance.getServiceId()); |
|
|
|
|
|
|
|
params.put("ip", serviceInstance.getIp()); |
|
|
|
|
|
|
|
params.put("port", String.valueOf(serviceInstance.getPort())); |
|
|
|
|
|
|
|
String instanceJson = httpTool.request("http://" + nacosAddr + "/nacos/v1/ns/instance", params, null, HttpTool.HTTPMethod.GET); |
|
|
|
|
|
|
|
return JSON.parseObject(instanceJson, Instance.class); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|