lyhzzz преди 3 години
родител
ревизия
8ea4f2aae7
променени са 1 файла, в които са добавени 26 реда и са изтрити 0 реда
  1. 26 0
      platform-api/src/main/java/com/platform/config/NacosConfig.java

+ 26 - 0
platform-api/src/main/java/com/platform/config/NacosConfig.java

@@ -0,0 +1,26 @@
+package com.platform.config;
+
+import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
+import com.alibaba.cloud.nacos.registry.NacosRegistration;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class NacosConfig implements ApplicationListener<ApplicationReadyEvent> {
+
+    @Autowired
+    private NacosRegistration registration;
+
+    @Autowired
+    private NacosAutoServiceRegistration nacosAutoServiceRegistration;
+
+    @Override
+    public void onApplicationEvent(ApplicationReadyEvent event) {
+        String property = event.getApplicationContext().getEnvironment().getProperty("server.port");
+        registration.setPort(Integer.parseInt(property));
+        nacosAutoServiceRegistration.setPort(Integer.parseInt(property));
+        nacosAutoServiceRegistration.start();
+    }
+}