Merge pull request #61 from SkyeBeFreeman/main

Fix url 404 of README.md.
pull/68/head
Haotian Zhang 2 years ago committed by GitHub
commit 6197a7fc75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -57,13 +57,15 @@ Example 列表:
- [PolarisMesh](https://github.com/polarismesh)接入相关的样例: - [PolarisMesh](https://github.com/polarismesh)接入相关的样例:
- [服务发现](spring-cloud-tencent-examples/polaris-discovery-example/README-zh.md) - [服务发现](spring-cloud-tencent-examples/polaris-discovery-example/README-zh.md)
- [故障熔断](spring-cloud-tencent-examples/polaris-circuitbreaker-example/README-zh.md) - [故障熔断](spring-cloud-tencent-examples/polaris-circuitbreaker-example/README-zh.md)
- [限流](spring-cloud-tencent-examples/polaris-ratelimit-example/README-zh.md) - [限流](spring-cloud-tencent-examples/polaris-ratelimit-example/README-zh.md)
- [网关](spring-cloud-tencent-examples/polaris-gateway-example/README-zh.md) - [网关](spring-cloud-tencent-examples/polaris-gateway-example/README-zh.md)
更多详细功能,请参考[polaris-java](https://github.com/polarismesh/polaris-java/blob/main/README-zh.md)。
## 版本号规范 ## 版本号规范
采取与Spring Cloud大版本号相关的版本策略。 采取与Spring Cloud大版本号相关的版本策略。

@ -72,7 +72,7 @@ Example List:
- [Polaris Gateway Example](spring-cloud-tencent-examples/polaris-gateway-example/README.md) - [Polaris Gateway Example](spring-cloud-tencent-examples/polaris-gateway-example/README.md)
For more features, please refer to [使用polaris-java](https://github.com/polarismesh/website/blob/main/docs/zh/doc/%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/%E4%BD%BF%E7%94%A8polaris-java.md). For more features, please refer to [polaris-java](https://github.com/polarismesh/polaris-java).
### Version Standard ### Version Standard

@ -49,7 +49,8 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
*/ */
public class PolarisServiceRegistry implements ServiceRegistry<Registration> { public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
private static final Logger log = LoggerFactory.getLogger(PolarisServiceRegistry.class); private static final Logger log = LoggerFactory
.getLogger(PolarisServiceRegistry.class);
private static final int ttl = 5; private static final int ttl = 5;
@ -61,14 +62,15 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
private final ScheduledExecutorService heartbeatExecutor; private final ScheduledExecutorService heartbeatExecutor;
public PolarisServiceRegistry(PolarisProperties polarisProperties, PolarisDiscoveryHandler polarisDiscoveryHandler, public PolarisServiceRegistry(PolarisProperties polarisProperties,
PolarisDiscoveryHandler polarisDiscoveryHandler,
MetadataLocalProperties metadataLocalProperties) { MetadataLocalProperties metadataLocalProperties) {
this.polarisProperties = polarisProperties; this.polarisProperties = polarisProperties;
this.polarisDiscoveryHandler = polarisDiscoveryHandler; this.polarisDiscoveryHandler = polarisDiscoveryHandler;
this.metadataLocalProperties = metadataLocalProperties; this.metadataLocalProperties = metadataLocalProperties;
if (polarisProperties.isHeartbeatEnabled()) { if (polarisProperties.isHeartbeatEnabled()) {
ScheduledThreadPoolExecutor heartbeatExecutor = new ScheduledThreadPoolExecutor(0, ScheduledThreadPoolExecutor heartbeatExecutor = new ScheduledThreadPoolExecutor(
new NamedThreadFactory("spring-cloud-heartbeat")); 0, new NamedThreadFactory("spring-cloud-heartbeat"));
heartbeatExecutor.setMaximumPoolSize(1); heartbeatExecutor.setMaximumPoolSize(1);
this.heartbeatExecutor = heartbeatExecutor; this.heartbeatExecutor = heartbeatExecutor;
} }
@ -101,19 +103,20 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI(); ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
providerClient.register(instanceRegisterRequest); providerClient.register(instanceRegisterRequest);
log.info("polaris registry, {} {} {}:{} {} register finished", log.info("polaris registry, {} {} {}:{} {} register finished",
polarisProperties.getNamespace(), polarisProperties.getNamespace(), registration.getServiceId(),
registration.getServiceId(), registration.getHost(), registration.getHost(), registration.getPort(),
registration.getPort(), metadataLocalProperties.getContent()); metadataLocalProperties.getContent());
if (null != heartbeatExecutor) { if (null != heartbeatExecutor) {
InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest(); InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
BeanUtils.copyProperties(instanceRegisterRequest, heartbeatRequest); BeanUtils.copyProperties(instanceRegisterRequest, heartbeatRequest);
//注册成功后开始启动心跳线程 // 注册成功后开始启动心跳线程
heartbeat(heartbeatRequest); heartbeat(heartbeatRequest);
} }
} }
catch (Exception e) { catch (Exception e) {
log.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e); log.error("polaris registry, {} register failed...{},",
registration.getServiceId(), registration, e);
rethrowRuntimeException(e); rethrowRuntimeException(e);
} }
} }
@ -140,7 +143,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
providerClient.deRegister(deRegisterRequest); providerClient.deRegister(deRegisterRequest);
} }
catch (Exception e) { catch (Exception e) {
log.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", registration, e); log.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", registration,
e);
} }
finally { finally {
if (null != heartbeatExecutor) { if (null != heartbeatExecutor) {
@ -163,7 +167,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
@Override @Override
public Object getStatus(Registration registration) { public Object getStatus(Registration registration) {
String serviceName = registration.getServiceId(); String serviceName = registration.getServiceId();
InstancesResponse instancesResponse = polarisDiscoveryHandler.getInstances(serviceName); InstancesResponse instancesResponse = polarisDiscoveryHandler
.getInstances(serviceName);
Instance[] instances = instancesResponse.getInstances(); Instance[] instances = instancesResponse.getInstances();
if (null == instances || instances.length == 0) { if (null == instances || instances.length == 0) {
return null; return null;
@ -179,23 +184,26 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
/** /**
* Start the heartbeat thread. * Start the heartbeat thread.
*
* @param heartbeatRequest heartbeat request * @param heartbeatRequest heartbeat request
*/ */
public void heartbeat(InstanceHeartbeatRequest heartbeatRequest) { public void heartbeat(InstanceHeartbeatRequest heartbeatRequest) {
heartbeatExecutor.scheduleWithFixedDelay(() -> { heartbeatExecutor.scheduleWithFixedDelay(() -> {
try { try {
String healthCheckEndpoint = polarisProperties.getHealthCheckUrl(); String healthCheckEndpoint = polarisProperties.getHealthCheckUrl();
//先判断是否配置了health-check-url如果配置了需要先进行服务实例健康检查如果健康检查通过则进行心跳上报如果不通过则不上报心跳 // 先判断是否配置了health-check-url如果配置了需要先进行服务实例健康检查如果健康检查通过则进行心跳上报如果不通过则不上报心跳
if (Strings.isNotEmpty(healthCheckEndpoint)) { if (Strings.isNotEmpty(healthCheckEndpoint)) {
if (!healthCheckEndpoint.startsWith("/")) { if (!healthCheckEndpoint.startsWith("/")) {
healthCheckEndpoint = "/" + healthCheckEndpoint; healthCheckEndpoint = "/" + healthCheckEndpoint;
} }
String healthCheckUrl = String.format("http://%s:%s%s", heartbeatRequest.getHost(), heartbeatRequest.getPort(), healthCheckEndpoint); String healthCheckUrl = String.format("http://%s:%s%s",
heartbeatRequest.getHost(), heartbeatRequest.getPort(),
healthCheckEndpoint);
if (!OkHttpUtil.get(healthCheckUrl, null)) { if (!OkHttpUtil.get(healthCheckUrl, null)) {
log.error("backend service health check failed. health check endpoint = {}", healthCheckEndpoint); log.error(
"backend service health check failed. health check endpoint = {}",
healthCheckEndpoint);
return; return;
} }
} }

@ -68,14 +68,22 @@ public class Metadata2HeaderFeignInterceptorTest {
public void test1() throws JsonProcessingException { public void test1() throws JsonProcessingException {
String metadata = testFeign.test(); String metadata = testFeign.test();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
Assertions.assertThat(mapper.readTree(metadata)) Assertions.assertThat(mapper.readTree(metadata)).isEqualTo(mapper.readTree(
.isEqualTo(mapper.readTree("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{\"LOCAL_SERVICE\":\"test" "{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{\"LOCAL_SERVICE\":\"test"
+ "\",\"LOCAL_PATH\":\"/test\",\"LOCAL_NAMESPACE\":\"default\"}")); + "\",\"LOCAL_PATH\":\"/test\",\"LOCAL_NAMESPACE\":\"default\"}"));
Assertions.assertThat(metadataLocalProperties.getContent().get("a")).isEqualTo("1"); Assertions.assertThat(metadataLocalProperties.getContent().get("a"))
Assertions.assertThat(metadataLocalProperties.getContent().get("b")).isEqualTo("2"); .isEqualTo("1");
Assertions.assertThat(MetadataContextHolder.get().getTransitiveCustomMetadata("a")).isEqualTo("11"); Assertions.assertThat(metadataLocalProperties.getContent().get("b"))
Assertions.assertThat(MetadataContextHolder.get().getTransitiveCustomMetadata("b")).isEqualTo("22"); .isEqualTo("2");
Assertions.assertThat(MetadataContextHolder.get().getTransitiveCustomMetadata("c")).isEqualTo("33"); Assertions
.assertThat(MetadataContextHolder.get().getTransitiveCustomMetadata("a"))
.isEqualTo("11");
Assertions
.assertThat(MetadataContextHolder.get().getTransitiveCustomMetadata("b"))
.isEqualTo("22");
Assertions
.assertThat(MetadataContextHolder.get().getTransitiveCustomMetadata("c"))
.isEqualTo("33");
} }
@SpringBootApplication @SpringBootApplication
@ -84,18 +92,22 @@ public class Metadata2HeaderFeignInterceptorTest {
protected static class TestApplication { protected static class TestApplication {
@RequestMapping("/test") @RequestMapping("/test")
public String test(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr) public String test(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
String systemMetadataStr = JacksonUtils.serialize2Json(MetadataContextHolder.get().getAllSystemMetadata()); String systemMetadataStr = JacksonUtils
.serialize2Json(MetadataContextHolder.get().getAllSystemMetadata());
return URLDecoder.decode(customMetadataStr, "UTF-8") + systemMetadataStr; return URLDecoder.decode(customMetadataStr, "UTF-8") + systemMetadataStr;
} }
@FeignClient(name = "test-feign", url = "http://localhost:8081") @FeignClient(name = "test-feign", url = "http://localhost:8081")
public interface TestFeign { public interface TestFeign {
@RequestMapping(value = "/test", headers = {MetadataConstant.HeaderName.CUSTOM_METADATA + "={\"a\":\"11" @RequestMapping(value = "/test",
+ "\",\"b\":\"22\",\"c\":\"33\"}"}) headers = { MetadataConstant.HeaderName.CUSTOM_METADATA
+ "={\"a\":\"11" + "\",\"b\":\"22\",\"c\":\"33\"}" })
String test(); String test();
} }
@Configuration @Configuration
@ -106,7 +118,9 @@ public class Metadata2HeaderFeignInterceptorTest {
template.header(MetadataConstant.HeaderName.CUSTOM_METADATA, template.header(MetadataConstant.HeaderName.CUSTOM_METADATA,
"{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}"); "{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}");
} }
} }
} }
} }

Loading…
Cancel
Save