From b0b95c9368a425add87c3464d897135345ef3710 Mon Sep 17 00:00:00 2001 From: likeyoukang <3148526936@qq.com> Date: Wed, 29 Jun 2022 11:25:16 +0800 Subject: [PATCH 01/11] Fix wrong word in code log. eg. loadbalacer -> loadbalancer Excute -> Execute recommoned -> recommend --- .../com/alibaba/cloud/examples/ConsumerSCLBApplication.java | 2 +- .../com/alibaba/cloud/examples/tx/TransactionListenerImpl.java | 2 +- .../META-INF/additional-spring-configuration-metadata.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/ConsumerSCLBApplication.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/ConsumerSCLBApplication.java index 9cb57f9b5..f157897bf 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/ConsumerSCLBApplication.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/ConsumerSCLBApplication.java @@ -105,7 +105,7 @@ public class ConsumerSCLBApplication { @Override public Mono> choose(Request request) { - log.info("random spring cloud loadbalacer active -.-"); + log.info("random spring cloud loadbalancer active -.-"); ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider .getIfAvailable(NoopServiceInstanceListSupplier::new); return supplier.get().next().map(this::getInstanceResponse); diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-tx-example/src/main/java/com/alibaba/cloud/examples/tx/TransactionListenerImpl.java b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-tx-example/src/main/java/com/alibaba/cloud/examples/tx/TransactionListenerImpl.java index 9697f2242..6c67a1b47 100644 --- a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-tx-example/src/main/java/com/alibaba/cloud/examples/tx/TransactionListenerImpl.java +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-tx-example/src/main/java/com/alibaba/cloud/examples/tx/TransactionListenerImpl.java @@ -30,7 +30,7 @@ import org.springframework.stereotype.Component; public class TransactionListenerImpl implements TransactionListener { /** - * Excute local transaction. + * Execute local transaction. * @param msg messages * @param arg message args * @return Transaction state diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 97dd52e72..be24b764d 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -92,7 +92,7 @@ { "name": "spring.cloud.sentinel.servlet.block-page", "type": "java.lang.String", - "description": "recommoned use spring.cloud.sentinel.block-page." + "description": "recommend use spring.cloud.sentinel.block-page." }, { "name": "spring.cloud.sentinel.flow.coldFactor", From 99f329c1cf07b95c5b6f87a03c1ee76ee8135df7 Mon Sep 17 00:00:00 2001 From: lai hui <1353307710@qq.com> Date: Sun, 10 Jul 2022 09:27:12 +0800 Subject: [PATCH 02/11] fix doc error --- spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md b/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md index 53340b0b5..16ae4d026 100644 --- a/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md @@ -26,7 +26,7 @@ Binding 在消息中间件与应用程序提供的 Provider 和 Consumer 之间 下图是 Spring Cloud Stream 的架构设计。 -![](https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/images/SCSt-overview.png) +![](https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/images/SCSt-with-binder.png) From cfbf11fd8bbd8ddcc989128620f491fd7260b822 Mon Sep 17 00:00:00 2001 From: misselvexu Date: Sun, 10 Jul 2022 15:47:18 +0800 Subject: [PATCH 03/11] Add maxReconsumeTimes support . --- spring-cloud-alibaba-examples/pom.xml | 1 + .../rocketmq-example/readme-zh.md | 86 +++++++++++++++++++ .../rocketmq-example/readme.md | 86 +++++++++++++++++++ .../pom.xml | 57 ++++++++++++ .../RocketMQRetrieableConsumeApplication.java | 71 +++++++++++++++ .../src/main/resources/application.yml | 32 +++++++ .../inbound/RocketMQConsumerFactory.java | 2 + 7 files changed, 335 insertions(+) create mode 100644 spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/src/main/java/com/alibaba/cloud/examples/retrieable/RocketMQRetrieableConsumeApplication.java create mode 100644 spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/src/main/resources/application.yml diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 93c6ef14d..3fe4aa790 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -40,6 +40,7 @@ rocketmq-example/rocketmq-sql-consume-example rocketmq-example/rocketmq-example-common rocketmq-example/rocketmq-tx-example + rocketmq-example/rocketmq-retrieable-consume-example spring-cloud-bus-rocketmq-example spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-nacos-example diff --git a/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md b/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md index 53340b0b5..b6af10afc 100644 --- a/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md @@ -811,6 +811,92 @@ public class RocketMQTxApplication { } ``` +## 最大重试消费示例 + +- 重试消费消息:根据配置的重新消费的次数,服务端会根据客户端消费是否成功,进行重新推送消息。 + +### 创建Topic + +```sh +sh bin/mqadmin updateTopic -n localhost:9876 -c DefaultCluster -t retrieable +``` + +### 示例代码 + +**application.yml** + +```yaml +server: + port: 28089 +spring: + application: + name: rocketmq-retrieable-consume-example + cloud: + stream: + function: + definition: consumer; + rocketmq: + binder: + name-server: localhost:9876 + bindings: + producer-out-0: + producer: + group: output_1 + consumer-in-0: + consumer: + ## According to the configured number of `max-reconsume-times`, + ## the server will re-push the message according to whether the client's consumption is successful or not + push: + max-reconsume-times: 3 + bindings: + producer-out-0: + destination: retrieable + consumer-in-0: + destination: retrieable + group: retrieable-consumer + +logging: + level: + org.springframework.context.support: debug + +``` + +**code** + +```java +@SpringBootApplication +public class RocketMQRetrieableConsumeApplication { + + private static final Logger log = LoggerFactory + .getLogger(RocketMQRetrieableConsumeApplication.class); + + @Autowired + private StreamBridge streamBridge; + + public static void main(String[] args) { + SpringApplication.run(RocketMQRetrieableConsumeApplication.class, args); + } + + @Bean + public ApplicationRunner producer() { + return args -> { + Map headers = new HashMap<>(); + Message msg = new GenericMessage( + new SimpleMsg("Hello RocketMQ For Retrieable ."), headers); + streamBridge.send("producer-out-0", msg); + }; + } + + @Bean + public Consumer> consumer() { + return msg -> { + // Mock Exception in consumer function. + throw new RuntimeException("mock exception."); + }; + } +} +``` + ## Endpoint 信息查看 Spring Boot 应用支持通过 Endpoint 来暴露相关信息,RocketMQ Stream Starter 也支持这一点。 diff --git a/spring-cloud-alibaba-examples/rocketmq-example/readme.md b/spring-cloud-alibaba-examples/rocketmq-example/readme.md index dfd2680e0..aaaf45bf2 100644 --- a/spring-cloud-alibaba-examples/rocketmq-example/readme.md +++ b/spring-cloud-alibaba-examples/rocketmq-example/readme.md @@ -807,6 +807,92 @@ public class RocketMQTxApplication { } ``` +## Maximum Retry Consumption Example + +- Retry consumption message: According to the configured number of re-consumption, the server will re-push the message according to whether the client's consumption is successful or not. + +### Create topic + +```sh +sh bin/mqadmin updateTopic -n localhost:9876 -c DefaultCluster -t retrieable +``` + +### Example code + +**application.yml** + +```yaml +server: + port: 28089 +spring: + application: + name: rocketmq-retrieable-consume-example + cloud: + stream: + function: + definition: consumer; + rocketmq: + binder: + name-server: localhost:9876 + bindings: + producer-out-0: + producer: + group: output_1 + consumer-in-0: + consumer: + ## According to the configured number of `max-reconsume-times`, + ## the server will re-push the message according to whether the client's consumption is successful or not + push: + max-reconsume-times: 3 + bindings: + producer-out-0: + destination: retrieable + consumer-in-0: + destination: retrieable + group: retrieable-consumer + +logging: + level: + org.springframework.context.support: debug + +``` + +**code** + +```java +@SpringBootApplication +public class RocketMQRetrieableConsumeApplication { + + private static final Logger log = LoggerFactory + .getLogger(RocketMQRetrieableConsumeApplication.class); + + @Autowired + private StreamBridge streamBridge; + + public static void main(String[] args) { + SpringApplication.run(RocketMQRetrieableConsumeApplication.class, args); + } + + @Bean + public ApplicationRunner producer() { + return args -> { + Map headers = new HashMap<>(); + Message msg = new GenericMessage( + new SimpleMsg("Hello RocketMQ For Retrieable ."), headers); + streamBridge.send("producer-out-0", msg); + }; + } + + @Bean + public Consumer> consumer() { + return msg -> { + // Mock Exception in consumer function. + throw new RuntimeException("mock exception."); + }; + } +} +``` + ## Endpoint Add dependency `spring-cloud-starter-stream-rocketmq` to your pom.xml file, and configure your endpoint security strategy. diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/pom.xml b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/pom.xml new file mode 100644 index 000000000..d54b928ce --- /dev/null +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/pom.xml @@ -0,0 +1,57 @@ + + + + + com.alibaba.cloud + spring-cloud-alibaba-examples + ${revision} + ../../pom.xml + + 4.0.0 + + + rocketmq-retrieable-consume-example + Spring Cloud Starter Stream Alibaba RocketMQ Retrieable Consume Example + Example demonstrating how to use rocketmq to produce, and retrieable consume. + jar + + + + + com.alibaba.cloud + spring-cloud-starter-stream-rocketmq + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-json + + + com.alibaba.cloud + rocketmq-example-common + ${revision} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/src/main/java/com/alibaba/cloud/examples/retrieable/RocketMQRetrieableConsumeApplication.java b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/src/main/java/com/alibaba/cloud/examples/retrieable/RocketMQRetrieableConsumeApplication.java new file mode 100644 index 000000000..4ed32553a --- /dev/null +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/src/main/java/com/alibaba/cloud/examples/retrieable/RocketMQRetrieableConsumeApplication.java @@ -0,0 +1,71 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.examples.retrieable; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +import com.alibaba.cloud.examples.common.SimpleMsg; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.stream.function.StreamBridge; +import org.springframework.context.annotation.Bean; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.GenericMessage; + +/** + * RocketMQ Retrieable Consume Example . + * + * @author Palmer.Xu + */ +@SpringBootApplication +public class RocketMQRetrieableConsumeApplication { + + private static final Logger log = LoggerFactory + .getLogger(RocketMQRetrieableConsumeApplication.class); + + @Autowired + private StreamBridge streamBridge; + + public static void main(String[] args) { + SpringApplication.run(RocketMQRetrieableConsumeApplication.class, args); + } + + @Bean + public ApplicationRunner producer() { + return args -> { + Map headers = new HashMap<>(); + Message msg = new GenericMessage( + new SimpleMsg("Hello RocketMQ For Retrieable ."), headers); + streamBridge.send("producer-out-0", msg); + }; + } + + @Bean + public Consumer> consumer() { + return msg -> { + throw new RuntimeException("mock exception."); + }; + } + +} diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/src/main/resources/application.yml new file mode 100644 index 000000000..29b9d0187 --- /dev/null +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-retrieable-consume-example/src/main/resources/application.yml @@ -0,0 +1,32 @@ +server: + port: 28089 +spring: + application: + name: rocketmq-retrieable-consume-example + cloud: + stream: + function: + definition: consumer; + rocketmq: + binder: + name-server: localhost:9876 + bindings: + producer-out-0: + producer: + group: output_1 + consumer-in-0: + consumer: + ## According to the configured number of `max-reconsume-times`, + ## the server will re-push the message according to whether the client's consumption is successful or not + push: + max-reconsume-times: 3 + bindings: + producer-out-0: + destination: retrieable + consumer-in-0: + destination: retrieable + group: retrieable-consumer + +logging: + level: + org.springframework.context.support: debug diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-stream-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/integration/inbound/RocketMQConsumerFactory.java b/spring-cloud-alibaba-starters/spring-cloud-starter-stream-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/integration/inbound/RocketMQConsumerFactory.java index 13c2cd415..074ee1aa5 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-stream-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/integration/inbound/RocketMQConsumerFactory.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-stream-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/integration/inbound/RocketMQConsumerFactory.java @@ -90,6 +90,8 @@ public final class RocketMQConsumerFactory { consumer.setConsumeThreadMin(extendedConsumerProperties.getConcurrency()); consumer.setConsumeThreadMax(extendedConsumerProperties.getConcurrency()); consumer.setUnitName(consumerProperties.getUnitName()); + consumer.setMaxReconsumeTimes( + consumerProperties.getPush().getMaxReconsumeTimes()); return consumer; } From ef306f4369109159678352ebf165fa4717e8f915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84sir?= <2845964844@qq.com> Date: Sun, 10 Jul 2022 17:15:49 +0800 Subject: [PATCH 04/11] update overview pic url in readme.md of rocketmq-example --- spring-cloud-alibaba-examples/rocketmq-example/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-examples/rocketmq-example/readme.md b/spring-cloud-alibaba-examples/rocketmq-example/readme.md index dfd2680e0..dd511c742 100644 --- a/spring-cloud-alibaba-examples/rocketmq-example/readme.md +++ b/spring-cloud-alibaba-examples/rocketmq-example/readme.md @@ -22,7 +22,7 @@ Binding is Bridge between the external messaging systems and application provide This is a overview of Spring Cloud Stream. -![](https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/images/SCSt-overview.png) +![](https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/images/SCSt-with-binder.png) ## Preparation From 869daaa1c317caa122cdf559b344af2746e5fa6b Mon Sep 17 00:00:00 2001 From: poo0054 <85894494+poo0054@users.noreply.github.com> Date: Wed, 13 Jul 2022 14:40:29 +0800 Subject: [PATCH 05/11] Update SentinelAutoConfiguration.java Update sentinelbeanprocess creation, Sentinelautoconfiguration is not created in the post processor --- .../cloud/sentinel/custom/SentinelAutoConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelAutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelAutoConfiguration.java index 5d80b7350..f51796826 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelAutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelAutoConfiguration.java @@ -147,7 +147,7 @@ public class SentinelAutoConfiguration { @ConditionalOnClass(name = "org.springframework.web.client.RestTemplate") @ConditionalOnProperty(name = "resttemplate.sentinel.enabled", havingValue = "true", matchIfMissing = true) - public SentinelBeanPostProcessor sentinelBeanPostProcessor( + public static SentinelBeanPostProcessor sentinelBeanPostProcessor( ApplicationContext applicationContext) { return new SentinelBeanPostProcessor(applicationContext); } From 3628732dade4453c9dde527a906078cd98c09e35 Mon Sep 17 00:00:00 2001 From: "chengpu.rzh" Date: Thu, 14 Jul 2022 22:30:56 +0800 Subject: [PATCH 06/11] Reformat the codes --- pom.xml | 2 +- spring-cloud-alibaba-dependencies/pom.xml | 2 +- .../cloud/nacos/NacosDiscoveryProperties.java | 19 +- .../cloud/nacos/intetuntil/InetIPv6Utils.java | 295 ++++++++++-------- .../intetuntil/UtilIPv6AutoConfiguration.java | 34 +- 5 files changed, 194 insertions(+), 158 deletions(-) diff --git a/pom.xml b/pom.xml index 26cc9b9d7..55dd872aa 100644 --- a/pom.xml +++ b/pom.xml @@ -80,7 +80,7 @@ - 2.2.8.RELEASE + 2.2.9-SNAPSHOT Hoxton.SR12 diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index d471f7cac..059d85a87 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -18,7 +18,7 @@ Spring Cloud Alibaba Dependencies - 2.2.8.RELEASE + 2.2.9-SNAPSHOT 1.8.4 1.5.1 2.1.0 diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java index 8bb7e3d2b..43ac91b77 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java @@ -17,7 +17,6 @@ package com.alibaba.cloud.nacos; import java.net.Inet4Address; - import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; @@ -166,9 +165,9 @@ public class NacosDiscoveryProperties { private String networkInterface = ""; /** - * choose IPV4 or IPV6,if you don't set it will choose IPV4 + * choose IPV4 or IPV6,if you don't set it will choose IPV4. */ - private String ipType = "IPv4" ; + private String ipType = "IPv4"; /** * The port your want to register for your service instance, needn't to set it if the @@ -256,14 +255,16 @@ public class NacosDiscoveryProperties { if (StringUtils.isEmpty(ip)) { // traversing network interfaces if didn't specify a interface if (StringUtils.isEmpty(networkInterface)) { - if (ipType.equalsIgnoreCase("IPv4")){ + if (ipType.equalsIgnoreCase("IPv4")) { ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); - }else if (ipType.equalsIgnoreCase("IPv6")){ + } + else if (ipType.equalsIgnoreCase("IPv6")) { ip = inetIPUtils.findFirstNonLoopbackHostInfo().getIpAddress(); int index = ip.indexOf('%'); ip = index > 0 ? ip.substring(0, index) : ip; - ip = "["+ip+"]"; - }else { + ip = "[" + ip + "]"; + } + else { throw new IllegalArgumentException( "please checking the type of IP " + ipType); } @@ -281,7 +282,7 @@ public class NacosDiscoveryProperties { InetAddress currentAddress = inetAddress.nextElement(); if (currentAddress instanceof Inet4Address || currentAddress instanceof Inet6Address - && !currentAddress.isLoopbackAddress()) { + && !currentAddress.isLoopbackAddress()) { ip = currentAddress.getHostAddress(); break; } @@ -335,7 +336,7 @@ public class NacosDiscoveryProperties { this.logName = logName; } - public void setInetIPUtils(InetIPv6Utils inetIPUtils){ + public void setInetIPUtils(InetIPv6Utils inetIPUtils) { this.inetIPUtils = inetIPUtils; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java index 3a596d7e2..e0511588d 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java @@ -1,13 +1,23 @@ -package com.alibaba.cloud.nacos.intetuntil; +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.cloud.commons.util.InetUtils; -import org.springframework.cloud.commons.util.InetUtilsProperties; +package com.alibaba.cloud.nacos.intetuntil; import java.io.Closeable; import java.io.IOException; - import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; @@ -19,140 +29,149 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.cloud.commons.util.InetUtils; +import org.springframework.cloud.commons.util.InetUtilsProperties; + /** * @author HH */ public class InetIPv6Utils implements Closeable { - private final ExecutorService executorService; - - private final Log log = LogFactory.getLog(InetIPv6Utils.class); - - private final InetUtilsProperties properties; - - public InetIPv6Utils(final InetUtilsProperties properties) { - this.properties = properties; - this.executorService = Executors.newSingleThreadExecutor((r) -> { - Thread thread = new Thread(r); - thread.setName("spring.cloud.alibaba.inetutilsIPV6"); - thread.setDaemon(true); - return thread; - }); - } - - public void close() { - this.executorService.shutdown(); - } - - public InetUtils.HostInfo findFirstNonLoopbackHostInfo() { - InetAddress address = this.findFirstNonLoopbackIPv6Address(); - if (address != null) { - return this.convertAddress(address); - } else { - InetUtils.HostInfo hostInfo = new InetUtils.HostInfo(); - this.properties.setDefaultIpAddress("0:0:0:0:0:0:0:1"); - hostInfo.setHostname(this.properties.getDefaultHostname()); - hostInfo.setIpAddress(this.properties.getDefaultIpAddress()); - return hostInfo; - } - } - - public InetAddress findFirstNonLoopbackIPv6Address() { - InetAddress address = null; - - try { - int lowest = Integer.MAX_VALUE; - for (Enumeration nics = NetworkInterface.getNetworkInterfaces(); - nics.hasMoreElements();) { - NetworkInterface ifc = nics.nextElement(); - if (ifc.isUp()) { - log.trace("Testing interface:" + ifc.getDisplayName()); - if (ifc.getIndex() < lowest || address == null) { - lowest = ifc.getIndex(); - } - else if (address != null) { - continue; - } - - if (!ignoreInterface(ifc.getDisplayName())) { - for (Enumeration addrs = ifc - .getInetAddresses(); addrs.hasMoreElements();) { - InetAddress inetAddress = addrs.nextElement(); - if (inetAddress instanceof Inet6Address - && !inetAddress.isLoopbackAddress() - && isPreferredAddress(inetAddress)) { - log.trace("Found non-loopback interface: " - + ifc.getDisplayName()); - address = inetAddress; - } - } - } - } - } - } - catch (IOException e) { - log.error("Cannot get first non-loopback address", e); - } - - if (address != null) { - return address; - } - - try { - return InetAddress.getLocalHost(); - } - catch (UnknownHostException e) { - log.warn("Unable to retrieve localhost"); - } - - return null; - } - - boolean isPreferredAddress(InetAddress address) { - if (this.properties.isUseOnlySiteLocalInterfaces()) { - final boolean siteLocalAddress = address.isSiteLocalAddress(); - if (!siteLocalAddress) { - log.trace("Ignoring address"+address.getHostAddress()); - } - return siteLocalAddress; - } - final List preferredNetworks = this.properties.getPreferredNetworks(); - if (preferredNetworks.isEmpty()) { - return true; - } - for (String regex : preferredNetworks) { - final String hostAddress = address.getHostAddress(); - if (hostAddress.matches(regex) || hostAddress.startsWith(regex)) { - return true; - } - } - log.trace("Ignoring address: " + address.getHostAddress()); - return false; - } - - boolean ignoreInterface(String interfaceName) { - for (String regex : this.properties.getIgnoredInterfaces()) { - if (interfaceName.matches(regex)) { - log.trace("Ignoring interface: " + interfaceName); - return true; - } - } - return false; - } - - public InetUtils.HostInfo convertAddress(final InetAddress address) { - InetUtils.HostInfo hostInfo = new InetUtils.HostInfo(); - Future result = this.executorService.submit(address::getHostName); - - String hostname; - try { - hostname = result.get(this.properties.getTimeoutSeconds(), TimeUnit.SECONDS); - } - catch (Exception e) { - log.info("Cannot determine local hostname"); - hostname = "localhost"; - } - hostInfo.setHostname(hostname); - hostInfo.setIpAddress(address.getHostAddress()); - return hostInfo; - } + + private final ExecutorService executorService; + + private final Log log = LogFactory.getLog(InetIPv6Utils.class); + + private final InetUtilsProperties properties; + + public InetIPv6Utils(final InetUtilsProperties properties) { + this.properties = properties; + this.executorService = Executors.newSingleThreadExecutor((r) -> { + Thread thread = new Thread(r); + thread.setName("spring.cloud.alibaba.inetutilsIPV6"); + thread.setDaemon(true); + return thread; + }); + } + + public void close() { + this.executorService.shutdown(); + } + + public InetUtils.HostInfo findFirstNonLoopbackHostInfo() { + InetAddress address = this.findFirstNonLoopbackIPv6Address(); + if (address != null) { + return this.convertAddress(address); + } + else { + InetUtils.HostInfo hostInfo = new InetUtils.HostInfo(); + this.properties.setDefaultIpAddress("0:0:0:0:0:0:0:1"); + hostInfo.setHostname(this.properties.getDefaultHostname()); + hostInfo.setIpAddress(this.properties.getDefaultIpAddress()); + return hostInfo; + } + } + + public InetAddress findFirstNonLoopbackIPv6Address() { + InetAddress address = null; + + try { + int lowest = Integer.MAX_VALUE; + for (Enumeration nics = NetworkInterface + .getNetworkInterfaces(); nics.hasMoreElements();) { + NetworkInterface ifc = nics.nextElement(); + if (ifc.isUp()) { + log.trace("Testing interface:" + ifc.getDisplayName()); + if (ifc.getIndex() < lowest || address == null) { + lowest = ifc.getIndex(); + } + else if (address != null) { + continue; + } + + if (!ignoreInterface(ifc.getDisplayName())) { + for (Enumeration addrs = ifc + .getInetAddresses(); addrs.hasMoreElements();) { + InetAddress inetAddress = addrs.nextElement(); + if (inetAddress instanceof Inet6Address + && !inetAddress.isLoopbackAddress() + && isPreferredAddress(inetAddress)) { + log.trace("Found non-loopback interface: " + + ifc.getDisplayName()); + address = inetAddress; + } + } + } + } + } + } + catch (IOException e) { + log.error("Cannot get first non-loopback address", e); + } + + if (address != null) { + return address; + } + + try { + return InetAddress.getLocalHost(); + } + catch (UnknownHostException e) { + log.warn("Unable to retrieve localhost"); + } + + return null; + } + + boolean isPreferredAddress(InetAddress address) { + if (this.properties.isUseOnlySiteLocalInterfaces()) { + final boolean siteLocalAddress = address.isSiteLocalAddress(); + if (!siteLocalAddress) { + log.trace("Ignoring address" + address.getHostAddress()); + } + return siteLocalAddress; + } + final List preferredNetworks = this.properties.getPreferredNetworks(); + if (preferredNetworks.isEmpty()) { + return true; + } + for (String regex : preferredNetworks) { + final String hostAddress = address.getHostAddress(); + if (hostAddress.matches(regex) || hostAddress.startsWith(regex)) { + return true; + } + } + log.trace("Ignoring address: " + address.getHostAddress()); + return false; + } + + boolean ignoreInterface(String interfaceName) { + for (String regex : this.properties.getIgnoredInterfaces()) { + if (interfaceName.matches(regex)) { + log.trace("Ignoring interface: " + interfaceName); + return true; + } + } + return false; + } + + public InetUtils.HostInfo convertAddress(final InetAddress address) { + InetUtils.HostInfo hostInfo = new InetUtils.HostInfo(); + Future result = this.executorService.submit(address::getHostName); + + String hostname; + try { + hostname = result.get(this.properties.getTimeoutSeconds(), TimeUnit.SECONDS); + } + catch (Exception e) { + log.info("Cannot determine local hostname"); + hostname = "localhost"; + } + hostInfo.setHostname(hostname); + hostInfo.setIpAddress(address.getHostAddress()); + return hostInfo; + } + } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java index f7588ec9e..0e581f00e 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.cloud.nacos.intetuntil; import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled; @@ -8,7 +24,6 @@ import org.springframework.cloud.commons.util.InetUtilsProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; - /** * @author HH */ @@ -16,13 +31,14 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnDiscoveryEnabled @ConditionalOnNacosDiscoveryEnabled public class UtilIPv6AutoConfiguration { - public UtilIPv6AutoConfiguration() { - } - @Bean - @ConditionalOnMissingBean - public InetIPv6Utils inetUtils(InetUtilsProperties properties) { - return new InetIPv6Utils(properties); - } -} + public UtilIPv6AutoConfiguration() { + } + + @Bean + @ConditionalOnMissingBean + public InetIPv6Utils inetUtils(InetUtilsProperties properties) { + return new InetIPv6Utils(properties); + } +} From 5fd365ec8acb9615b374d898d27bb29cc7f0f119 Mon Sep 17 00:00:00 2001 From: lai hui <1353307710@qq.com> Date: Thu, 14 Jul 2022 17:42:17 +0800 Subject: [PATCH 07/11] add example --- .../src/main/resources/application.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/resources/application.properties index e967cd12f..408e9d2c2 100755 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/resources/application.properties @@ -3,6 +3,8 @@ spring.application.name=service-provider spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.enabled=true #spring.cloud.nacos.discovery.instance-enabled=true +#register IPv6 instance +#spring.cloud.nacos.discovery.ip-type=IPv6 spring.cloud.nacos.username=nacos spring.cloud.nacos.password=nacos From 6eccc24ba58c0730bf3246af2886458f94406a75 Mon Sep 17 00:00:00 2001 From: zhangzhi Date: Fri, 15 Jul 2022 11:41:10 +0800 Subject: [PATCH 08/11] solve #2658 SeataRestTemplateAutoConfiguration Circular dependency in --- .../SeataRestTemplateAutoConfiguration.java | 39 +++----------- ...TemplateInterceptorAfterPropertiesSet.java | 51 +++++++++++++++++++ 2 files changed, 57 insertions(+), 33 deletions(-) create mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/rest/SeataRestTemplateInterceptorAfterPropertiesSet.java diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/rest/SeataRestTemplateAutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/rest/SeataRestTemplateAutoConfiguration.java index b9b587568..c8430e9fa 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/rest/SeataRestTemplateAutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/rest/SeataRestTemplateAutoConfiguration.java @@ -16,17 +16,8 @@ package com.alibaba.cloud.seata.rest; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import javax.annotation.PostConstruct; - -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.web.client.RestTemplate; /** * @author xiaojing @@ -35,32 +26,14 @@ import org.springframework.web.client.RestTemplate; @Configuration(proxyBeanMethods = false) public class SeataRestTemplateAutoConfiguration { - @Autowired(required = false) - private Collection restTemplates; - - @Autowired - private SeataRestTemplateInterceptor seataRestTemplateInterceptor; - - @PostConstruct - public void init() { - if (this.restTemplates != null) { - for (RestTemplate restTemplate : restTemplates) { - List interceptors = new ArrayList( - restTemplate.getInterceptors()); - interceptors.add(this.seataRestTemplateInterceptor); - restTemplate.setInterceptors(interceptors); - } - } + @Bean + public SeataRestTemplateInterceptor seataRestTemplateInterceptor() { + return new SeataRestTemplateInterceptor(); } - @Configuration(proxyBeanMethods = false) - static class SeataRestTemplateInterceptorConfiguration { - - @Bean - public SeataRestTemplateInterceptor seataRestTemplateInterceptor() { - return new SeataRestTemplateInterceptor(); - } - + @Bean + public SeataRestTemplateInterceptorAfterPropertiesSet seataRestTemplateInterceptorConfiguration() { + return new SeataRestTemplateInterceptorAfterPropertiesSet(); } } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/rest/SeataRestTemplateInterceptorAfterPropertiesSet.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/rest/SeataRestTemplateInterceptorAfterPropertiesSet.java new file mode 100644 index 000000000..efef0bd64 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-seata/src/main/java/com/alibaba/cloud/seata/rest/SeataRestTemplateInterceptorAfterPropertiesSet.java @@ -0,0 +1,51 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.seata.rest; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.web.client.RestTemplate; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * @author ZhangZhi + */ +public class SeataRestTemplateInterceptorAfterPropertiesSet implements InitializingBean { + + @Autowired(required = false) + private Collection restTemplates; + + @Autowired + private SeataRestTemplateInterceptor seataRestTemplateInterceptor; + + @Override + public void afterPropertiesSet() { + if (this.restTemplates != null) { + for (RestTemplate restTemplate : restTemplates) { + List interceptors = new ArrayList<>( + restTemplate.getInterceptors()); + interceptors.add(this.seataRestTemplateInterceptor); + restTemplate.setInterceptors(interceptors); + } + } + } + +} From 58795a679a22d83055bd0cfc662f38f24188c1cb Mon Sep 17 00:00:00 2001 From: "chengpu.rzh" Date: Sun, 24 Jul 2022 21:08:03 +0800 Subject: [PATCH 09/11] Fixed wrong registeration bean --- .../java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java | 1 + .../cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java | 2 +- .../src/main/resources/META-INF/spring.factories | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java index e0511588d..5613b22d7 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/InetIPv6Utils.java @@ -56,6 +56,7 @@ public class InetIPv6Utils implements Closeable { }); } + @Override public void close() { this.executorService.shutdown(); } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java index 0e581f00e..461ca1d4c 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/intetuntil/UtilIPv6AutoConfiguration.java @@ -37,7 +37,7 @@ public class UtilIPv6AutoConfiguration { @Bean @ConditionalOnMissingBean - public InetIPv6Utils inetUtils(InetUtilsProperties properties) { + public InetIPv6Utils inetIPv6Utils(InetUtilsProperties properties) { return new InetIPv6Utils(properties); } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/spring.factories index 92f91f592..50dfd2efa 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/spring.factories @@ -7,7 +7,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration,\ com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration,\ com.alibaba.cloud.nacos.NacosServiceAutoConfiguration,\ - com.alibaba.cloud.nacos.intetuntil.InetIPv6Utils + com.alibaba.cloud.nacos.intetuntil.UtilIPv6AutoConfiguration org.springframework.cloud.bootstrap.BootstrapConfiguration=\ com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration org.springframework.context.ApplicationListener=\ From 2ff47457926e258deab6a5715934b1c485526e6b Mon Sep 17 00:00:00 2001 From: MageekChiu <1223732872@qq.com> Date: Sat, 23 Jul 2022 17:03:31 +0800 Subject: [PATCH 10/11] feat(nacos): add meta method for instance --- .../cloud/nacos/NacosServiceInstance.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosServiceInstance.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosServiceInstance.java index 1db224499..f6fba00aa 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosServiceInstance.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosServiceInstance.java @@ -18,6 +18,7 @@ package com.alibaba.cloud.nacos; import java.net.URI; import java.util.Map; +import java.util.Objects; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; @@ -87,4 +88,27 @@ public class NacosServiceInstance implements ServiceInstance { this.metadata = metadata; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + NacosServiceInstance that = (NacosServiceInstance) o; + + return Objects.equals(this.serviceId, that.serviceId) + && Objects.equals(this.instanceId, that.instanceId) + && Objects.equals(this.host, that.host) + && this.port == that.port + && this.secure == that.secure + && Objects.equals(this.metadata, that.metadata); + } + + @Override + public int hashCode() { + return (instanceId == null) ? 31 : (instanceId.hashCode() + 31); + } + } From 655b950d4d4ba577c488dfbee8d38c2d43fbaa87 Mon Sep 17 00:00:00 2001 From: "chengpu.rzh" Date: Mon, 25 Jul 2022 19:49:10 +0800 Subject: [PATCH 11/11] Deleted missing field --- .../java/com/alibaba/cloud/nacos/NacosServiceInstance.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosServiceInstance.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosServiceInstance.java index f6fba00aa..2c087e8d4 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosServiceInstance.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosServiceInstance.java @@ -99,7 +99,6 @@ public class NacosServiceInstance implements ServiceInstance { NacosServiceInstance that = (NacosServiceInstance) o; return Objects.equals(this.serviceId, that.serviceId) - && Objects.equals(this.instanceId, that.instanceId) && Objects.equals(this.host, that.host) && this.port == that.port && this.secure == that.secure @@ -108,7 +107,7 @@ public class NacosServiceInstance implements ServiceInstance { @Override public int hashCode() { - return (instanceId == null) ? 31 : (instanceId.hashCode() + 31); + return (serviceId == null) ? 31 : (serviceId.hashCode() + 31); } }