栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud之Eureka服务注册中心

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud之Eureka服务注册中心

一、Eureka介绍

1、Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。

SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

2、Eureka包含两个组件:Eureka Server和Eureka Client。

在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。

Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。

二、Eureka搭建 2.1、Eureka Server搭建

新建springboot项目neil-eureka-server。

在pom.xml里面添加Eureka的依赖

		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		

配置文件application.yml

server:
  port: 8989

spring:
  application:
    name: neil-eureka-server

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8989/eureka/
    fetch-registry: true
    register-with-eureka: true
  instance:
    prefer-ip-address: true

springboot启动类里面添加@EnableEurekaServer注解

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

@EnableEurekaServer
@SpringBootApplication
public class NeilEurekaServerApplication {

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

启动neil-eureka-server

在浏览器地址栏输入机器ip加端口访问Eureka页面
http://127.0.0.1:8989/

2.2 、Eureka Client搭建

新建springboot项目neil-eureka-client。

在pom.xml里面添加Eureka的依赖。

		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		

配置文件application.yml

server:
  port: 8990
  servlet:
    context-path: /neil-eureka-client
spring:
  application:
    name: neil-eureka-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8989/eureka/ #Eureka Server地址
  instance:
    prefer-ip-address: true  #Hostname使用主机ip

springboot启动类里面添加@EnableEurekaClient注解

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

@EnableEurekaClient
@SpringBootApplication
public class ProductApplication {

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

}

启动neil-eureka-client

在浏览器地址栏输入机器ip加端口访问Eureka页面,查看是否注册上Eureka了。

http://127.0.0.1:8989/

至此,Eureka的服务端和客户端就搭建完成了!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号