https://blog.csdn.net/ThinkWon/article/details/103726655
Spring Cloud Netflix Eureka是Spring Cloud Netflix子项目的核心组件之一,主要用于微服务架构中的服务治理
先疑惑了一下此Netflix是不是彼Netflix
查了一下 Netflix是什么,与Spring Cloud有什么关系 还真是
爱了爱了
搭建Eureka注册中心
创建eureka-server模块
@EnableEurekaServer启用Euerka注册中心功能
application.yml添加配置
项目默认是application.properties,为了和教程保持一致
删除application.properties,执行 mvn -X clean install
Run Dashboard运行应用
浏览器打开注册中心
成功
依赖下了挺久,看眼都有些什么
😱😱😱
上面是spring-cloud-starter-netflix-eureka-server
下面是spring-boot-starter-test
DEBUG:没有Run Dashboard 尝试1
https://blog.csdn.net/chinoukin/article/details/80577890
改.idea/workspace.xml的
原本是这样
1 2 3 4 5 <component name ="RunDashboard" > <option name ="configurationTypes" > <option value ="SpringBootApplicationConfigurationType" /> </option > </component >
但是这个文件每次改了command+s就又回去了
尝试2
https://blog.csdn.net/feixiangsmile/article/details/96908744
Edit Configurations中
Templates中
点击加号添加Application和Spring Boot
workspace.xml变成了
1 2 3 4 5 6 7 8 <component name ="RunDashboard" > <option name ="configurationTypes" > <set > <option value ="Application" /> <option value ="SpringBootApplicationConfigurationType" /> </set > </option > </component >
但还是没有
尝试3
https://www.cnblogs.com/JealousGirl/p/openorcloserundash.html
https://blog.csdn.net/icebox126/article/details/78861943
在workspace.xml中添加(这次可以加了)
1 <property name ="show.run.dashboard.notification" value ="true" />
但是重启还是不会弹出提示
尝试4
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000428264-New-Spring-Boot-Run-dashboard-not-showing-up
https://blog.csdn.net/jianxia801/article/details/99576764
…
fine
原来一直都在啊
搭建Eureka客户端
创建eureka-client模块
添加依赖
加@EnableDiscoveryClient表明是一个Eureka客户端
配置
DEBUG:注册中心中没有显示eureka-client 尝试1
原来的pom.xml中的依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-client</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > <exclusions > <exclusion > <groupId > org.junit.vintage</groupId > <artifactId > junit-vintage-engine</artifactId > </exclusion > </exclusions > </dependency >
教程中说要添加的依赖
1 2 3 4 5 6 7 8 9 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-client</artifactId > </dependency >
一开始由于spring-cloud-starter-netflix-eureka-client已经有了
直接添加了上面这个spring-boot-starter-web
但是觉得好像有点问题?
spring-boot-starter-test和spring-boot-starter-web会不会冲突呢?
没冲突
但是spring-boot-starter-test似乎多了很多东西(试了一下并不能删)
尝试2
看下报错
1 2 3 2020-04-09 10:43:38.777 ERROR 1243 --- [ main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8001/eureka/} com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
好像干了个蠢事
果然 服务器没开
重来 先开eureka-server 再开eureka-client
注册成功
搭建Eureka注册中心集群 目的:防止注册中心挂了整个垮掉 搭建双节点的注册中心集群
2个配置文件
(不知道原本的application.yml要不要删?看了下源码没有删)
本机配置域名
macOS在/etc/hosts
改之前的存个档
1 2 3 4 5 6 7 8 9 10 11 ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 xmind.net 127.0.0.1 www.xmind.net
加两行
1 2 127.0.0.1 replica1 127.0.0.1 replica2
启动
修改配置,让eureka-client连接到集群
1 2 3 4 # 配置注册中心地址 # defaultZone: http://localhost:8001/eureka # 同时注册到两个注册中心 defaultZone: http://replica1:8002/eureka/,http://replica2:8003/eureka/
DEBUG:服务器没有注册 复制一份原启动配置,修改为从replica1启动
Run
发现有备份,但是没有注册?
似乎是只复制了一份配置,replica2没有启动
加上
好了
给Eureka注册中心添加认证
创建eureka-security-server模块
添加依赖
配置pom.xml
添加WebSecurityConfig
eureka-client注册到有登录认证的注册中心
以application-security.yml配置运行eureka-client
DEBUG:SpringSecurity模块 要添加SpringSecurity模块,也许是Spring Cloud Security下的Cloud Security?先试试
创建出的pom.xml中依赖是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-security</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > <exclusions > <exclusion > <groupId > org.junit.vintage</groupId > <artifactId > junit-vintage-engine</artifactId > </exclusion > </exclusions > </dependency >
教程中要加的依赖是
1 2 3 4 5 6 7 8 9 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-server</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-security</artifactId > </dependency >
好像不是这个
重新创建
这次直接搜索Spring Security 发现有这个模块
(最后发现都行,且没必要创建时添加,随时在pom.xml中修改都可)
DEBUG:新的模块没有被识别为Maven项目
https://www.jianshu.com/p/909eb8a48d49
右边Maven栏+对应的pom.xml
DEBUG:启动报错&Whitelabel Error Page
https://blog.csdn.net/qq_28379809/article/details/102789716
添加依赖
1 2 3 4 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency >
启动
输入账号密码
发现EurekaSecurityServerApplication没有加@EnableEurekaServer
pom.xml中有spring-cloud-starter-netflix-eureka-server,为什么导不进来呢
和eureka-server的配置比较,发现少一句
1 <spring-cloud.version > Hoxton.SR3</spring-cloud.version >
加上
还是不行
源码中是
1 <spring-cloud.version > Hoxton.RELEASE</spring-cloud.version >
现在的依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-server</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-security</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > <exclusions > <exclusion > <groupId > org.junit.vintage</groupId > <artifactId > junit-vintage-engine</artifactId > </exclusion > </exclusions > </dependency > <dependency > <groupId > org.springframework.security</groupId > <artifactId > spring-security-test</artifactId > <scope > test</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-netflix-eureka-server</artifactId > <version > 2.2.2.RELEASE</version > <scope > compile</scope > </dependency > </dependencies >
源码中的依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-server</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-security</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > <exclusions > <exclusion > <groupId > org.junit.vintage</groupId > <artifactId > junit-vintage-engine</artifactId > </exclusion > </exclusions > </dependency > </dependencies >
复制过去
Reimport Maven Projects
发现正在下载的进度条
所以难道刚才是因为依赖还没下载完吗
下载完成
全红了…
改回原本的依赖 删掉spring-boot-starter-web 不红了
启动
🙃
仔细看下源码的配置是
1 <artifactId > spring-cloud-starter-security</artifactId >
而不是
1 <artifactId > spring-boot-starter-security</artifactId >
所以难道还是应该引入Spring Cloud Security模块?
重新创建试试
成功!
DEBUG:以application-security.yml配置运行eureka-client 现在eureka-client有2个yml
直接运行还是application.yml的8101端口
根据刚才eureka-server的多个yml
是修改了指定主机地址
1 2 3 4 eureka: instance: hostname: localhost
1 2 3 4 eureka: instance: hostname: replica1
1 2 3 4 eureka: instance: hostname: replica2
然后在Edit Configurations中复制Configuration,然后修改主机名
于是这里也加上指定主机名
1 2 instance: hostname: eureka-client-security
复制一个EurekaClientApplication的配置,Active profiles处改为主机名
但还是8101?
观察发现
Server的配置文件1叫application-replica1.yml,hostname是replica1,Active profiles填的replica1
配置文件2叫application-replica2.yml,hostname是replica2,Active profiles填的replica2
难道…
把Client的application-security.yml中改成
1 2 instance: hostname: security
Active profiles改成security
Run
打开了!
有个EMERGENCY!
保护机制后面再看
先试试这个指定yml到底是怎么指定的
文件名
hostname
Active profiles
能否启动
application-security.yml
eureka-client-security
eureka-client-security
否
application-security.yml
security
security
可
application-security.yml
securityX
security
可
application-securityX.yml
security
security
否
看来Active profiles要填文件名后面的部分
教程最下面的常用配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8001/eureka/ enabled: true registry-fetch-interval-seconds: 30 instance: lease-renewal-interval-in-seconds: 30 lease-expiration-duration-in-seconds: 90 metadata-map: zone: guangdong hostname: localhost prefer-ip-address: false server: enable-self-preservation: false
于是在eureka-server的yml中加上
1 2 server: enable-self-preservation: true
就没有EMERGENCY! 了
关于eureka-security-server的依赖 发现教程里写的依赖是
1 2 3 4 5 6 7 8 9 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-server</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-security</artifactId > </dependency >
源码里用的依赖是
1 2 3 4 5 6 7 8 9 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-server</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-security</artifactId > </dependency >
自己刚才启动成功的是spring-cloud-starter-security
改成spring-boot-starter-security试了一下也成功了
问题解决总结
用Spring Cloud Security模块,不是Spring Security模块
EurekaSecurityServerApplication要记得加@EnableEurekaServer
用指定的yml(application-XXX.yml)启动Spring Boot,在Edit Configurations中复制一个原Configuration,Active profiles填指定yml文件后半部分(XXX),运行这个Configuration
总结