Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@

package org.zowe.apiml.discovery.config;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.eureka.DefaultEurekaServerContext;
import com.netflix.eureka.EurekaServerConfig;
import com.netflix.eureka.EurekaServerContextHolder;
import com.netflix.eureka.cluster.PeerEurekaNodes;
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
import com.netflix.eureka.resources.ServerCodecs;
import com.netflix.eureka.transport.EurekaServerHttpClientFactory;
import com.netflix.eureka.util.EurekaMonitors;
import jakarta.ws.rs.client.ClientRequestFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.eureka.server.InstanceRegistryProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.zowe.apiml.discovery.ApimlInstanceRegistry;
import org.zowe.apiml.discovery.eureka.RefreshablePeerEurekaNodes;
Expand All @@ -37,6 +40,7 @@
* Configuration to rewrite default Eureka's implementation with custom one
*/
@Configuration
@Slf4j
public class EurekaConfig {

@Value("${apiml.discovery.serviceIdPrefixReplacer:#{null}}")
Expand All @@ -47,29 +51,62 @@ public class EurekaConfig {

@Bean
@Primary
public ApimlInstanceRegistry getApimlInstanceRegistry(
public ApimlInstanceRegistry apimlInstanceRegistry(
EurekaServerConfig serverConfig,
EurekaClientConfig clientConfig,
ServerCodecs serverCodecs,
EurekaClient eurekaClient,
EurekaServerHttpClientFactory eurekaServerHttpClientFactory,
InstanceRegistryProperties instanceRegistryProperties,
ApplicationContext appCntx) {
ApplicationContext appCntx,
Collection<ClientRequestFilter> replicationClientAdditionalFilters,
@Qualifier("secureSslContext") SSLContext secureSslContext
) {
eurekaClient.getApplications(); // force initialization
return new ApimlInstanceRegistry(serverConfig, clientConfig, serverCodecs, eurekaClient, eurekaServerHttpClientFactory, instanceRegistryProperties, appCntx, new Tuple(tuple));

var apimlInstanceRegistry = new ApimlInstanceRegistry(serverConfig, clientConfig, serverCodecs, eurekaClient, eurekaServerHttpClientFactory, instanceRegistryProperties, appCntx, new Tuple(tuple));

var applicationInfoManager = eurekaClient.getApplicationInfoManager();

PeerEurekaNodes peerEurekaNodes = new RefreshablePeerEurekaNodes(
apimlInstanceRegistry,
serverConfig,
clientConfig,
serverCodecs,
applicationInfoManager,
replicationClientAdditionalFilters,
secureSslContext,
maxPeerRetries
);

var serverContext = new DefaultEurekaServerContext(
serverConfig,
serverCodecs,
apimlInstanceRegistry,
peerEurekaNodes,
applicationInfoManager
);

EurekaServerContextHolder.initialize(serverContext);

serverContext.initialize();
log.info("Initialized server context");

// Copy registry from neighboring eureka node
//int registryCount = apimlInstanceRegistry.syncUp();
//apimlInstanceRegistry.openForTraffic(applicationInfoManager, registryCount);
Comment on lines +95 to +97
Copy link
Contributor

Choose a reason for hiding this comment

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

is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I was thinking about it a lot (therefor I decided to leave it there as a comment). These lines make blocking call between the Discovery services. Basically, it is waiting till the mirror is copied. I have tested on my local machine and it prolong start-up for a minute. The problem is that we don't control DVIPA and the client can call APIML only based on the state the port is listening. It would be great (at least for Modulith) if we enable port after this it could be reasonable. For now I prefer to use the same behaviour (read the peer asynchronously).


// Register all monitoring statistics.
EurekaMonitors.registerAllStats();

return apimlInstanceRegistry;
}

@Bean
@Primary
public PeerEurekaNodes peerEurekaNodes(
PeerAwareInstanceRegistry registry, ServerCodecs serverCodecs,
Collection<ClientRequestFilter> replicationClientAdditionalFilters,
ApplicationInfoManager applicationInfoManager, EurekaServerConfig eurekaServerConfig,
EurekaClientConfig eurekaClientConfig, @Qualifier("secureSslContext") SSLContext secureSslContext
) {
return new RefreshablePeerEurekaNodes(registry, eurekaServerConfig,
eurekaClientConfig, serverCodecs, applicationInfoManager,
replicationClientAdditionalFilters, secureSslContext, maxPeerRetries);
@DependsOn("apimlInstanceRegistry")
public PeerEurekaNodes peerEurekaNodes() {
return EurekaServerContextHolder.getInstance().getServerContext().getPeerEurekaNodes();
}

public static class Tuple {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/

package org.zowe.apiml.discovery.config;

import com.netflix.eureka.cluster.PeerEurekaNodes;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.util.ReflectionTestUtils;
import org.zowe.apiml.discovery.ApimlInstanceRegistry;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;

class EurekaConfigTest {

@Nested
@SpringBootTest
class Initialization {

@Autowired
private PeerEurekaNodes peerEurekaNodes;

@Autowired
private ApimlInstanceRegistry apimlInstanceRegistry;

@Test
void givenDefaultConfiguration_whenInitialize_thenPeerEurekaNodesIsAvailable() {
assertNotNull(peerEurekaNodes);
}

@Test
void givenDefaultConfiguration_whenInitialize_thenApimlInstanceRegistryIsFullyConfigured() {
assertNotNull(apimlInstanceRegistry);
var setPeerEurekaNodes = ReflectionTestUtils.getField(apimlInstanceRegistry, "peerEurekaNodes");
assertNotNull(setPeerEurekaNodes);
assertSame(peerEurekaNodes, setPeerEurekaNodes);
}

}

}
Loading