注册远程nacos

最后发布时间:2024-08-13 11:58:38 浏览量:
@Configuration
@ConditionalOnProperty(prefix = "nacos", name = "enabled", havingValue = "true", matchIfMissing = false)
public class NacosConfig implements SmartInitializingSingleton, ApplicationListener<ContextClosedEvent> {
    @Value("${spring.application.name}")
    String name;

    @Value("${nacos.server-addr}")
    String serverAddr;

    @Value("${nacos.ip}")
    String ip;

    @Value("${nacos.port}")
    Integer port;


    private NamingService namingService;
    @Override
    public void afterSingletonsInstantiated() {

        try {
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR,serverAddr);
            namingService= NacosFactory.createNamingService(properties);
            Instance instance = new Instance();
            instance.setIp(ip);
            instance.setPort(port);
            namingService.registerInstance(name,"DEV_GROUP",instance);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
//        registry.deregister(registration);
        if(namingService!=null){
            try {
                namingService.shutDown();
            } catch (NacosException e) {
                throw new RuntimeException(e);
            }
        }

    }

}
nacos:
  server-addr: 192.168.10.176:28848
  ip: 192.168.10.176
  port: 30024
  enabled: true

这里实际服务在192.168.10.177

如果ip 端口配置正确,nginx没监听30024的端口

生信小木屋

配置nginx监听

    server {
        listen 30024;
        location / {
            proxy_pass http://192.168.10.177:30000/service-survey/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
快捷入口
java 思维导图 浏览PDF 下载PDF
分享到:
标签