# 快速搭建微服务-注册中心、服务发现

注册中心是微服务的核心组件,SpringCloud比较常见的注册中心有eureka和consul,这里简单说明下这两种注册中心服务如何实现。这里的例子是基于SpringCloud Edgeware版本。

# Spring Cloud Eureka注册中心

# Server端

  • maven

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
    1
    2
    3
    4
  • application.yml

    server:
      port: 9101
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
      server:
        wait-time-in-ms-when-sync-empty: 0
    spring:
      application:
        name: eureka-server
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
  • Application.java

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7

# Client端

  • maven

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
    1
    2
    3
    4
  • application.yml

    server:
      port: 9110
    eureka:
      client:
        service-url:
          defaultZone: http://eureka-server:9101/eureka
      instance:
        prefer-ip-address: true
        instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    spring:
      application:
        name: service-order
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
  • Application.java

    @EnableDiscoveryClient
    @SpringBootApplication
    public class ServiceOrderApplication {
        public static void main(String[] args) {
            SpringApplication.run(ServiceOrderApplication.class, args);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7

# Spring Cloud Consul注册中心

# Server端

  1. 下载consul服务端程序

  2. 运行consul

    • 开发环境
    consul agent -dev
    
    1
    • 正式部署
    consul agent -data-dir=tem/consul
    
    1

# Client端

  • maven

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    
    1
    2
    3
    4
  • application.yml

    server:
      port: 9110
    spring:
      application:
        name: service-order
      cloud: 
        consul:
          host: localhost
          port: 8500
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
  • Application.java

    @EnableDiscoveryClient
    @SpringBootApplication
    public class ServiceOrderApplication {
        public static void main(String[] args) {
            SpringApplication.run(ServiceOrderApplication.class, args);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7