1.搭建2個Eureka服務
eureka7001,eureka7002
1.pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.yml文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服務端的實例名
client:
#false表示不像客戶中心注冊自己
register-with-eureka: false
#false表示自己就是客戶中心,我得 職責就是維護服務實例,並不需要去檢索服務
fetch-registry: false
service-url:
#設置與Eureka Server交互的地址查詢服務和注冊服務都需要依賴這個地址
defaultZone: http://eureka7002.com:7002/eureka/ #集群版
#設置與Eureka Server交互的地址查詢服務和注冊服務都需要依賴這個地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #單機版
#server:
#enable-self-preservation: false #不啟用默認的保護機制, #eviction-interval-timer-in-ms: 2000 #設置默認世間為2秒
7002和7001類似
改hosts文件
eureka7001、eureka7002 映射到 127.0.0.1
3.啟動類
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
4.頁面展示
2.我們再來寫支付服務
1.pom文件
<!--eureka client-->
<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>
<!--監控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.yml文件
server:
port: 8001 #服務端口
spring:
application:
name: cloud-payment-service #服務名
eureka:
client:
#表示是否將自己注冊進EurekaServer默認為true
register-with-eureka: true
#是否從EurekaServer抓取已有的注冊信息,默認為true,單節點無所謂,集群必須設置為true才能配合ribbon使用負載均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
#defaultZone: http://eureka7001.com:7001/eureka #單機版
instance:
instance-id: payment8001
prefer-ip-address: true #點進去左下角會顯示ip
#Eureka 客戶端像服務端發送心跳的時間間隔,單位為秒(默認是30秒)
#lease-renewal-interval-in-seconds: 1
#Eureka 服務端收到最后一次心跳后等待時間上線,單位為秒(默認是90秒),超時將剔除服務
#lease-expiration-duration-in-seconds: 2
3.啟動類
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
3.最終實現效果