創建Eureka服務註冊中心
1. 配置pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
2. 啓動類
啓動類使用@EnableEurekaServer
註解表示這是一個服務端應用
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication7001.class, args);
}
}
3. 配置文件application.yml
這裏配置的兩個Eureka服務註冊中心的模塊。一個的服務端口爲7001,另一個爲7002。兩者通過serviceUrl
分別將各自注冊到對方的服務上去。
server:
port: 7001
spring:
#profiles:
# active: dev
application:
# 顯示的名字爲Application名
name: EurekaServer7001
eureka:
instance:
hostname: EurekaServerInstance7001
# 是否使用Ip註冊
preferIpAddress: true
# ipAddress: localhost:7001
client:
# 是否向服務註冊中心註冊
registerWithEureka: true
# 是否檢索服務
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:7002/eureka/
將應用註冊到Eureka
1. 增加pom中的eureka依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2. 啓動類
啓動類中需要添加@EnableDiscoveryClient
註解,使得Eureka能夠掃到我們的應用並註冊。
3. 配置yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka
4. 啓動
在瀏覽器中輸入http://localhost:7001/便可以看到所有註冊到Eureka上的服務。