搭建基於Spring Cloud的微服務註冊中心_spring

來源:互聯網
上載者:User
簡單幾步,搭建基於Spring Cloud的微服務註冊中心,包括幾個特性:
- 提供註冊中心由服務提供者進行註冊
- 服務消費者在註冊中心探索服務所在的位置
- 高可用(叢集)配置

1、引入依賴:
需要引入兩個依賴:
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>      .....</dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

2、配置:
為了配置高可用的註冊中心叢集,我們配置了3個註冊中心互相備用。假設所在的主機分別為:
- 192.168.0.9
- 192.168.0.10
- 192.168.0.18

連接埠為58000。
spring:  profiles:    active: dev  application:    name: eureka-service    server:  port: 58000management:  port: 59000eureka:  client:    #register-with-eureka: false    fetch-registry: false  #instance:  #  prefer-ip-address: true        ---# 開發環境配置spring:  profiles: dev---# 開發服0配置spring:  profiles: dev0eureka:  instance:    hostname: 192.168.0.9  client:    service-url:      defaultZone: http://192.168.0.10:58000/eureka/,http://192.168.0.18:58000/eureka/---# 開發服1配置spring:  profiles: dev1eureka:  instance:    hostname: 192.168.0.10  client:    service-url:      defaultZone: http://192.168.0.9:58000/eureka/,http://192.168.0.18:58000/eureka/---# 開發服2配置spring:  profiles: dev2eureka:  instance:    hostname: 192.168.0.18  client:    service-url:      defaultZone: http://192.168.0.10:58000/eureka/,http://192.168.0.9:58000/eureka/

3、代碼:
只需要一個主類就行了:
@EnableEurekaServer@SpringBootApplicationpublic class RegistryApplication {public static void main(String[] args) {SpringApplication.run(RegistryApplication.class, args);}}

4、部署和檢查結果:
把三個服務中心分別部署到三個伺服器中,然後開啟管理頁面:
http://192.168.0.9:58000/

可以看到是否已經正常啟動,各個備用(replicas)註冊中心是否available。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.