Fork me on GitHub

SpringCloud———Eureka依赖问题

异常问题

最近在学习SpringCloud时,添加eureka依赖时,一直无法成功导入,@EnableEurekaServer一直导不进去,经过百度最终找到原因是springboot与springcloud的支持版本不一致,写篇博客记录下来。修改pom.xml文件如下。

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
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>micro</artifactId>
<groupId>net.zzqd</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--SpringCloud最新稳定版本-->
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>

<artifactId>eurekaa</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<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>
</dependencies>
<!--SpringCloud依赖版本管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>

SpringBoot与SpringCloud版本对应如下

spring-boot-starter-parent spring-cloud-dependencies
版本号 发布日期 版本号 发布日期
1.5.2.RELEASE 2017年3月 稳定版 Dalston.RC1 2017年未知月
1.5.9.RELEASE 2017年11月 稳定版 Edgware.RELEASE 稳定版
1.5.16.RELEASE
1.5.20.RELEASE Edgware.SR5
2.0.2.RELEASE 2018年5月 Finchley.BUILD-SNAPSHOT 2018年未知月
2.0.6.RELEASE Finchley.SR2
2.1.4.RELEASE GreenWith.SR1
2.1.7.RELEASE Aug, 2019 Greenwich.RELEASE Jan 23,2019
搭建Eureka服务注册中心

添加以上依赖

入口类

1
2
3
4
5
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp
{
}

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server:
port: 10000

eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:10000/eureka

#security:
# basic:
# enabled: true #开启 方法已过时
http:
authorizeRequests():
antMatchers("/**"):
permitAll();:

spring:
security:
user:
name: admin
password: admin
  • spring.application.name:服务名称
  • server.port:服务端口号
  • eureka.client.service-url.defaultZone:Eureka默认的服务地址空间信息配置
  • eureka.client.fetch-registry:是否从其他Eureka注册中心同步服务列表(单节点无需配置启用).
  • eureka.client.register-with-eureka:是否将自己作为服务注册到其他Eureka服务注册中心(单节点无需配置启用)
注意事项

security.basic.enabled 配置过时或不可用解决:需要自己实现一个配置类继承自WebSecurityConfigurerAdapter,并重写configure(http)方法

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
package net.zzqd;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
* @author zqnh
* @date 2019/8/20 on 20:47.
*/
@Configuration
@EnableWebSecurity
class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().antMatchers("/**").permitAll();

}
/**
*
* 配置一个userDetailsService Bean不在生成默认security.user用户
* * @return
* */
@Bean
@Override
protected UserDetailsService userDetailsService()
{
return super.userDetailsService();
}
}

并且在application.yml里添加

1
2
3
4
5
6
7
8
9
10
http:
authorizeRequests():
antMatchers("/**"):
permitAll();:

spring:
security:
user:
name: admin
password: admin