一.Eureka介紹
博主本着實用爲主的目的,不會對Eureka的原理做過多的解釋,只會告訴大家這是個什麼東西,以及他的用途,原理以後如果有時間研究,就會深入探討。
Spring Cloud 通過爲Eureka 增加了Spring Boot 風格的自動化配置,我們只需通過簡單引入依賴和註解配置就能讓Spring Boot 構建的微服務應用輕鬆地與Eureka 服務治理體系進行整合。
Eureka的三個角色:
- 服務註冊中心:
Eureka 提供的服務端, 提供服務註冊與發現的功能, 一般被稱作eureka-server。
- 服務提供者:
提供服務的應用, 可以是Spring Boot 應用, 也可以是其他技術平臺且遵循Eureka 通信機制的應用。它將自己提供的服務註冊到Eureka, 以供其他應用發現。
- 服務消費者:
消費者應用從服務註冊中心獲取服務列表, 從而使消費者可以知道去何處調用其所需要的服務。
二.Eureka服務搭建
- 依賴包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 配置文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
- 啓動類
@Slf4j
@EnableEurekaServer
@SpringBootApplication(scanBasePackages = {"org.qy.eureka.server"})
public class EurekaApplication
{
public static void main( String[] args )
{
ApplicationContext context = SpringApplication.run(EurekaApplication.class, args);
String serverPort = context.getEnvironment().getProperty("server.port");
log.info("後臺配置管理服務啓動成功! Swagger2: http://127.0.0.1:".concat(serverPort));
}
}
- 界面展示
只需要上面很簡單的配置服務端註冊中心就搭建成功。
如果看書很難理解那就動手吧!實踐出真知!
項目地址:微服務集成項目