1. 程式人生 > >springcloud服務註冊中心+Eureka概念和使用場景

springcloud服務註冊中心+Eureka概念和使用場景

測試中使用到的程式碼到在這裡https://download.csdn.net/download/zhou920786312/10853300

Eureka概念和使用場景

可以看這邊文章https://blog.csdn.net/xiao__jia__jia/article/details/81141556

 

 

Eureka伺服器

將微服務註冊到服務中心去

多個客戶端配置就可以做到叢集

@SpringBootApplication
@EnableEurekaServer // EurekaServer伺服器端啟動類,接受其它微服務註冊進來
public class EurekaServer7001_App
{
    public static void main(String[] args)
    {
        SpringApplication.run(EurekaServer7001_App.class, args);
    }
}
 

server: 
  port: 7001
 
eureka: 
  instance:
    hostname: eureka7001.com #eureka服務端的例項名稱,對應host檔案的127.0.0.1
  client: 
    register-with-eureka: false     #false表示不向註冊中心註冊自己。
    fetch-registry: false     #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務
    service-url: 
      #單機 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設定與Eureka Server互動的地址查詢服務和註冊服務都需要依賴這個地址(單機)。
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

 

<dependencies>
        <!--eureka-server服務端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- 修改後立即生效,熱部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

Eureka客戶端

eureka:
  client: #客戶端註冊進eureka服務列表內
    service-url: 
         #eureka伺服器地址
        defaultZone: http://localhost:7001/eureka
#       defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/      
  instance:
  #設定在eureka伺服器satus中顯示的名稱
    instance-id: microservicecloud-dept8001
    prefer-ip-address: true     #訪問路徑可以顯示IP地址     
 
info: #eureka 中微服務的介紹
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

 

 

@SpringBootApplication
@EnableEurekaClient //本服務啟動後會自動註冊進eureka服務中,相當於這個微服務是eureka的客戶端
@EnableDiscoveryClient //發現在eureka中的微服務資訊
public class DeptProvider8001_App
{
    public static void main(String[] args)
    {
        SpringApplication.run(DeptProvider8001_App.class, args);
    }
}