SpringCloud_Eureka
适用于springboot微服务的注册管理中心
依赖pom.xml
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.pingruan</groupId>
<artifactId>vander-framework-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>org.pingruan.springboot</groupId>
<artifactId>vander-server-center</artifactId>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件bootstrap.yml
spring:
security: # 安全访问
user:
name: root
password: qwe123
profiles:
active: stand-alone
---
#演示-单机 默认
spring:
profiles: stand-alone
application:
name: vander-eureka‐center #指定服务名
server:
port: 9001 #服务端口
eureka:
client:
registerWithEureka: false #服务注册,是否将自己注册到Eureka服务中
fetchRegistry: false #服务发现,是否从Eureka中获取注册信息
serviceUrl: #Eureka客户端与Eureka服务端的交互地址,高可用状态配置对方的地址,单机状态配置自己(如果不配置则默认本机8761端口)
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:9001/eureka/
server:
enable-self-preservation: false #是否开启自我保护模式
eviction-interval-timer-in-ms: 6000 #服务注册表清理间隔(单位毫秒,默认是60*1000)
---
#演示-高可用配置 第一台注册中心
spring:
profiles: high-availability-a
application:
name: vander-eureka‐center #指定服务名
server:
port: 9002 #服务端口
eureka:
client:
registerWithEureka: true #服务注册,是否将自己注册到Eureka服务中
fetchRegistry: true #服务发现,是否从Eureka中获取注册信息
serviceUrl: #Eureka客户端与Eureka服务端的交互地址,高可用状态配置对方的地址,单机状态配置自己(如果不配置则默认本机8761端口)
defaultZone: http://server2:9003/eureka/
server:
enable-self-preservation: false #是否开启自我保护模式
eviction-interval-timer-in-ms: 6000 #服务注册表清理间隔(单位毫秒,默认是60*1000)
instance:
hostname: server1
---
#演示-高可用配置 第二台注册中心
spring:
profiles: high-availability-b
application:
name: vander-eureka‐center #指定服务名
server:
port: 9003 #服务端口
eureka:
client:
registerWithEureka: true #服务注册,是否将自己注册到Eureka服务中
fetchRegistry: true #服务发现,是否从Eureka中获取注册信息
serviceUrl: #Eureka客户端与Eureka服务端的交互地址,高可用状态配置对方的地址,单机状态配置自己(如果不配置则默认本机8761端口)
defaultZone: http://server1:9002/eureka/
server:
enable-self-preservation: false #是否开启自我保护模式
eviction-interval-timer-in-ms: 6000 #服务注册表清理间隔(单位毫秒,默认是60*1000)
instance:
hostname: server2
配置源码
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* @Description: 高版本的丢弃了
* security:
* basic:
* enabled: true 配置,应该使用以下方式开启
* @Param: [http]
* @Return: void
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
// Configure HttpSecurity as needed (e.g. enable http basic).
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable();
//注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
// 如果是form方式,不能使用url格式登录
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
/**
* 注册中心
*
* @author vander
*
*/
@EnableEurekaServer//标识这是一个Eureka服务
@SpringBootApplication
public class SpringBootServerCenter {
public static void main(String[] args) {
SpringApplication.run(SpringBootServerCenter.class, args);
}
}