美文网首页
eureka搭建及注册

eureka搭建及注册

作者: 唯有口袋里的钱能给我安全感 | 来源:发表于2018-05-11 09:27 被阅读79次

    1.如何搭建一个eureka server?

    新建工程,导入依赖:

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

    在启动类添加注解:

    @EnableEurekaServer
    

    在src/resource文件夹添加配置文件application.yml,如下配置:

    server:
      port: 8012
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8012/eureka
        register-with-eureka: false
        fetch-registry: false
    #   eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种默认行为
    #   eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务
    

    2.如何将一个微服务注册到eureka server

    打开spring cloud官方文档 搜索"eureka"。

    新建工程,导入依赖:

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

    在启动类添加注解:
    在Spring Boot的入口函数处,通过添加@EnableDiscoveryClient注解来激活Eureka中的DiscoveryClient实现(因为我们在HelloController中注入了DiscoveryClient)。

    @EnableEurekaClient 或者 @EnableDiscoveryClient
    

    在src/resource文件夹配置文件application.yml,添加如下配置(配置服务名和注册中心地址):

    eureka:
      client:
        serviceUrl:
            defaultZone: http://localhost:8012/eureka/
    

    指定Application显示的名称,做如下配置:

    spring:
      application:
        name: microservice-provider-user
    

    修改Eureka Instance ID
    搜索"Changing the Eureka Instance ID"

    eureka:
      instance:
        instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}
        prefer-ip-address: true
    

    3.如何为eureka server添加认证

    为eureka server添加依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    

    在src/resource配置文件application.yml,添加如下配置:

    security:
      user:
        name: echo
        password: echoecho
      basic:
        enabled: true
    

    修改Eureka的配置如下(注意:name和password应该和security配置的一致):

    eureka:
      client:
        service-url:
          defaultZone: http://name:password@localhost:8012/eureka
    

    修改微服务的serviceUrl:(这样微服务才能注册到有认证的eureka server上)

    eureka:
      client:
        serviceUrl:
          defaultZone: http://echo:echoecho@localhost:8012/eureka
    

    4.在生产环境中,需要实时或定期监控服务的可用性。
    Spring Boot的actuator(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。
    要使用actuator(健康监控)功能需要添加依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    

    在src/resource文件夹配置文件application.yml,添加如下配置可以解决“There was an unexpected error (type=Unauthorized, status=401).
    Full authentication is required to access this resource.”的问题:

    management:
      security:
        enabled: false
    

    5.配置健康检查(health check)

    相关文章

      网友评论

          本文标题:eureka搭建及注册

          本文链接:https://www.haomeiwen.com/subject/jjwxdftx.html