Eureka 入门案例

eureka-demo 聚合工程。SpringBoot 2.2.4.RELEASESpring Cloud Hoxton.SR1

创建项目

我们创建聚合项目来讲解 Eureka,首先创建一个 pom 父工程。

image

image

image

添加依赖

pom.xml

4.0.0


com.example

eureka-demo

1.0-SNAPSHOT




org.springframework.boot
spring-boot-starter-parent
2.2.4.RELEASE



Hoxton.SR1






org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import


注册中心 eureka-server

在刚才的父工程下创建 eureka-server 注册中心的项目。

创建项目

image

image

image

image

image

添加依赖

pom.xml

4.0.0

com.example
eureka-server
1.0-SNAPSHOT



com.example
eureka-demo
1.0-SNAPSHOT





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



org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-starter-test
test


org.junit.vintage
junit-vintage-engine



配置文件

application.yml

server: port: 8761 # 端口

spring: application: name: eureka-server # 应用名称

配置 Eureka Server 注册中心

eureka: instance: hostname: localhost # 主机名,不配置的时候将根据操作系统的主机名来获取 client: register-with-eureka: false # 是否将自己注册到注册中心,默认为 true fetch-registry: false # 是否从注册中心获取服务注册信息,默认为 true service-url: # 注册中心对外暴露的注册地址 defaultZone: http://{server.port}/eureka/

此时如果直接启动项目是会报错的,错误信息:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect

,这是因为 Eureka 默认开启了将自己注册至注册中心从注册中心获取服务注册信息的配置,如果该应用的角色是注册中心并是单节点的话,要关闭这两个配置项。

启动类

EurekaServerApplication.java

package com.example;

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

@SpringBootApplication // 开启 EurekaServer 注解 @EnableEurekaServer public class EurekaServerApplication {

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

}

访问

访问:http://localhost:8761/

image

今天要说的Eureka 入门案例 篇暂时先说这么多,了解更多技术干货,关注公众号【乐字节发送123可了解,我们一起学习吖】,我是哩哩,一个有趣的灵魂!下期见!

你可能感兴趣的:(Eureka 入门案例)