org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent Java Examples

The following examples show how to use org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EurekaInstanceRegisteredListener.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Translates service instance Eureka metadata from older versions to the current version
 */
@EventListener
public void listen(EurekaInstanceRegisteredEvent event) {
    final InstanceInfo instanceInfo = event.getInstanceInfo();
    final Map<String, String> metadata = instanceInfo.getMetadata();
    final String serviceId = EurekaUtils.getServiceIdFromInstanceId(instanceInfo.getInstanceId());

    metadataTranslationService.translateMetadata(serviceId, metadata);
    metadataDefaultsService.updateMetadata(serviceId, metadata);

    if (StringUtils.equalsIgnoreCase(GatewayNotifier.GATEWAY_SERVICE_ID, serviceId)) {
        /**
         * meanwhile gateway was down, another Gateway could receive logout, those invalidated credentials should
         * be distributed to this new Gateway
         */
        gatewayNotifier.distributeInvalidatedCredentials(instanceInfo.getInstanceId());
    }
    // ie. new instance can have different authentication (than other one), this is reason to evict caches on gateway
    gatewayNotifier.serviceUpdated(serviceId, instanceInfo.getInstanceId());
}
 
Example #2
Source File: InstanceRegistryTest.java    From didi-eureka-server with MIT License 6 votes vote down vote up
@Test
public void testRegister() throws Exception {
	// creating instance info
	final LeaseInfo leaseInfo = getLeaseInfo();
	final InstanceInfo instanceInfo = getInstanceInfo(APP_NAME, HOST_NAME, INSTANCE_ID, PORT, leaseInfo);
	// calling tested method
	instanceRegistry.register(instanceInfo, false);
	// event of proper type is registered
	assertEquals(1, this.testEvents.applicationEvents.size());
	assertTrue(this.testEvents.applicationEvents.get(0) instanceof EurekaInstanceRegisteredEvent);
	// event details are correct
	final EurekaInstanceRegisteredEvent registeredEvent =
			(EurekaInstanceRegisteredEvent) (this.testEvents.applicationEvents.get(0));
	assertEquals(instanceInfo, registeredEvent.getInstanceInfo());
	assertEquals(leaseInfo.getDurationInSecs(), registeredEvent.getLeaseDuration());
	assertEquals(instanceRegistry, registeredEvent.getSource());
	assertFalse(registeredEvent.isReplication());
}
 
Example #3
Source File: InstanceRegistryTests.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@Test
public void testRegister() throws Exception {
	// creating instance info
	final LeaseInfo leaseInfo = getLeaseInfo();
	final InstanceInfo instanceInfo = getInstanceInfo(APP_NAME, HOST_NAME,
			INSTANCE_ID, PORT, leaseInfo);
	// calling tested method
	instanceRegistry.register(instanceInfo, false);
	// event of proper type is registered
	assertThat(this.testEvents.applicationEvents.size()).isEqualTo(1);
	assertThat(this.testEvents.applicationEvents
			.get(0) instanceof EurekaInstanceRegisteredEvent).isTrue();
	// event details are correct
	final EurekaInstanceRegisteredEvent registeredEvent = (EurekaInstanceRegisteredEvent) (this.testEvents.applicationEvents
			.get(0));
	assertThat(registeredEvent.getInstanceInfo()).isEqualTo(instanceInfo);
	assertThat(registeredEvent.getLeaseDuration())
			.isEqualTo(leaseInfo.getDurationInSecs());
	assertThat(registeredEvent.getSource()).isEqualTo(instanceRegistry);
	assertThat(registeredEvent.isReplication()).isFalse();
}
 
Example #4
Source File: EurekaInstanceListener.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
/**
 * 监听eureka实例注册事件,并发送信息给灰度服务器
 *
 * @param event eureka 实例注册事件
 */
@EventListener
public void listenRegistered(EurekaInstanceRegisteredEvent event) {
    com.netflix.appinfo.InstanceInfo instanceInfo = event.getInstanceInfo();
    InstanceStatus instanceStatus = EurekaInstatnceTransformer.toGrayInstanceStatus(instanceInfo.getStatus());
    sendNotice(instanceInfo, instanceStatus, "REGISTERED");
}
 
Example #5
Source File: EurekaInstanceRegisteredListenerTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void getServiceId() {
    MetadataTranslationService metadataTranslationService = Mockito.mock(MetadataTranslationService.class);
    MetadataDefaultsService metadataDefaultsService = Mockito.mock(MetadataDefaultsService.class);
    GatewayNotifier gatewayNotifier = mock(GatewayNotifier.class);

    EurekaInstanceRegisteredListener eirl = new EurekaInstanceRegisteredListener(metadataTranslationService, metadataDefaultsService, gatewayNotifier);

    doAnswer(
        x -> {
            assertEquals("serviceName", x.getArgument(0));
            return null;
        }
    ).when(metadataDefaultsService).updateMetadata(anyString(), any());

    final Map<String, String> metadata = new HashMap<>();

    InstanceInfo instanceInfo = mock(InstanceInfo.class);
    when(instanceInfo.getInstanceId()).thenReturn("1:serviceName:2");
    EurekaInstanceRegisteredEvent event = mock(EurekaInstanceRegisteredEvent.class);
    when(event.getInstanceInfo()).thenReturn(instanceInfo);

    eirl.listen(event);

    verify(metadataTranslationService, times(1)).translateMetadata("serviceName", metadata);
    verify(metadataDefaultsService, times(1)).updateMetadata("serviceName", metadata);
    verify(gatewayNotifier, times(1)).serviceUpdated("serviceName", "1:serviceName:2");
}
 
Example #6
Source File: EurekaInstanceRegisteredListenerTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private EurekaInstanceRegisteredEvent createEvent(String instanceId) {
    InstanceInfo instanceInfo = mock(InstanceInfo.class);
    when(instanceInfo.getInstanceId()).thenReturn(instanceId);

    EurekaInstanceRegisteredEvent out = mock(EurekaInstanceRegisteredEvent.class);
    when(out.getInstanceInfo()).thenReturn(instanceInfo);

    return out;
}
 
Example #7
Source File: MetadataDefaultsServiceTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    mockRegistry = mock(PeerAwareInstanceRegistry.class);
    doAnswer(x -> {
        EurekaInstanceRegisteredEvent event = mock(EurekaInstanceRegisteredEvent.class);
        when(event.getInstanceInfo()).thenReturn(x.getArgument(0));
        eurekaInstanceRegisteredListener.listen(event);
        return mockRegistry;
    }).when(mockRegistry).register(any(), anyBoolean());
    EurekaServerContext mockEurekaServerContext = mock(EurekaServerContext.class);
    when(mockEurekaServerContext.getRegistry()).thenReturn(mockRegistry);
    EurekaServerContextHolder.initialize(mockEurekaServerContext);
}
 
Example #8
Source File: InstanceRegistry.java    From didi-eureka-server with MIT License 5 votes vote down vote up
private void handleRegistration(InstanceInfo info, int leaseDuration,
		boolean isReplication) {
	log("register " + info.getAppName() + ", vip " + info.getVIPAddress()
			+ ", leaseDuration " + leaseDuration + ", isReplication "
			+ isReplication);
	publishEvent(new EurekaInstanceRegisteredEvent(this, info, leaseDuration,
			isReplication));
}
 
Example #9
Source File: InstanceRegistryTest.java    From didi-eureka-server with MIT License 5 votes vote down vote up
@Test
public void testDefaultLeaseDurationRegisterEvent() throws Exception {
	// creating instance info
	final InstanceInfo instanceInfo = getInstanceInfo(APP_NAME, HOST_NAME, INSTANCE_ID, PORT, null);
	// calling tested method
	instanceRegistry.register(instanceInfo, false);
	// instance info duration is set to default
	final EurekaInstanceRegisteredEvent registeredEvent =
			(EurekaInstanceRegisteredEvent) (this.testEvents.applicationEvents.get(0));
	assertEquals(LeaseInfo.DEFAULT_LEASE_DURATION,
			registeredEvent.getLeaseDuration());
}
 
Example #10
Source File: InstanceRegistry.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
private void handleRegistration(InstanceInfo info, int leaseDuration,
		boolean isReplication) {
	log("register " + info.getAppName() + ", vip " + info.getVIPAddress()
			+ ", leaseDuration " + leaseDuration + ", isReplication "
			+ isReplication);
	publishEvent(new EurekaInstanceRegisteredEvent(this, info, leaseDuration,
			isReplication));
}
 
Example #11
Source File: InstanceRegistryTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultLeaseDurationRegisterEvent() throws Exception {
	// creating instance info
	final InstanceInfo instanceInfo = getInstanceInfo(APP_NAME, HOST_NAME,
			INSTANCE_ID, PORT, null);
	// calling tested method
	instanceRegistry.register(instanceInfo, false);
	// instance info duration is set to default
	final EurekaInstanceRegisteredEvent registeredEvent = (EurekaInstanceRegisteredEvent) (this.testEvents.applicationEvents
			.get(0));
	assertThat(registeredEvent.getLeaseDuration())
			.isEqualTo(LeaseInfo.DEFAULT_LEASE_DURATION);
}
 
Example #12
Source File: GemFrameEurekaListener.java    From gem with MIT License 4 votes vote down vote up
@EventListener //服务注册事件
public void listen(EurekaInstanceRegisteredEvent event) {
    InstanceInfo instanceInfo = event.getInstanceInfo();
    System.out.println("服务注册事件:"+instanceInfo.getAppName()+":"+instanceInfo.getIPAddr()+":"+instanceInfo.getPort());
}
 
Example #13
Source File: InstanceRegistryTest.java    From didi-eureka-server with MIT License 4 votes vote down vote up
@EventListener(EurekaInstanceRegisteredEvent.class)
public void onEvent(EurekaInstanceRegisteredEvent event) {
	this.applicationEvents.add(event);
}
 
Example #14
Source File: InstanceRegistryTests.java    From spring-cloud-netflix with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
	return EurekaInstanceRegisteredEvent.class.isAssignableFrom(eventType)
			|| EurekaInstanceCanceledEvent.class.isAssignableFrom(eventType)
			|| EurekaInstanceRenewedEvent.class.isAssignableFrom(eventType);
}