Skip to content
This repository was archived by the owner on Oct 13, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alpha/alpha-server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ spring:
healthCheckPath: /actuator/health
healthCheckInterval: 10s
instanceId: ${spring.application.name}-${alpha.server.host}-${server.port}
tags: alpha-server-host=${alpha.server.host},alpha-server-port:${alpha.server.port}
tags: alpha-server-host=${alpha.server.host},alpha-server-port=${alpha.server.port}

eureka:
client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void setDiscoveryInfo(String discoveryInfo) {
}

public enum DiscoveryType{
DEFAULT,SPRING_CLOUD_EUREKA
DEFAULT,EUREKA,CONSUL
}

public static final Builder builder(){
Expand Down
52 changes: 52 additions & 0 deletions omega/omega-spring-cloud-consul-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>omega</artifactId>
<groupId>org.apache.servicecomb.pack</groupId>
<version>0.4.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>omega-spring-cloud-consul-starter</artifactId>
<name>Pack::Omega::Spring Cloud Consul Starter</name>

<dependencies>
<dependency>
<groupId>org.apache.servicecomb.pack</groupId>
<artifactId>omega-spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb.pack</groupId>
<artifactId>omega-format</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb.pack</groupId>
<artifactId>omega-connector-grpc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>${spring.cloud.starter.consul.discovery.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.servicecomb.pack.omega.spring.cloud;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(OmegaSpringConsulConfig.class)
@ConditionalOnProperty(value = {"omega.enabled"}, matchIfMissing = true)
public class OmegaSpringConsulAutoConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.
*/

/**
* Get the access address of Alpah Server from Consul
* Turn this feautre on by set alpha.cluster.register.type=consul
* First omega gets the Alpha address from Consul with ${alpha.cluster.serviceId}
* If omega can't get it in Consul then use ${alpha.cluster.address}
*/

package org.apache.servicecomb.pack.omega.spring.cloud;

import org.apache.servicecomb.pack.omega.connector.grpc.AlphaClusterDiscovery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.consul.ConsulAutoConfiguration;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
@ConditionalOnProperty(value = {"alpha.cluster.register.type"}, havingValue = "consul")
@AutoConfigureAfter(value = {ConsulAutoConfiguration.class})
class OmegaSpringConsulConfig {

private static final Logger LOG = LoggerFactory.getLogger(OmegaSpringConsulConfig.class);
private static final String ALPHA_SERVER_HOST_KEY = "alpha-server-host";
private static final String ALPHA_SERVER_PORT_KEY = "alpha-server-port";

@Autowired
public ConsulDiscoveryClient discoveryClient;

@Bean
AlphaClusterDiscovery alphaClusterAddress(
@Value("${alpha.cluster.serviceId:servicecomb-alpha-server}") String serviceId,
@Value("${alpha.cluster.address:localhost:8080}") String[] addresses) {
StringBuffer eurekaServiceUrls = new StringBuffer();

String[] alphaAddresses = this.getAlphaAddress(serviceId);
if (alphaAddresses.length > 0) {
AlphaClusterDiscovery alphaClusterDiscovery = AlphaClusterDiscovery.builder()
.discoveryType(AlphaClusterDiscovery.DiscoveryType.CONSUL)
.discoveryInfo(eurekaServiceUrls.toString())
.addresses(alphaAddresses)
.build();
return alphaClusterDiscovery;
} else {
AlphaClusterDiscovery alphaClusterDiscovery = AlphaClusterDiscovery.builder()
.discoveryType(AlphaClusterDiscovery.DiscoveryType.DEFAULT)
.addresses(addresses)
.build();
return alphaClusterDiscovery;
}
}

private String[] getAlphaAddress(String serviceId) {
List<String> alphaAddresses = new ArrayList<>();
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceId);
boolean foundAlphaServer = Boolean.FALSE;
for (ServiceInstance serviceInstance : serviceInstances) {
foundAlphaServer = Boolean.TRUE;
if (serviceInstance.getMetadata().containsKey(ALPHA_SERVER_HOST_KEY) && serviceInstance.getMetadata().containsKey(ALPHA_SERVER_PORT_KEY)) {
alphaAddresses.add(serviceInstance.getMetadata().get(ALPHA_SERVER_HOST_KEY).trim()+":"+serviceInstance.getMetadata().get(ALPHA_SERVER_PORT_KEY).trim());
}else{
LOG.warn("Ignore alpha instance {}, No tag {} or {} found ",serviceInstance.getServiceId(),ALPHA_SERVER_HOST_KEY,ALPHA_SERVER_PORT_KEY);
}
}
if (foundAlphaServer) {
if (alphaAddresses.size() == 0) {
LOG.warn("Alpha has been found in Consul, " +
"but Alpha's registered address information is not found in Consul instance tags. " +
"Please check Alpha is configured spring.cloud.consul.enabled=true and version 0.4.0+");
}
} else {
LOG.warn("No Alpha Server {} found in the Consul", serviceId);
}
return alphaAddresses.toArray(new String[alphaAddresses.size()]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.apache.servicecomb.pack.omega.spring.cloud.OmegaSpringConsulAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ AlphaClusterDiscovery alphaClusterAddress(
String[] alphaAddresses = this.getAlphaAddress(serviceId);
if (alphaAddresses.length > 0) {
AlphaClusterDiscovery alphaClusterDiscovery = AlphaClusterDiscovery.builder()
.discoveryType(AlphaClusterDiscovery.DiscoveryType.SPRING_CLOUD_EUREKA)
.discoveryType(AlphaClusterDiscovery.DiscoveryType.EUREKA)
.discoveryInfo(eurekaServiceUrls.toString())
.addresses(alphaAddresses)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ AlphaClusterConfig alphaClusterConfig(
@Lazy MessageHandler handler,
@Lazy TccMessageHandler tccMessageHandler) {

LOG.info("Discovery alpha cluster address from {} {}",alphaClusterDiscovery.getDiscoveryType().name()
,alphaClusterDiscovery.getDiscoveryInfo() == null ? "" : alphaClusterDiscovery.getDiscoveryInfo());
LOG.info("Discovery alpha cluster address {} from {}",alphaClusterDiscovery.getAddresses() == null ? "" : String.join(",",alphaClusterDiscovery.getAddresses()), alphaClusterDiscovery.getDiscoveryType().name());
MessageFormat messageFormat = new KryoMessageFormat();
AlphaClusterConfig clusterConfig = AlphaClusterConfig.builder()
.addresses(ImmutableList.copyOf(alphaClusterDiscovery.getAddresses()))
Expand Down
1 change: 1 addition & 0 deletions omega/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<module>omega-connector</module>
<module>omega-spring-starter</module>
<module>omega-spring-cloud-eureka-starter</module>
<module>omega-spring-cloud-consul-starter</module>
<module>omega-format</module>
</modules>

Expand Down