赞
踩
Eureka是Netflix开源的服务发现组件,本身是基于Rest的服务,它包含服务端和客户端两部分;在SpringCloud中将它集成在其中,从而实现了微服务的发现与注册;
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.edu.springboot</groupId> <artifactId>eureka-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <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>
加上@EnableEurekaServer
spring.application.name=eureka-server server.port=8025 # logging 配置 logging.config=classpath:log4j2.xml #运行环境 eureka.environment=dev eureka.instance.hostname=127.0.0.1 #由于该应用是注册中心,false:代表不向注册中心注册自己;true:代表注册自己。多节点时打开 eureka.client.register-with-eureka=false #是否启动检测服务,表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据 eureka.client.fetch-registry=false #注册地址 多节点用,分隔 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ #是否开启保护模式 测试时,需要关闭,线上打开 eureka.server.enable-self-preservation=false
红字的意思是:自动保存模式关闭。在网络/其他问题的情况下,这可能无法保护实例失效。
关于Eureka自我保护机制可以参考:Eureka自我保护机制
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.edu.springboot</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-commons</artifactId> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </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>
加上@EnableDiscoveryClientr
spring.application.name=eureka-client server.port=8030 # logging 配置 logging.config=classpath:log4j2.xml #ribbon 饥饿加载 #ribbon.eager-load.enabled=true #指定需要饥饿加载的客户端名称、服务名 #ribbon.eager-load.clients=eureka-client,eureka-server #ribbon.ConnectTimeout=90000 #ribbon.ReadTimeout=90000 #根据ip注册实例 eureka.instance.prefer-ip-address=true #指定注册实例ID(默认是主机名:应用名:应用端口) #eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port} #指定注册实例主机名 #eureka.instance.hostname=127.0.0.1 #eureka.instance.hostname= ${spring.cloud.client.ip-address} #注册地址 eureka服务端的地址 多节点用,分隔 eureka.client.service-url.defaultZone=http://127.0.0.1:8025/eureka/
eureka的服务注册中心、服务端、客户端要使用同一版本,具体版本如下(也可以去SpringCloud官网查询:https://spring.io/projects/spring-cloud):
查询到版本后可以到https://mvnrepository.com/搜索下载
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。