搭建微服务注册中心

本教程使用Eureka来作为微服务的注册中心,Eureka遵循AP原则,

CAP原则如下:强一致性(C)Consistency;可用性(A)Avaliability;分区容错性(P)Partition tolerance

点击New -> Module...,新建一个Maven模块(以后创建module将不再赘述)

点击下一步,如下图所示填写自己ArtifactId是继承父级的


然后修改pom文件,这里需要引入eureka的依赖,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>heal-cloud</artifactId>
<groupId>com.tan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>heal-register</artifactId>
<description>heal-Cloud服务注册中心</description>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

spring cloud版本为Greenwich.SR1,这个在教程二的父模块中有指定。

在src下面创建com.tan.register包结构,在下面创建HealRegisterApplication类,内容如下:

1
2
3
4
5
6
7
8
package com.tan.register;
@EnableEurekaServer
@SpringBootApplication
public class HealRegisterApplication {
public static void main(String[] args) {
SpringApplication.run(HealRegisterApplication.class, args);
}
}

在com.tan.register下面创建configure包结构,在下面创建HealRegisterWebSecurityConfigure类,用来配置访问Eureka服务,内容如下:

1
2
3
4
5
6
7
8
9
package com.tan.register.configure;
@EnableWebSecurity
public class HealRegisterWebSecurityConfigure extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}

在resources下创建application.properties配置文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 服务端口
server.port=8001
# 服务路径
server.servlet.context-path=/register

# security安全控制器用户名和密码
spring.security.user.name=heal
spring.security.user.password=123456

# 服务名称
spring.application.name=Heal-Register
# Eureka服务端的地址,我本地搭建的,故为localhost
eureka.instance.hostname=localhost
# 表示是否将服务注册到Eureka服务端,由于我们这里是单节点的Eureka服务端,所以这里指定false
eureka.client.register-with-eureka=false
# 表示是否从Eureka服务端获取服务信息,因为这里是单节点的Eureka服务端,并不需要从别的Eureka服务端同步服务信息,所以这里设置为false
eureka.client.fetch-registry=false
# 微服务更新实例信息的变化到Eureka服务端的间隔时间,单位为秒,这里指定为30秒
eureka.client.instance-info-replication-interval-seconds=30
# 指定Eureka服务端的地址,这里为当前项目地址,这里前面需要加上security安全控制器用户名和密码,才可以正常访问
eureka.client.serviceUrl.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}${server.servlet.context-path}/eureka/

注册中已创建并配置完毕,启动刚刚创建的HealRegisterApplication中的main方法即可启动项目,启动成功后访问:http://localhost:8001/register/,就可以看到Eureka的登录页面。如下:


输入上面配置文件配置的安全器的用户名和密码,登录后,就会进入到Eureka的主页面。如下:


现在,Eureka注册中已经搭建完毕了!