- 引入maven依赖,该依赖包含spring boot starter依赖,所以不需要再引入其他依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 添加eureka注册中心启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
3.配置application.yml
server:
port: 8001
spring:
security:
basic:
enabled: true
user:
name: root
password: zxy2019
application:
name: eureka-server
eureka:
instance:
namespace: eureka-server
instace-id: ${spring.cloud.client.ipAddress}:${spring.application.name}
hostname: localhost
prefer-ip-address: false
client:
registerWithEureka: false
fetchRegistry: false
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka
server:
wait-time-in-ms-when-sync-empty: 0
enable-self-preservation: false
eviction-interval-timer-in-ms: 5000
- 为了安全起见,加入spring security starter依赖,以保证不被人随便查看注册中心控制台和随意注册服务到注册中心
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
上面defaultZone属性已经配置过注册中心注册时需要携带用户名密码才可以注册,而且访问eureka控制台也需要这个用户名密码