springcloud 配置eureka-server与clie
开发工具用的是Intellij idea 2018.3
首先使用spring initializr创建eureka-server

下一步填写好groupId和artifact名称,然后点击下一步,出现下图,注意第三点,这里选择的版本号,要根据spring cloud官网给出的版本映射表选择

进入官网之后一直往下拉,就可以看到映射表

为什么我在上上张图第③处选择的是springboot2.0.7,而这里推荐使用2.0.6release呢?
是因为springcloud官网显示springboot2.0.7对应的spring-cloud是 napshot(快照版本),可能存在不稳定,我们这里使用稳定版
所以对应创建出来的maven pom.xml也要修改下版本,如下图

然后一个springboot项目就初始化完成了,接下来,有两处我们需要修改一下
1. 在springboot的启动类上添加注解@EnableEurekaServer

2. 在application.yml中添加配置

一个简单的eureka-server就搭建好了,浏览器输入http://localhost:8761/就可以访问
接下来我们需要注册一个简单的eureka-client
依旧使用spring initializr创建eureka-client

然后记得创建完成后一定要修改pom.xml和eureka-server版本相同,如下图

然后在启动类上加上@EnableDiscoveryClient
这里引申一下,有的教程说添加的是@EnableEurekaClient,这两个有什么区别呢?
1.@EnableDiscoveryClient注解是基于spring-cloud-commons依赖,并且在classpath中实现;
2.@EnableEurekaClient注解是基于spring-cloud-netflix依赖,只能为eureka作用;如果你的classpath中添加了eureka,则它们的作用是一样的
接下来还需要修改下eureka-client的application.yml,请注意我下图标记出来的地方,在client中要配置eureka-server的地址

好了,一切就绪,启动一下client !
咦?怎么报错了
Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
这简直是一个大坑,错误信息很有误导性,真正原因是因为
client里不包含Tomcat的依赖,所以Spring容器无法创建一些实例,从而导致项目无法启动,只需在pom.xml文件中,加上web依赖即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
重启client之后,再刷新eureka-sever页面,就可以看到client已经注册进来了
