pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
EurekaApplication.java
@EnableEurekaServer //启用服务器的配置中心
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
在全局配置文件中,开启基于http basic的安全认证。
application.yml
spring:
application:
name: eureka-server
profiles: ## application-{}.yml
active: $spring.profiles.active$ ## linux
active:dev ## window
application-dev.yml
server:
port: 8001
eureka:
instance:
hostname: eureka1
lease-renewal-interval-in-seconds: 10 ##客户端向服务器(注册中心发送心跳的时间间隔)
lease-expiration-duration-in-seconds: 120 ##服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
client:
service-url:
##defaultZone: http://eureka2:8002/eureka/ ##互相注册到对方--高可用服务发现
defaultZone: http://test:123456@eureka2:8002/eureka/
security: ## 开启基于http basic的安全认证
basic:
enabled: true
user:
name: test
password: 123456
springcloud-eureka-b
pom.文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
EurekaApplication.java
@EnableEurekaServer //启用服务器的配置中心
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
application.yml
spring:
application:
name: eureka-server
profiles: ## application-{}.yml
active: $spring.profiles.active$ ## linux
active:dev ## window
application-dev.yml
server:
port: 8002
eureka:
instance:
hostname: eureka2
lease-renewal-interval-in-seconds: 10 ##客户端向服务器(注册中心发送心跳的时间间隔)
lease-expiration-duration-in-seconds: 120 ##服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
client:
service-url:
## defaultZone: http://eureka1:8001/eureka/
defaultZone: http://test:123456@eureka1:8001/eureka/
security: ## 开启基于http basic的安全认证
basic:
enabled: true
user:
name: test
password: 123456
window 启动 略。。。
linux 启动
可以在Linux终端中,通过java命令来启动Eureka Server。在启动的时候,可以通过启动参数来设置有效的配置环境。具体命令如下:
java -jar -Dspring.profiles.active=eurekaserver1 spring-cloud-eureka-server-cluster-1.0.jar
其中-Dspring.profiles.active启动参数,用于定义本次启动的Eureka Server应用的有效全局配置命名,也就是全局配置文件的后缀。SpringBOOT在启动的时候,会根据启动参数来决定读取的有效全局配置文件是哪一个。
效果图: