Eureka
EurekaServer
依赖
pom.xml
<!-- 导入eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
启动类
@EnableEurekaServer //开启EurekaServer支持
@SpringBootApplication
public class GovernApp {
public static void main(String[] args) {
SpringApplication.run(GovernApp.class,args);
}
}
单机状态
单机状态是所有的服务都注册到这一个EurekaServer中,缺点是当这台EurekaServer宕机的时候,微服务之间都不能正常调用,一般是开发环境下使用。
配置文件
server:
port: 50101 #服务端口
spring:
application:
name: xc‐govern‐center #指定服务名
eureka:
client:
registerWithEureka: false #服务注册,是否将自己注册到Eureka服务中
fetchRegistry: false #服务发现,是否从Eureka中获取注册信息
serviceUrl: #Eureka客户端与Eureka服务端的交互地址,高可用状态配置对方的地址,单机状态配置自己(如果 不配置则默认本机8761端口)
defaultZone: http://localhost:50101/eureka/
server:
enable‐self‐preservation: false #是否开启自我保护模式
eviction‐interval‐timer‐in‐ms: 60000 #服务注册表清理间隔(单位毫秒,默认是60*1000)
高可用环境(集群)
高可用环境至少需要2台EurekaServer,他们之间相互注册,优点是当EurekaServer发生宕机的时候,只要有一台以上存活,远程调用依旧可以使用。
server:
port: ${PORT:50101}
spring:
application:
name: xc‐govern‐center
eureka:
client:
#服务注册,是否将自己注册到Eureka服务中
registerWithEureka: true
#服务发现,是否从Eureka中获取注册信息
fetchRegistry: true
#Eureka客户端与Eureka服务端的交互地址,高可用状态配置对方的地址,单机状态配置自己(如果 不配置则默认本机8761端口)
serviceUrl:
defaultZone: ${SERVER_URL:http://eureka02:50101/eureka/}
server:
#是否开启自我保护模式
enable‐self‐preservation: false
#服务注册表清理间隔(单位毫秒,默认是60*1000)
eviction‐interval‐timer‐in‐ms: 60000
instance:
hostname: ${DOMAIN_NAME:eureka01}
#true : 将本机的ip注册到eurekaServer false:将本机的hostname注册到eurekaServer
prefer-ip-address: false
自我保护模式
如果EurekaServer一定时间内没有收到注册服务实例的心跳包,EurekaServer 会将该实例注销,但是当发生网络故障的时候,这个行为就会很危险,因为服务本身是健康的,这个时候不应该注销该服务。自我保护模式就是为了解决这个问题。当节点在短时间内丢失过多客户端时,节点会进入自我保护模式,这个时候EurekaServer不会因为没有收到心跳包而移除该服务,当网络故障恢复后会自动退出自我保护模式。
用户认证
eurekaServer本身是支持匿名访问的,但是出于安全考虑,还是需要加一个用户认证,此时需要借助spring-boot-starter-security的支持
<!-- 安全认证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
对于老的版本配置
# 安全认证
security:
user:
# 用户名
name: user
# 密码
password: 123
basic:
# 开启基于http basic的认证
enabled: true
对于2.0之后的版本,security5.0以后的版本
spring:
application:
name: xc‐govern‐center
# 安全认证
security:
user:
name: user
password: 123
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@EnableEurekaServer //开启EurekaServer支持
@SpringBootApplication
@EnableWebSecurity// 开启安全验证
public class GovernApp {
public static void main(String[] args) {
SpringApplication.run(GovernApp.class,args);
}
}
对于2.0版本之后的springboot 需要另外使用一个配置类,否则会导致eurekaServer不能注册到服务器上
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
使用了用户认证之后,eureka.client.serviceUrl.defaultZone上需要设置用户名密码如:
http://用户名:密码@域名:端口/eureka/
EurekaClient
依赖
<!-- 导入eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类
@EnableEurekaClient 和@EnableDiscoveryClient 都可以 开启服务发现
@EnableDiscoveryClient//@EnableEurekaClient 和@EnableDiscoveryClient 都可以 开启服务发现
@SpringBootApplication
@EntityScan("com.xuecheng.framework.domain.cms")//扫描实体类
@ComponentScan(basePackages={"com.xuecheng.api","com.xuecheng.framework","com.xuecheng.manage_cms"})//扫描接口
public class ManageCmsApplication {
public static void main(String[] args) {
SpringApplication.run(ManageCmsApplication.class,args);
}
}
其他配置
配置 | 描述 |
---|---|
spring.cloud.service-registry.auto-registration.enabled | false:关闭服务自动注册到EurekaServer |
eureka.instance.metadata-map.my-metadata | 自定义元数据 |
遇到的坑
- 使用用户认证后不能将服务注册到eurekaServer