springcloud學習-03 Eureka【周陽springcloud2020學習筆記】

單機版註冊中心

1.創建module

2.pom引入依賴:注意是server!

    <!--eureka-server-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

3.yml配置

    server:
      port: 7001

    eureka:
      instance:
        hostname: localhost #eureka服務端的實例名字
      client:
        allow-redirects: false # 是否向註冊中心註冊本服務:true爲是。註冊中心不需要將自己註冊進去
        fetch-registry: false # false表示自己是註冊中心,職責是維護服務實例,並不需要去檢索服務
        service-url:
          #設置與eureka server交互的地址查詢服務和註冊服務都需要依賴這個地址,多個時用逗號隔開
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4.主啓動類:

@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer //服務端:註冊中心
public class EurekaApplication7001 {
	public static void main(String[] args) {
		SpringApplication.run(EurekaApplication7001.class,args);
	}
}

5.啓動測試啦

業務模塊註冊到eureka中來

1.8001提供者註冊到eureka
1)pom引入依賴:注意是client!

      <!--eureka-client-->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>

2)yml添加eureka配置

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

3)主啓動類添加註解:@EnableEurekaClient

2.80消費者註冊到eureka 同上
yml記得加上服務名稱

springcloud學習系列目錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章