Skip to content

Commit

Permalink
add default server
Browse files Browse the repository at this point in the history
  • Loading branch information
YeautyYE committed Mar 30, 2019
1 parent 3b9b0e6 commit 73d1667
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.yeauty</groupId>
<artifactId>nacos-nginx-template</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>

<properties>
<java.version>1.8</java.version>
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/org/yeauty/service/impl/MonitorServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class MonitorServiceImpl implements MonitorService {

private static final Logger logger = LoggerFactory.getLogger(MonitorServiceImpl.class);

private static final String DEFAULT_SERVER = "127.0.0.1:65535";

@Override
public void updateNginxFromNacos(File configFile) throws IOException, InterruptedException, NacosException {
Properties pro = new Properties();
Expand Down Expand Up @@ -138,26 +140,29 @@ private void refreshProxyPass(List<Instance> instances, String nginxProxyPass, S
//拼接新的upstream
String newUpstream = UPSTREAM_FOMAT.replace(PLACEHOLDER, nginxProxyPass);
StringBuffer servers = new StringBuffer();
for (Instance instance : instances) {
//不健康或不可用的跳过
if (!instance.isHealthy()||!instance.isEnabled()){
continue;
if (instances.size() > 0) {
for (Instance instance : instances) {
//不健康或不可用的跳过
if (!instance.isHealthy() || !instance.isEnabled()) {
continue;
}
String ip = instance.getIp();
int port = instance.getPort();
servers.append(formatSymbol + " server " + ip + ":" + port + ";\n");
}
String ip = instance.getIp();
int port = instance.getPort();
servers.append(formatSymbol + " server " + ip + ":" + port + ";\n");
}
if (servers.length() > 0) {
servers.append(formatSymbol);
if (servers.length()==0){
//如果没有对应的服务,使用默认的服务防止nginx报错
servers.append(formatSymbol + " server "+DEFAULT_SERVER+";\n");
}
servers.append(formatSymbol);
newUpstream = newUpstream.replace(PLACEHOLDER_SERVER, servers.toString());

//替换原有的upstream
conf = matcher.replaceAll(newUpstream);
} else {
throw new IllegalArgumentException("can not found proxy_pass:" + nginxProxyPass);
}

try {
FileWriter fileWriter = new FileWriter(file, false);
fileWriter.write(conf);
Expand Down

0 comments on commit 73d1667

Please sign in to comment.