Y9ConsulAutoServiceRegistrationListener
This commit is contained in:
parent
c09fefd44e
commit
c5d8b00e22
|
@ -1,30 +1,23 @@
|
||||||
package net.risesoft.demo;
|
package net.risesoft.demo;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
|
|
||||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import net.risesoft.demo.entity.User;
|
|
||||||
import net.risesoft.demo.repository.UserRepository;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class OnApplicationReady implements ApplicationListener<ApplicationReadyEvent> {
|
public class OnApplicationReady implements ApplicationListener<ApplicationReadyEvent> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationReadyEvent event) {
|
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||||
//System.out.println(event.getApplicationContext().getEnvironment().getProperty("path"));
|
/*
|
||||||
|
* String[] ss =
|
||||||
String[] ss = event.getApplicationContext().getBeanNamesForType(ConnectionDetails.class);
|
* event.getApplicationContext().getBeanNamesForType(ConnectionDetails.class);
|
||||||
for (String s : ss) {
|
* for (String s : ss) { System.out.println(s); }
|
||||||
System.out.println(s);
|
*
|
||||||
}
|
* UserRepository userRepository =
|
||||||
|
* event.getApplicationContext().getBean(UserRepository.class); User user = new
|
||||||
UserRepository userRepository = event.getApplicationContext().getBean(UserRepository.class);
|
* User(); user.setName("张三"); user.setAge(20); userRepository.save(user);
|
||||||
User user = new User();
|
*/
|
||||||
user.setName("张三");
|
|
||||||
user.setAge(20);
|
|
||||||
userRepository.save(user);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package net.risesoft.demo.config;
|
package net.risesoft.demo.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.Y9ConsulAutoServiceRegistrationListener;
|
||||||
|
import org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistrationListener;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@ -10,5 +13,11 @@ public class Y9Config {
|
||||||
public Y9Context y9Context() {
|
public Y9Context y9Context() {
|
||||||
return new Y9Context();
|
return new Y9Context();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean(ConsulAutoServiceRegistrationListener.class)
|
||||||
|
public Y9ConsulAutoServiceRegistrationListener y9ConsulAutoServiceRegistrationListener() {
|
||||||
|
return new Y9ConsulAutoServiceRegistrationListener();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package org.springframework.cloud.client.serviceregistry;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
|
||||||
|
import org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistration;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
|
||||||
|
public class Y9ConsulAutoServiceRegistrationListener implements ApplicationListener<ApplicationReadyEvent> {
|
||||||
|
@Autowired
|
||||||
|
private ConsulAutoServiceRegistration consulAutoServiceRegistration;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConsulDiscoveryProperties consulProperties;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ServerProperties serverProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||||
|
// 参考 ConsulAutoServiceRegistrationListener
|
||||||
|
consulAutoServiceRegistration.getPort().compareAndSet(0, serverProperties.getPort());
|
||||||
|
consulAutoServiceRegistration.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ management:
|
||||||
exposure:
|
exposure:
|
||||||
include: '*'
|
include: '*'
|
||||||
server:
|
server:
|
||||||
port: 8081
|
port: 9000
|
||||||
servlet:
|
servlet:
|
||||||
context-path: /demo01
|
context-path: /demo01
|
||||||
encoding:
|
encoding:
|
||||||
|
@ -35,6 +35,8 @@ spring:
|
||||||
consul:
|
consul:
|
||||||
host: ${CONSUL_HOST:localhost}
|
host: ${CONSUL_HOST:localhost}
|
||||||
port: ${CONSUL_PORT:8500}
|
port: ${CONSUL_PORT:8500}
|
||||||
|
config:
|
||||||
|
enabled: false
|
||||||
discovery:
|
discovery:
|
||||||
register-health-check: true
|
register-health-check: true
|
||||||
healthCheckPath: ${server.servlet.context-path}/actuator/health
|
healthCheckPath: ${server.servlet.context-path}/actuator/health
|
||||||
|
|
Loading…
Reference in New Issue