Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Raft cluster mode supports address translation #7069

Merged
merged 13 commits into from
Dec 30, 2024
4 changes: 3 additions & 1 deletion changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Add changes here for all PR submitted to the 2.x branch.

### feature:

- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] support XXX
- [[#7069](https://github.com/apache/incubator-seata/pull/7069)] Raft mode supports access outside the cluster


### bugfix:

Expand Down Expand Up @@ -35,5 +36,6 @@ Thanks to these contributors for their code commits. Please report an unintended
- [slievrly](https://github.com/slievrly)
- [lyl2008dsg](https://github.com/lyl2008dsg)
- [remind](https://github.com/remind)
- [PeppaO](https://github.com/PeppaO)

Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
3 changes: 2 additions & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### feature:

- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] 支持XXX
- [[#7069](https://github.com/apache/incubator-seata/pull/7069)] Raft模式支持集群外访问
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该是 raft集群模式支持地址转换

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


### bugfix:

Expand Down Expand Up @@ -35,5 +35,6 @@
- [slievrly](https://github.com/slievrly)
- [lyl2008dsg](https://github.com/lyl2008dsg)
- [remind](https://github.com/remind)
- [PeppaO](https://github.com/PeppaO)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
Original file line number Diff line number Diff line change
Expand Up @@ -1110,4 +1110,15 @@ public interface ConfigurationKeys {
* The constant META_PREFIX
*/
String META_PREFIX = SEATA_FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR + FILE_ROOT_REGISTRY + FILE_CONFIG_SPLIT_CHAR + "metadata.";

/**
* The constant SERVER_REGISTRY_METADATA_PREFIX
*/
String SERVER_REGISTRY_METADATA_PREFIX = SERVER_PREFIX + FILE_ROOT_REGISTRY + ".metadata";

/**
* The constant SERVER_REGISTRY_METADATA_EXTERNAL
*/
String SERVER_REGISTRY_METADATA_EXTERNAL = SERVER_REGISTRY_METADATA_PREFIX + ".external";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.seata.common.exception;

public class ParseEndpointException extends RuntimeException {
public ParseEndpointException() {
}

public ParseEndpointException(String message) {
super(message);
}

public ParseEndpointException(String message, Throwable cause) {
super(message, cause);
}

public ParseEndpointException(Throwable cause) {
super(cause);
}

public ParseEndpointException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
86 changes: 84 additions & 2 deletions common/src/main/java/org/apache/seata/common/metadata/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.seata.common.exception.ParseEndpointException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import java.util.List;
import java.util.ArrayList;

public class Node {

Expand Down Expand Up @@ -195,4 +196,85 @@ public String toString() {
}
}

private Node.ExternalEndpoint createExternalEndpoint(String host, int controllerPort, int transactionPort) {
return new Node.ExternalEndpoint(host, controllerPort, transactionPort);
}

public List<ExternalEndpoint> createExternalEndpoints(String external) {
List<Node.ExternalEndpoint> externalEndpoints = new ArrayList<>();
String[] split = external.split(",");

for (String s : split) {
String[] item = s.split(":");
if (item.length == 3) {
try {
String host = item[0];
int controllerPort = Integer.parseInt(item[1]);
int transactionPort = Integer.parseInt(item[2]);
externalEndpoints.add(createExternalEndpoint(host, controllerPort, transactionPort));
} catch (NumberFormatException e) {
throw new ParseEndpointException("Invalid port number in: " + s);
}
} else {
throw new ParseEndpointException("Invalid format for endpoint: " + s);
}
}
return externalEndpoints;
}

public Map<String, Object> updateMetadataWithExternalEndpoints(Map<String, Object> metadata, List<Node.ExternalEndpoint> externalEndpoints) {
Object obj = metadata.get("external");
if (obj == null) {
if (!externalEndpoints.isEmpty()) {
Map<String, Object> metadataMap = new HashMap<>(metadata);
metadataMap.put("external", externalEndpoints);
return metadataMap;
}
return metadata;
}
if (obj instanceof List) {
List<Node.ExternalEndpoint> oldList = (List<Node.ExternalEndpoint>) obj;
oldList.addAll(externalEndpoints);
return metadata;
} else {
throw new ParseEndpointException("Metadata 'external' is not a List.");
}
}

public static class ExternalEndpoint {

private String host;
private int controlPort;
private int transactionPort;

public ExternalEndpoint(String host, int controlPort, int transactionPort) {
this.host = host;
this.controlPort = controlPort;
this.transactionPort = transactionPort;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getControlPort() {
return controlPort;
}

public void setControlPort(int controlPort) {
this.controlPort = controlPort;
}

public int getTransactionPort() {
return transactionPort;
}

public void setTransactionPort(int transactionPort) {
this.transactionPort = transactionPort;
}
}
}
16 changes: 16 additions & 0 deletions common/src/main/java/org/apache/seata/common/util/NetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,25 @@ public static String toIpAddress(SocketAddress address) {
* @return the string
*/
public static String toStringAddress(InetSocketAddress address) {
if (address.getAddress() == null) {
return address.getHostString() + ":" + address.getPort();
}
return address.getAddress().getHostAddress() + ":" + address.getPort();
}

/**
* To string host string.
*
* @param address the address
* @return the string
*/
public static String toStringHost(InetSocketAddress address) {
if (address.getAddress() == null) {
return address.getHostString();
}
return address.getAddress().getHostAddress();
}

/**
* To inet socket address inet socket address.
*
Expand Down
Loading
Loading