简介
Eureka作为微服务注册中心, Eureka-Server 可以搭建多台服务注册中心实例, 并相互进行注册, 形成高可用的集群 .
创建Eureka注册中心微服务
1. 创建微服务应用microservicecloud-eureka , 导入依赖.
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. 编写配置
server:
port: 7001
spring:
application:
name: eureka-server
eureka:
instance:
hostname: www.eureka01.com
instance-id: eureka01
prefer-ip-address: true
server:
enable-self-preservation: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://www.eureka01.com:7001/eureka,http://www.eureka02.com:7002/eureka,http://www.eureka03.com:7003/eureka
3. host文件添加域名映射
C:\Windows\System32\drivers\etc\hosts 文件添加自定义域名, 映射到本地地址127.0.0.1
127.0.0.1 www.eureka01.com
127.0.0.1 www.eureka02.com
127.0.0.1 www.eureka03.com
4. 编写启动类并复制多个实例
首先, 编写eureka-server 的启动类, 开启Eureka服务端 配置(@EnableEurekaServer 注解).
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
然后,启动eureka01 实例, 并复制eureka02 , eureka03 实例后启动各个微服务实例, eureka-server 各个实例相互注册, 形成高可用的集群 . 复制eureka02 实例, VM参数: -Dserver.port=7002 -Deureka.instance.hostname=www.eureka02.com -Deureka.instance.instance-id=eureka02 复制eureka03 实例, VM参数: -Dserver.port=7003 -Deureka.instance.hostname=www.eureka03.com -Deureka.instance.instance-id=eureka03 最后, 访问各位服务实例, 查看eureka-server 控制台的服务注册列表: http://www.eureka01.com:7001/ http://www.eureka02.com:7002/ http://www.eureka03.com:7003/
个人博客
欢迎各位访问我的个人博客: https://www.crystalblog.xyz/
备用地址: https://wang-qz.gitee.io/crystal-blog/
|