springcloud-EurekaServer模块

看过了eureka的架构图了,那得有一个注册中心吧,那这个角色呢由eurekaServer来负责了,我也得为他分配一个端口吧,即创建一个单独的子模块,毕竟人家也算是一个服务,只不过他可以管理服务间的关系,跟你班主任管理你们差不多。

  下面开始典型的6个步骤:

    1.创建模块

    2.引入依赖到pom.xml

    <dependencies>
        <!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>cn.aib.springcloud</groupId>
            <artifactId>springcloud-api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 一般通用配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

  重点关注是eureka提供的server端的依赖

    3.配置

server:
  port: 7001
eureka:
  instance:
    hostname: localhost   #eureka服务端的实例名称;这是eureka服务端的实例名称,用于区分不同的eureka实例,和服务之间的服务名有点类似
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己;这个属性表示是否向注册中心注册自己,true代表注册,反之。由于eurekaServer就是注册中心了,就不注册自己
    fetch-registry: false   #false表示自己端就是注册中心;这个表示是否捕获注册中心的信心,这个对于注册中心来说false即可,因为一般是eurekaclient来捕获注册信息
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/   #单机  这个是eurekaServer的服务URL
  #      defaultZone: http://eureka7002.com:7002/eureka/  #集群   如果是集群的情况,那这里列举的是其他eurekaServer的地址,用于注册到自己的eurekaServer中。
#  server:
#    # 关闭自我保护机制,保证不可用服务被及时剔除
#    enable-self-preservation: false
#    eviction-interval-timer-in-ms: 2000
spring:
  application:
    name: cloud-eureka-service

    4.主启动

@SpringBootApplication
@EnableEurekaServer  //一定得加这个注解,不然该服务就不是eurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

    5.写业务(本次不用,他负责管理服务,不需要写业务)

    6.测试。访问如下地址:

http://localhost:7001/
原文地址:https://www.cnblogs.com/ibcdwx/p/14224285.html