SpringCloud之Eureka
基本配置
配置pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
配置application.properties
#服务名称
spring.application.name=register
#服务端口
server.port=1001
#自我注册
eureka.client.register-with-eureka=true
## 默认90秒,监听心跳时间
eureka.instance.lease-expiration-duration-in-seconds=10
## 默认30秒,表示eureka client发送心跳给server端的频率。
eureka.instance.lease-renewal-interval-in-seconds=3
配置Application
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
增加security
增加pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
增加application.properties
#加密认证账户
security.user.name=frank
#加密认证密码
security.user.password=frank123456
高可用
创建application-register1.properties
#原application.properties的server.port移至此文件
server.port=1001
#eureka实例主机名
eureka.instance.hostname=register1
#注册到其他注册中心
eureka.client.service-url.defaultZone=http://frank:frank123456@127.0.0.1:1002/eureka/,http://frank:frank123456@127.0.0.1:1003/eureka/
创建application-register2.properties
#原application.properties的server.port移至此文件
server.port=1002
#eureka实例主机名
eureka.instance.hostname=register2
#注册到其他注册中心
eureka.client.service-url.defaultZone=http://frank:frank123456@127.0.0.1:1001/eureka/,http://frank:frank123456@127.0.0.1:1003/eureka/
创建application-register3.properties
#原application.properties的server.port移至此文件
server.port=1003
#eureka实例主机名
eureka.instance.hostname=register3
#注册到其他注册中心
eureka.client.service-url.defaultZone=http://frank:frank123456@127.0.0.1:1001/eureka/,http://frank:frank123456@127.0.0.1:1002/eureka/
修改application.properties
#服务名称
spring.application.name=register
#服务端口
#server.port=1001
#自我注册
eureka.client.register-with-eureka=true
#加密认证账户
security.user.name=frank
#加密认证密码
security.user.password=frank123456
spring.profiles.active=register1
总
application.properties
spring.profiles.active=register1
#加密认证
security.user.name=frank
security.user.password=frank123456
spring.application.name=register
#自我注册
eureka.client.register-with-eureka=true
#eureka自我保护装置
eureka.server.enable-self-preservation=true
eureka.client.fetch-registry=true
## 默认90秒,监听心跳时间
eureka.instance.lease-expiration-duration-in-seconds=10
## 默认30秒,表示eureka client发送心跳给server端的频率。
eureka.instance.lease-renewal-interval-in-seconds=3
application-register1.properties
#端口号
server.port=1001
#eureka实例主机名
eureka.instance.hostname=register2
eureka.client.service-url.defaultZone=http://frank:frank123456@127.0.0.1:1002/eureka/,http://frank:frank123456@127.0.0.1:1003/eureka/
application-register2.properties
#端口号
server.port=1002
#eureka实例主机名
eureka.instance.hostname=register2
eureka.client.service-url.defaultZone=http://frank:frank123456@127.0.0.1:1001/eureka/,http://frank:frank123456@127.0.0.1:1003/eureka/
application-register1.properties
#端口号
server.port=1003
#eureka实例主机名
eureka.instance.hostname=register3
eureka.client.service-url.defaultZone=http://frank:frank123456@127.0.0.1:1001/eureka/,http://frank:frank123456@127.0.0.1:1002/eureka/
Application
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
小结
- 如果不使用自我注册配置
eureka.client.register-with-eureka=true
可以
eureka.client.service-url.defaultZone=http://frank:frank123456@127.0.0.1:1001/eureka/,http://frank:frank123456@127.0.0.1:1002/eureka/,http://frank:frank123456@127.0.0.1:1002/eureka/
- 使用idea,启动多次,勾上即可,每次启动的时候,切换eureka.instance.hostname的值【register1】【register2】【register3】指的是对应的文件,即application-${eureka.instance.hostname}.properties
原文地址:https://blog.csdn.net/m0_37975854/article/details/111478821
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。