Fully compatible with the old format for health check

This commit is contained in:
KomachiSion 2020-05-25 10:39:51 +08:00
parent f2ef335244
commit db6fc0c026
8 changed files with 21 additions and 55 deletions

View File

@ -19,6 +19,7 @@ import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker.None;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Tcp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
@ -35,16 +36,17 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
})
public abstract class AbstractHealthChecker implements Cloneable {
protected String type = "unknown";
@JsonIgnore
protected final String type;
protected AbstractHealthChecker(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* Clone all fields of this instance to another one.
*
@ -62,7 +64,7 @@ public abstract class AbstractHealthChecker implements Cloneable {
public static final String TYPE = "NONE";
public None() {
this.setType(TYPE);
super(TYPE);
}
@Override

View File

@ -41,7 +41,7 @@ public class Http extends AbstractHealthChecker {
private int expectedResponseCode = 200;
public Http() {
this.type = TYPE;
super(TYPE);
}
public int getExpectedResponseCode() {
@ -113,12 +113,9 @@ public class Http extends AbstractHealthChecker {
@Override
public Http clone() throws CloneNotSupportedException {
Http config = new Http();
config.setPath(this.getPath());
config.setHeaders(this.getHeaders());
config.setType(this.getType());
config.setExpectedResponseCode(this.getExpectedResponseCode());
return config;
}
}

View File

@ -36,7 +36,7 @@ public class Mysql extends AbstractHealthChecker {
private String cmd;
public Mysql() {
this.type = TYPE;
super(TYPE);
}
public String getCmd() {
@ -70,11 +70,11 @@ public class Mysql extends AbstractHealthChecker {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql)) {
if (!(obj instanceof Mysql)) {
return false;
}
com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql other = (com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql) obj;
Mysql other = (Mysql) obj;
if (!StringUtils.equals(user, other.getUser())) {
return false;
@ -88,13 +88,11 @@ public class Mysql extends AbstractHealthChecker {
}
@Override
public com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql clone() throws CloneNotSupportedException {
com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql config = new com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql();
public Mysql clone() throws CloneNotSupportedException {
Mysql config = new Mysql();
config.setUser(this.getUser());
config.setPwd(this.getPwd());
config.setCmd(this.getCmd());
config.setType(this.getType());
return config;
}
}

View File

@ -25,10 +25,11 @@ import com.google.common.base.Objects;
* @author yangyi
*/
public class Tcp extends AbstractHealthChecker {
public static final String TYPE = "TCP";
public Tcp() {
this.type = TYPE;
super(TYPE);
}
@Override
@ -43,8 +44,6 @@ public class Tcp extends AbstractHealthChecker {
@Override
public Tcp clone() throws CloneNotSupportedException {
Tcp config = new Tcp();
config.setType(this.type);
return config;
return new Tcp();
}
}

View File

@ -48,15 +48,7 @@ public class AbstractHealthCheckerTest {
}
@Test
public void testDeserializeWithFullInfo() throws IOException {
String testChecker = "{\"type\":\"TEST\",\"type\":\"TEST\",\"testValue\":\"\"}";
TestChecker actual = objectMapper.readValue(testChecker, TestChecker.class);
assertEquals("", actual.getTestValue());
assertEquals(TestChecker.TYPE, actual.getType());
}
@Test
public void testDeserializeWithoutFullInfo() throws IOException {
public void testDeserialize() throws IOException {
String testChecker = "{\"type\":\"TEST\",\"testValue\":\"\"}";
TestChecker actual = objectMapper.readValue(testChecker, TestChecker.class);
assertEquals("", actual.getTestValue());

View File

@ -35,7 +35,7 @@ public class TestChecker extends AbstractHealthChecker {
}
public TestChecker() {
setType(TYPE);
super(TYPE);
}
@Override

View File

@ -68,20 +68,9 @@ public class HttpTest {
}
@Test
public void testDeserializeWithFullInfo() throws IOException {
String testChecker = "{\"type\":\"HTTP\",\"type\":\"HTTP\",\"path\":\"/x\",\"headers\":\"x:a|y:\",\"expectedResponseCode\":200}";
Http actual = objectMapper.readValue(testChecker, Http.class);
assertHttp(actual);
}
@Test
public void testDeserializeWithoutFullInfo() throws IOException {
public void testDeserialize() throws IOException {
String testChecker = "{\"type\":\"HTTP\",\"path\":\"/x\",\"headers\":\"x:a|y:\",\"expectedResponseCode\":200}";
Http actual = objectMapper.readValue(testChecker, Http.class);
assertHttp(actual);
}
private void assertHttp(Http actual) {
assertEquals("x:a|y:", actual.getHeaders());
assertEquals("/x", actual.getPath());
assertEquals(200, actual.getExpectedResponseCode());

View File

@ -51,20 +51,9 @@ public class MysqlTest {
}
@Test
public void testDeserializeWithFullInfo() throws IOException {
String testChecker = "{\"type\":\"MYSQL\",\"type\":\"MYSQL\",\"user\":\"user\",\"pwd\":\"pwd\",\"cmd\":\"cmd\"}";
Mysql actual = objectMapper.readValue(testChecker, Mysql.class);
assertMysql(actual);
}
@Test
public void testDeserializeWithoutFullInfo() throws IOException {
public void testDeserialize() throws IOException {
String testChecker = "{\"type\":\"MYSQL\",\"user\":\"user\",\"pwd\":\"pwd\",\"cmd\":\"cmd\"}";
Mysql actual = objectMapper.readValue(testChecker, Mysql.class);
assertMysql(actual);
}
private void assertMysql(Mysql actual) {
assertEquals("cmd", actual.getCmd());
assertEquals("pwd", actual.getPwd());
assertEquals("user", actual.getUser());