服务注册、发现之eureka
- 官网地址:
https://spring.io/projects/spring-cloud-netflix
eureka服务端
- 创建eureka-server项目(springboot)
- 添加eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 在启动类上添加
@EnableEurekaServer
注解
- 配置eureka application.yml
- 配置项和默认值在
EurekaServerConfigBean
类可以查看
server:
port: 6001
eureka:
instance:
# eureka服务端的实例名称
hostname: eureka-local
# EurekaClient向EurekaServer发送心跳的频率(默认30秒),如果在 lease-expiration-duration-in-seconds 指定的时间内未收到心跳,则移除该实例
lease-renewal-interval-in-seconds: 3
# Eureka Server 在接收到上一个心跳之后等待下一个心跳的秒数(默认90秒),若不能在指定时间内收到心跳,则移除此实例,并禁止此实例的流量
lease-expiration-duration-in-seconds: 10
client:
# 是否像服务注册中心注册自己
register-with-eureka: false
# registry 为false,标识自己未注册中心
fetch-registry: false
service-url:
# 单机配置,访问地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 集群配置,多个服务地址逗号隔开
# defaultZone: http://localhost:6002/eureka/,http://localhost:6003/eureka/
server:
# 表示Eureka Server清理无效节点的频率,默认60000 毫秒(60 秒)
eviction-interval-timer-in-ms: 5000
use-read-only-response-cache: false
logging:
level:
com.netflix: error
eureka客户端
- 在无服务的应用上添加eureka-client依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 在启动类上面添加开启注解
@EnableEurekaClient
或者@EnableDiscoveryClient
- 配置client端eureka配置
application.yml
- 配置项和默认值在
EurekaInstanceConfigBean
类可以查看
server:
port: 8001
spring:
application:
# eureka上面的application名称
name: privider-server
eureka:
client:
service-url:
# eureka服务的地址,如果eureka是集群的话,多个服务地址使用逗号分隔
defaultZone: http://127.0.0.1:6001/eureka/
# 表示客户端是否从Eureka Server获取实例注册信息
fetch-registry: true
registry-fetch-interval-seconds: 3
instance:
instance-id: ${eureka.instance.ip-address}:${server.port}
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 5
# true-实例以IP的形式注册 false-实例以机器housenmae形式注册
prefer-ip-address: true
ip-address: 127.0.0.1
eureka安全配置spring-security
eureka服务端安全配置
- 在eureka-server应用添加security依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 添加配置类
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;
/**
* @author liouwb
*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
- 在application.yml配置文件添加用户信息
spring:
security:
user:
name: admin
password: 1234
roles: admin
- 再访问eureka服务端就需要登录了
- 输入配置文件里面的用户名密码就可以正常登录了
eureka客户端安全配置
- 这个时候eurekaclient就注册不到eureka服务上了,需要对eurekaclient项目进行改造
- 在eureka客户端添加spring-security依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 修改配置文件,在eureka配置下添加安全配置
eureka:
client:
service-url:
# http://用户名:密码@ip:port/eureka/
defaultZone: http://admin:1234@127.0.0.1:6001/eureka/
fetch-registry: true
registry-fetch-interval-seconds: 3
instance:
instance-id: ${eureka.instance.ip-address}:${server.port}
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 5
prefer-ip-address: true
ip-address: 127.0.0.1
- 之后eureka客户端就能正常注册到eureka服务端了
- 其他自定义安全授权配置可以参考spring-security