1、eureka-server的application.xml
spring:
application:
name: eureka-server
security:
user:
name: user
password: password
server:
port: 8761
eureka:
instance:
#eureka服务端配置
prefer-ip-address: false
# 不使用主机名来定义注册中心的地址,而使用IP地址的形式
status-page-url-path: /actuator/info
# 获取此实例状态页的URL路径,然后构造出主机名,安全端口等,默认为/info
health-check-url-path: /actuator/health
# 获取此实例的相对健康检查URL路径,默认为/health
client:
#eureka客户端配置
register-with-eureka: false
# 实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true
fetch-registry: false
# 此客户端是否获取eureka服务器注册表上的注册信息,默认为true
service-url:
# 指定服务注册中心的地址
defaultZone: http://user:password@localhost:8761/eureka/
2、eureka-server的maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3、eureka-client的application.xml
spring:
application:
name: eureka-client-provider
# 设置我们的服务名称
server:
port: 8603
# servlet:
# context-path: /jdbc
# 设置服务访问地址以/jdbc开头
eureka:
instance:
prefer-ip-address: true
# status-page-url-path: /actuator/info
# health-check-url-path: /actuator/health
# 这两个都是默认的地址
client:
service-url:
defaultZone: http://user:password@localhost:8761/eureka/
healthcheck:
enabled: true
#健康检查,eureka实例注册到eureka服务器上是以一个实例为单位的,eureka是根据客户端实例的心跳来确定客户端是否启动,也就是说
#即使我们的实例的一部分服务宕掉,我们的实例整体没有宕掉的时候,请求还是会分发到此实例中,开启健康检查之后,就会将服务的状态
#发送到应用程序
4、如果按照之前的版本,这样子就是正确的,但是在某个版本之后,出现了问题,具体在哪个版本没有详细了解
官方文档就发生了变化,所以只要以官方文档为主就没有问题。
我的是Finchley.SR2版本的,这种之前的也不行,所以根据官方文档。
(1)添加maven依赖(这步官方文档上没写,但是我如果不添,下面的代码报错找不到类)
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
(2)启动类里添加代码
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
此处的路径,一定要和你的eureka-server的default-zone相对应