下面为单机版,后面为集群
一、在之前的父工程中新建module eureka服务1
eureka-server7001
eureka-server7002
二、引入pom
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
三、写yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
三、在主启动类加@EnableEurekaServer
启动项目。访问localhost:7001看到eureka客户端页面
下面是集群
一、测试将系统hosts修改下域名下
#在C:\Windows\System32\drivers\etc
127.0.0.1 eureka-server7001
127.0.0.1 eureka-server7002
二、分别在创建的两个项目的yml
7001服务器
server:
port: 7001
spring:
application:
name: eureka-server7001
eureka:
instance:
hostname: eureka-server7001 #eureka服务端的实例名字 #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka-server7002:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
7002服务器
server:
port: 7002
spring:
application:
name: eureka-server7002
eureka:
instance:
hostname: eureka-server7002 #eureka服务端的实例名字 #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka-server7001:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
三、将之前的项目注册到注册中心
1、重新设置添加pom
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、修改yml
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka-server7001:7001/eureka,http://eureka-server7001:7001/eureka #集群版
3、在启动类上加@EnableEurekaClient
启动项目测试是否注册进服务中心
扫描二维码关注公众号,回复:
12914236 查看本文章
