搭建Eureka注册中心(环境配置)

创建Eureka Server (注册中心)

创建Eureka项目、配置

创建Spring Initializr项目,添加Eureka Server依赖

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 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.3.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>xx.xx</groupId>
    <artifactId>artifactId</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekaName</name>
    <description>Demo project for Spring Boot</description>
   
 	<!-- Hoxton.SR10对应spring2.3.9版本 -->
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR10</spring-cloud.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    
    <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.yml配置
	# 向注册中心注册的“服务ID”
spring:
  application:
    name: eureka-server
    
    # 服务端口号
server:
  port: 2001

eureka:
  instance:
    # 主机名,集群中用主机名互相区分
    hostname: eureka1
  client:
    # 针对单台服务器,不向自己注册,也不从自己拉取
    register-with-eureka: false
    fetch-registry: false
  server:
    # 禁用自我保护模式(开发调试时建议关闭)
    enable-self-preservation: false

  • eureka 集群服务器之间,通过 hostname 来区分
  • eureka.server.enable-self-preservation
    eureka 的自我保护状态:心跳失败的比例,在15分钟内是否超过85%,如果出现了超过的情况,Eureka Server会将当前的实例注册信息保护起来,同时提示一个警告,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据。也就是不会注销任何微服务
  • eureka.client.register-with-eureka=false
    不向自身注册
  • eureka.client.fetch-registry=false
    不从自身拉取注册信息
  • eureka.instance.lease-expiration-duration-in-seconds
    最后一次心跳后,间隔多久认定微服务不可用,默认90

主程序

启动类上添加 @EnableEurekaServer

package cn.eureka;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaTestApplication {

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

Eureka 域名映射

修改hosts文件,地址:C:\Windows\System32\drivers\etc\hosts
可以使用工具:switchHosts

127.0.0.1 eureka1
127.0.0.1 eureka2

启动测试:http://eureka1:2001

在这里插入图片描述

创建 Service Provider 服务提供者(微服务)

微服务注册到 eureka 服务器

  1. pom.xml 添加eureka依赖
  2. application.yml 添加eureka注册配置
  3. 主程序启用eureka客户端
  4. 启动服务,在eureka中查看注册信息

pom.xml 微服务项目添加依赖:

Eureka Discovery Client

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

application.yml 配置:

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka
  • eureka.instance.lease-renewal-interval-in-seconds 心跳间隔时间,默认 30 秒
  • defaultZone,默认位置,可以修改为具体地理位置,比如:beiJing, shangHai, shenZhen 等,表示
    eureka 服务器的部署位置, 需要云服务器提供
  • eureka.client.registry-fetch-interval-seconds 拉取注册信息间隔时间,默认 30 秒

主程序

启动类上添加 @EnableDiscoveryClient

package cn.tedu.sp03;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class UserserviceApplication {

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

启动测试:http://eureka1:2001

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值