Spring Cloud Eureka服务注册中心
微服务服务提供方------点击查看
关于注册中心的优化以及状态监测后续会在该篇博客补充。
一、基于Spring Boot项目的POM依赖
注意:
- 核心依赖spring-cloud-starter,spring-cloud-starter-netflix-eureka-server,其余依赖包是附加功能。
- 对于集群化服务注册中心来说,只要将服务注册到一个服务中心即可,其余服务注册中心会自动同步这个服务。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.drsanjun</groupId>
<artifactId>Drsanjun-Eureka-RegistrationCenter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Drsanjun-Eureka-RegistrationCenter</name>
<description>注册中心</description>
<properties>
<!--JDK -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Cloud启动依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<!-- Spring Actuator启动依赖(状态监测) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Spring Eureka启动服务端依赖(服务注册服务端) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Security安全认证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Spring Boot支持xml和properties配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring Boot支持热启动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<!-- Spring Cloud依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- SpringBoot Maven插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、单一节点的服务注册中心
1、Spring Boot主启动类
@SpringBootApplication表示该类为主程序入口
@EnableEurekaServer表示开启Eureka注册中心
package com.drsanjun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaRegistrationCenterApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRegistrationCenterApplication.class, args);
}
}
2、配置详解
#配置服务器端口号
server.port=7111
#主机地址
eureka.instance.hostname=localhost
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#不检索服务
eureka.client.fetch-registry=false
#注册中心地址
eureka.client.service-url.defaultZone =http://${eureka.instance.hostname}:${server.port}/eureka/
#安全配置:服务注册中心用户名和密码
spring.security.user.name=drsanjun
spring.security.user.password=123456
#日志配置
logging.level.root=info
logging.level.org=warn
logging.level.com.drsanjun=debug
二、集群化服务注册中心
a、集群化服务注册中心就是将系统中所有的服务注册中心相互注册,形成服务网络。
b、如果是本机环境测试,可能需要在C:\Windows\System32\drivers\etc\hosts文件中添加如下:
127.0.0.1 localhost1
127.0.0.1 localhost2
127.0.0.1 localhost3
c、集群部署时可以使用三个不同的源码部署在不同的主机上;如果是部署在同一台主机的多个节点,可以为项目指定不同的配置文件,然后分别在不同的端口启动三个服务即可,不需要拷贝三份源代码。
1、注册中心A配置
#配置服务器端口号
server.port=7111
#主机地址
eureka.instance.hostname=localhost1
#注册中心地址
eureka.client.service-url.defaultZone =http://${eureka.instance.hostname}:${server.port}/eureka/
,http://localhost2:7112/eureka/
,http://localhost3:7113/eureka/
#安全配置:服务注册中心用户名和密码
spring.security.user.name=drsanjun
spring.security.user.password=123456
#日志配置
logging.level.root=info
logging.level.org=warn
logging.level.com.drsanjun=debug
2、注册中心B配置
#配置服务器端口号
server.port=7112
#主机地址
eureka.instance.hostname=localhost2
#注册中心地址
eureka.client.service-url.defaultZone =http://${eureka.instance.hostname}:${server.port}/eureka/
,http://localhost2:7111/eureka/
,http://localhost3:7113/eureka/
#安全配置:服务注册中心用户名和密码
spring.security.user.name=drsanjun
spring.security.user.password=123456
#日志配置
logging.level.root=info
logging.level.org=warn
logging.level.com.drsanjun=debug
3、注册中心C配置
#配置服务器端口号
server.port=7113
#主机地址
eureka.instance.hostname=localhost3
#注册中心地址
eureka.client.service-url.defaultZone =http://${eureka.instance.hostname}:${server.port}/eureka/
,http://localhost2:7111/eureka/
,http://localhost3:7112/eureka/
#安全配置:服务注册中心用户名和密码
spring.security.user.name=drsanjun
spring.security.user.password=123456
#日志配置
logging.level.root=info
logging.level.org=warn
logging.level.com.drsanjun=debug
4、Spring Boot主启动类
相比于单一节点的服务注册中心,多了一个@EnableDiscoveryClient用于开启发现服务的功能
```java
package com.drsanjun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient
public class EurekaRegistrationCenterApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRegistrationCenterApplication.class, args);
}
}