Spring Cloud Netflix之Euraka Server注册中心

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ptsx0607/article/details/88953304

服务的注册中心

  Spring Cloud Netflix提供Euraka Server来实现注册中心。注册中心在整个微服务架构中是最核心的模块,用于提供注册中心给微服务实例实现自动化注册与发现。为实现注册中心的高可用,一般会创建多个注册中心,并相互注册(一般创建2个),避免单点故障导致服务不可用。

  以下介绍如何搭建高可用的服务注册中心,并实现实例下线时注册中心对实例列表的动态删除。

搭建环境,仍让使用STS,方便。

1. 新建一个spring starter project

 项目默认结构如下

一 、 引入jar包依赖pom.xml

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

 如果只是引入了以上内容,会提示招不到jar,需要新增以下两部分内容

全部pom.xml内容如下

<?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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.lee</groupId>
	<artifactId>EurekaServer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>EurekaServer</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<!-- eureka-server02 -->
		<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- eureka-server01 -->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
	</dependencies>
	<!-- eureka-server03 -->
	 <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>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

二、application.properties

spring.application.name=eureka-server

server.port=10001
#同理配置另一个注册中心,(需要在hosts文件中增加127.0.0.1 peer0  127.0.0.1 peer1)。
eureka.instance.hostname: peer1
#强制不注册到注册服务器
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

#驱逐下线的服务,间隔,5秒,默认是60,建议开发和测试环境配置
#org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean.evictionIntervalTimerInMs
eureka.server.evictionIntervalTimerInMs=5000
 主要几个配置的说明:
eureka.instance.hostname:注册中心的注册名。
euraka.client.register-with-euraka:配置为false,表示不将自己注册到注册中心。
euraka.client.serviceUrl.defaultZone:配置defaultZone中的注册地址,该项用于配置注册中心高可用时,需要向另一台注册中心注册自己。(此配置为key-value类型,defaultZone为默认Zone的key)
euraka.client.fetch-registry:配置为false,表明该注册中心只负责管理实例,不负责去检索实例服务。
euraka.server.enable-self-preservation:配置为false,用处是为了注册中心能及时删除下线服务,而不是保留,配置禁止其保护模式。(根据项目实际需求配置)

三、 在入口程序中使用@EnableEurakaServer声明为euraka server

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}

}

四、启动项目,访问 http://localhost:10001/

猜你喜欢

转载自blog.csdn.net/ptsx0607/article/details/88953304