面试题答案
一键面试在Spring Cloud微服务架构中,服务启动阶段确保能正常注册到Eureka Server的关键配置如下:
- 引入依赖:
- 在
pom.xml
文件中引入Eureka客户端依赖。例如:
- 在
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 配置文件:
- 在
application.properties
或application.yml
文件中进行如下配置:- 指定Eureka Server地址:
使用
application.properties
配置:
- 指定Eureka Server地址:
使用
- 在
eureka.client.service-url.defaultZone=http://eureka-server1:8761/eureka/,http://eureka-server2:8761/eureka/
使用application.yml
配置:
eureka:
client:
service-url:
defaultZone: http://eureka-server1:8761/eureka/,http://eureka-server2:8761/eureka/
- **配置服务实例信息**:
application.properties
:
spring.application.name=your - service - name
eureka.instance.hostname=your - hostname
eureka.instance.prefer - ip - address=true
application.yml
:
spring:
application:
name: your - service - name
eureka:
instance:
hostname: your - hostname
prefer - ip - address: true
- 启用Eureka客户端:
- 在Spring Boot主应用类上添加
@EnableEurekaClient
注解,例如:
- 在Spring Boot主应用类上添加
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class YourServiceApplication {
public static void main(String[] args) {
SpringApplication.run(YourServiceApplication.class, args);
}
}