标签:spring server 集群 注册 springcloud 7001 eureka server7001
下面为单机版,后面为集群
一、在之前的父工程中新建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
启动项目测试是否注册进服务中心
标签:spring,server,集群,注册,springcloud,7001,eureka,server7001 来源: https://blog.csdn.net/qq_38132995/article/details/111143480
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。