搭建注册中心
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 非必须,用来加载bootstrap.yml,这部分的内容也可以直接写在applicaition.yml中 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
application.yml
spring:
application:
name: eureka
bootstrap.yml
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://127.0.0.1:8761/eureka
启动类添加@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
查看注册中心
启动项目,访问http://127.0.0.1:8761/
业务服务注册
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
bootstrap.yml
server:
port: 8090
eureka:
instance:
# 以IP注册到注册中心
preferIpAddress: ${EUREKA_INSTANCE_PREFER_IP_ADDRESS:true}
# 续约间隔时间,默认30秒
leaseRenewalIntervalInSeconds: ${EUREKA_INSTANCE_LEASE_RENEWAL_INTERVAL_IN_SECONDS:30}
# 实例故障摘除时间,默认90秒,实际是90*2=180秒,配置45就是90秒
leaseExpirationDurationInSeconds: ${EUREKA_INSTANCE_LEASE_EXPIRATION_DURATION_IN_SECONDS:45}
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka
registryFetchIntervalSeconds: 30
启动类添加@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
查看注册中心
启动项目,访问http://127.0.0.1:8761/,业务服务已经注册上了
Eureka添加认证
pom添加Security依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.yml中配置Security相关参数
spring:
security:
user:
name: root
password: 123456
处理CSRF
package com.learn.eureka.config;
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.web.SecurityFilterChain;
/**
* @author PC
*/
@Configuration
public class RegisterConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
return http.build();
}
}
查看注册中心
重启项目,访问http://127.0.0.1:8761/,可以看到需要认证
登录后查看注册中心,会发现业务服务已经下线,这是因为业务服务也需要认证
业务服务添加认证
bootstrap.yml调整defaultZone
eureka:
instance:
# 以IP注册到注册中心
preferIpAddress: ${EUREKA_INSTANCE_PREFER_IP_ADDRESS:true}
# 续约间隔时间,默认30秒
leaseRenewalIntervalInSeconds: ${EUREKA_INSTANCE_LEASE_RENEWAL_INTERVAL_IN_SECONDS:30}
# 实例故障摘除时间,默认90秒,实际是90*2=180秒,配置45就是90秒
leaseExpirationDurationInSeconds: ${EUREKA_INSTANCE_LEASE_EXPIRATION_DURATION_IN_SECONDS:45}
client:
service-url:
defaultZone: http://root:123456@127.0.0.1:8761/eureka
registryFetchIntervalSeconds: 30
查看注册中心
重启项目,查看注册中心
参考资料
[1].eureka项目
[2].demo项目