(三)spring cloud 註冊中心

eureka服務端

依賴:

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

 

yml配置:


 
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eurka-server

 啓動類:


@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run( EurekaApplication.class, args );
    }
}

客戶端:

依賴

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

yml

server:
  port: 7001

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/

spring:
  application.name: eureka-client

啓動類添加註解:

@EnableEurekaClient
@EnableDiscoveryClient

spring cloud中discovery service有許多種實現(eureka、consul、zookeeper等等),@EnableDiscoveryClient基於spring-cloud-commons, @EnableEurekaClient基於spring-cloud-netflix。

其實用更簡單的話來說,就是如果選用的註冊中心是eureka,那麼就推薦@EnableEurekaClient,如果是其他的註冊中心,那麼推薦使用@EnableDiscoveryClient。

 

 

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