Eureka 学习
本文章
Springboot 版本号: 2.0.2.RELEASE
Springcloud版本号: Finchley.RELEASE
1. 创建一个maven项目
hayes-spring-cloud
- pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
</modules>
<properties>
<java.version>1.8</java.version>
<spring.boot.version>2.0.2.RELEASE</spring.boot.version>
<spring.cloud.version>Finchley.RELEASE</spring.cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.53</version>
</dependency>
</dependencies>
2. 创建module
hayes-spring-cloud-eureka
- pom.xml
<parent>
<groupId>com.hayes.base</groupId>
<artifactId>hayes-spring-cloud-</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--引入security 加密-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
- application.yml
server:
port: 1111
spring:
security:
user:
name: wolf
password: cloud
eureka:
instance:
hostname: localhost
# 使用主机名来定义注册中心的地址, 也可以使用IP地址的形式
prefer-ip-address: true
# 上一次收到client的心跳之后,等待下一次心跳的超时时间
lease-expiration-duration-in-seconds: 90
# 发送心跳给server端的频率 默认30
lease-renewal-interval-in-seconds: 20
server:
# 设为false,关闭自我保护
enable-self-preservation: false
# 清理无效节点的时间间隔
eviction-interval-timer-in-ms: 60000
client:
# 不注册自己
register-with-eureka: false
# 注册中心的职责就是维护服务实例, 它并不需要去检索服务
fetch-registry: false
# 间隔多久去拉取服务注册信息 默认30
registry-fetch-interval-seconds: 20
service-url.defaultZone: http://wolf:cloud@${eureka.instance.hostname}:${server.port}/eureka/
# service-url.defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- EurekaApplication.class
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); //关闭csrf
http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证
}
}
}