Eureka 服務端需要引入spring-cloud-starter-netflix-eureka-server
需要註冊進eureka的所有服務(客戶端)需要引入spring-cloud-starter-netflix-eureka-client
單機版
Eureka server服務端配置
Application.yml
server:
port: 7001
eureka: #官方文檔拷貝來的
instance:
hostname: localhost
client:
registerWithEureka: false #不向註冊中心註冊自己
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:7002/eureka/
Main方法1)
@SpringBootApplication
@EnableEurekaServer
//要加上這個
public class Application {
public static void main(String[]
args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
Eureka client客戶端配置
Application.yml
eureka:
instance:
hostname: localhost
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Main方法
@SpringBootApplication
@EnableEurekaClient//需要這個註解
public class Main80 {
public static void main(String[] args) {
SpringApplication.run(Main80.class, args);
}
}
集羣版eueka配置
server:
port: 7001
eureka: #官方文檔拷貝來的
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:7002/eureka/
server:
port: 7002
eureka: #官方文檔拷貝來的
instance:
hostname: localhost
client:
registerWithEureka: false #不向註冊中心註冊自己
fetchRegistry: false #false表示自己端就是註冊中心 職責就是維護實例 不需要去檢索服務
serviceUrl:
defaultZone: http://localhost:7001/eureka/
然後服務往進來註冊的時候倆都要註冊
```bash
server: port: 80
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001,http://localhost:8002