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 //啟用服務(wù)器的配置中心
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
在全局配置文件中,開(kāi)啟基于http basic的安全認(rèn)證皱埠。
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 ##客戶端向服務(wù)器(注冊(cè)中心發(fā)送心跳的時(shí)間間隔)
lease-expiration-duration-in-seconds: 120 ##服務(wù)器(注冊(cè)中心)租期到期的時(shí)間, 也就是說(shuō)服務(wù)器在收到最后一次心跳的時(shí)間上線
client:
service-url:
##defaultZone: http://eureka2:8002/eureka/ ##互相注冊(cè)到對(duì)方--高可用服務(wù)發(fā)現(xiàn)
defaultZone: http://test:123456@eureka2:8002/eureka/
security: ## 開(kāi)啟基于http basic的安全認(rèn)證
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 //啟用服務(wù)器的配置中心
@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 ##客戶端向服務(wù)器(注冊(cè)中心發(fā)送心跳的時(shí)間間隔)
lease-expiration-duration-in-seconds: 120 ##服務(wù)器(注冊(cè)中心)租期到期的時(shí)間, 也就是說(shuō)服務(wù)器在收到最后一次心跳的時(shí)間上線
client:
service-url:
## defaultZone: http://eureka1:8001/eureka/
defaultZone: http://test:123456@eureka1:8001/eureka/
security: ## 開(kāi)啟基于http basic的安全認(rèn)證
basic:
enabled: true
user:
name: test
password: 123456
window 啟動(dòng) 略。自娩。晤硕。
linux 啟動(dòng)
可以在Linux終端中挣柬,通過(guò)java命令來(lái)啟動(dòng)Eureka Server惕虑。在啟動(dòng)的時(shí)候坟冲,可以通過(guò)啟動(dòng)參數(shù)來(lái)設(shè)置有效的配置環(huán)境磨镶。具體命令如下:
java -jar -Dspring.profiles.active=eurekaserver1 spring-cloud-eureka-server-cluster-1.0.jar
其中-Dspring.profiles.active啟動(dòng)參數(shù),用于定義本次啟動(dòng)的Eureka Server應(yīng)用的有效全局配置命名健提,也就是全局配置文件的后綴琳猫。SpringBOOT在啟動(dòng)的時(shí)候,會(huì)根據(jù)啟動(dòng)參數(shù)來(lái)決定讀取的有效全局配置文件是哪一個(gè)私痹。
效果圖: