IDEA2022.3.1+JAVA19+springBoot3.0.2环境注册中心简化了?

来源:3-2 项目优化

慕沐7075796

2023-01-26

1.将system注册到eureka

视频要求:
1.在pom.xml中添加

org.springframework.cloud
spring-cloud-starter-netflix-eureka-client

                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
  2。在application.properties中添加:
  eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
  3.启动类中添加注解:@EnableEurekaClient
  但实际操作中只需添加1、2即可注册,添加3会报错。

pom.xml内容为:
<?xml version="1.0" encoding="UTF-8"?>

4.0.0

com.lantf
demo
0.0.1-SNAPSHOT

<artifactId>system</artifactId>

<properties>
    <maven.compiler.source>19</maven.compiler.source>
    <maven.compiler.target>19</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

application.properties文件的内容为:
spring.application.name=system
server.port=9001

SystemApplication.java的内容为:
package com.lantf.system;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;

@SpringBootApplication

public class SystemApplication {

private static final Logger LOG =LoggerFactory.getLogger(SystemApplication.class);

public static void main(String[] args) {


	SpringApplication app=new SpringApplication(SystemApplication.class);
	ConfigurableEnvironment env;
	env = app.run(args).getEnvironment();

	LOG.info("启动成功!!");
	LOG.info("System地址:\thttp://127.0.0.1:{}",env.getProperty("server.port"));


}

}
正常注册,在注册中心可以看到system记录。
图片描述

记录一下,不知是否正确。

写回答

3回答

甲蛙

2023-01-30

最新版本还没用过,有些属性是有默认值的,eureka按约定的8761来启动可能没问题,如果换个端口就有问题了。所以这也是约定优于配置的一种体现

0
0

慕沐7075796

提问者

2023-01-26

application.properties中的这行注释掉还是可以正常注册:  eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

0
0

慕沐7075796

提问者

2023-01-26

把第二点:2.在application.properties中添加: 

  eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

这行注释掉,还是可以正常注册。

0
0

Spring Cloud+ Vue前后端分离开发企业级在线视频系统

全网稀缺课程 市场热门项目+主流框架 一课掌握前后端技术

1758 学习 · 1719 问题

查看课程