springcloud-02-eureka

在dubbo项目中(http://www.cnblogs.com/wenbronk/p/6774539.html), 我们使用了zookeeper作为集群的注册中心, 在springcloud中, 也可以使用zookeeper, 但好的方案是eureka

关于eureka的原理, 看到过几个不错的的博客

http://itmuch.com/spring-cloud-1/
http://baike.renwuyi.com/2016-12/18938.html
http://blog.csdn.net/jenny8080/article/details/52448403
http://blog.csdn.net/zipo/article/details/60588647

接下来新建一个名为 eureka-discovery 的 module

安装: 

1, pom.xml

        <!--springcloude-eureka服务器端 依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

2, mainClass

package com.wenbronk.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Created by wenbronk on 2017/5/17.
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}

3, application.yml

server:
  port: 8761
#  context-path: /eureka
eureka:
  client:
   # 不作为client使用
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/

eureka启动的时候, 会默认向自己中注册一份自己的应用, 这里我们使用的是单机的, 所以禁止掉就可以了

4, log4j2.yml

自己配置...

这样, 启动应用就可以有一个eureka的注册中心了

通过网页访问: 

 密码登录配置:

通过springcloud的官方文档, 可以看到springcloud可以 通过 url的形式进行密码登录::

pom.xml中添加: 

        <!-- 添加此依赖才可设置密码 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

然后将 application.yml改成下面这种: 

security:
  basic:
    enabled: true
  user:
    name: wenbronk
    password: abc
server:
  port: 8761
#  context-path: /eureka
eureka:
  client:
   # 不作为client使用
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://wenbronk:abc@localhost:8761/eureka/

此时通过浏览器访问, 需要输入用户名和密码才可以登陆

使用eureka常见的问题: 

1, eureka environment的配置:  

eureka.environment: 字符串

2, eureka dataCenter配置, 这样eureka将知道实在aws云上

eureka.datacenter: cloud

3, 关闭自我保护模式, 开发时关闭应用后不踢掉服务

server端: 

eureka.server.enable-self-preservation  设为false
eureka.server.eviction-interval-timer-in-ms 清理时间间隔, 毫秒, 默认 60s

client 端:

eureka.client.healthcheck.enabled=ture   开启健康检查(需要actuator依赖)
eureka.instance.lease-rennewal-interval-in-seconds=10 (租赁更新时间)
eureka.instance.lease-expiration-duration-in-seconds=30 (租赁到期时间)
原文地址:https://www.cnblogs.com/wenbronk/p/6881481.html