首先: 创建一个新的项目 作为Eureka的注册中心服务器
需要导入的有
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Greenwich.SR1
pom
import
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
创建入口类S
@SpringBootApplication
@EnableEurekaServer //开启服务的注解 必须要有 表示这是Eureka的注册服务端
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
创建配置文件并配置
application.properties
# 服务基本配置
server.port=9696
# 配置Eureka服务注册中心
# 服务实例的主机名
eureka.instance.hostname=localhost
# 注册中心服务相关配置
# 客户端至少每多少秒发送过来一下心跳 单位秒 没在规定时间发送 我可能会将你剔除
eureka.server.expected-client-renewal-interval-seconds=20
# 时隔多少时间 我会检测下有没有需要剔除的客户端 单位毫秒
eureka.server.eviction-interval-timer-in-ms=1000
# 如果收到的心跳低于期望心跳85%(默认值是0.85),因为有效心跳数如果低于0.85 就可能是注册中心出问题了
# 则注册中心会进入`自我保护机制`- 不删除注册中心认定失效的节点 (就是关闭注册中心剔除节点的权利)
eureka.server.renewal-percent-threshold=0.85
# 当没有新数据产生时 会隔多少时间同步一次心跳值 单位毫秒 (默认值是5分钟)
eureka.server.wait-time-in-ms-when-sync-empty=10000
# 它不仅是一个web的服务实例 也是一个客户端
# 未来我们可以以这个服务端去抓取其他的 eureka 服务实例上的一些东西
# 关闭Eureka自我注册
eureka.client.register-with-eureka=false
# 关闭Eureka抓取功能
eureka.client.fetch-registry=false
Eureka 的注册中心这样就好了 可
http://localhost:9696/
查看注册中心情况
补充下
计算期望心跳值 的方法
一分钟内 能收到几个心跳 然后 乘于 自我保护机制触发比例 取整数
以本期配置的举例
60 / 20 =3
3 * 0.85= 2.55 取整 就是为 2
下面就是设置服务实例
首先还是要添加的依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Greenwich.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
然后是在配置上添加配置
# 服务实例的主机名
# 把服务以什么名字注册
spring.application.name=USER-SERVICE
# 服务实例的id
eureka.instance.instance-id=001
# 你的服务实例在坐标选择器是否优选择它的ip
eureka.instance.prefer-ip-address=true
# 说明这个实例每隔多少秒就发送一次心跳 值不要大于注册中心配置的最少多少秒发送一次心跳 (单位秒)
eureka.instance.lease-renewal-interval-in-seconds=10
# 至少要多长时间没发心跳,你才能认为我这个实例有问题了 (单位秒) 一般要比发送心跳时间长
eureka.instance.lease-expiration-duration-in-seconds=30
# 向Eureka注册中心注册服务
eureka.client.register-with-eureka=true
# 关闭向Eureka抓取功能
eureka.client.fetch-registry=false
# 设置注册中心的 url 就是告诉实例往哪注册
eureka.client.service-url.defaultZone=http://localhost:9696/eureka/
最后便是 web 消费者
首先还是添加依赖
org.springframework.cloud
spring-cloud-dependencies
Greenwich.SR1
pom
import
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
提醒:
注意springcloud和springboot的版本对应关系,springcloud使用英文单词作为版本,springboot是用数字作为版本。 如果不对应的话 程序会起不来并报错
Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating
bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'discoveryCompositeHealthIndicator' defined in class path resource [org/springframework/cloud/client/CommonsClientAutoConfiguration$DiscoveryLoadBalancerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.cloud.client.discovery.health.DiscoveryCompositeHealthIndicator]: Factory method 'discoveryCompositeHealthIndicator' threw exception; nested exception is ...
最新对应关系可以查看官网https://spring.io/projects/spring-cloud
在配置文件里添加新的配置信息
# 不需要向Eureka注册注册
eureka.client.register-with-eureka=false
# 向Eureka注册中心获取服务
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:9696/eureka/
还有要在写在入口类的一个Bean 方法 加个注解
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
@Bean
@LoadBalanced //当扫描是发现这个注解 便会对他进行负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean //补充的修改E 的负载均衡策略
public IRule rule(){
return new RoundRobinRule();//设置轮训策略
}
}
注册中心和单机版的区别只有配置文件
集群版,有几个节点就有几个配置文件 通常3个就够啦
首先创建三个配置文件
application-eureka-1.properties
application-eureka-2.properties
application-eureka-3.properties
配置内容那 只有细小的区别
application-eureka-1.properties
server.port=1111
# 指定当前注册中心的服务名称
spring.application.name=eureka-registry
## 启用注册中心主动失效,并且每次主动失效检测间隔为5s 默认值60s
eureka.server.eviction-interval-timer-in-ms= 5000
## 设置eureka注册中心的响应更新时间
eureka.server.responseCacheUpdateIntervalMs=3000
eureka.server.responseCacheAutoExpirationInSeconds=60
eureka.server.expected-client-renewal-interval-seconds=60
eureka.server.renewal-percent-threshold=0.85
eureka.server.wait-time-in-ms-when-sync-empty=10000
## 配置注册中心的主机名
eureka.instance.instance-id = eureka-1
eureka.instance.hostname = Mysql01
## 服务刷新时间配置,每隔这个时间会主动心跳一次
eureka.instance.lease-renewal-interval-in-seconds= 5
## 服务提供者被认定为丢失心跳超时,失效多久后被删除
eureka.instance.lease-expiration-duration-in-seconds=15
## 配置定时获取|抓取注册中心的数据时间
eureka.client.registry-fetch-interval-seconds= 5
eureka.client.instance-info-replication-interval-seconds= 5
## 配置集群中其他eureka实例,用于本eureka实例的注册方。
eureka.client.region=beijing
eureka.client.availability-zones.beijing=zone-2,zone-3
eureka.client.service-url.zone-3=http://SparkTwo:1111/eureka/
eureka.client.service-url.zone-2=http://Mysql02:1111/eureka/
application-eureka-2.properties
server.port=1111
# 指定当前注册中心的服务名称
spring.application.name=eureka-registry
## 启用注册中心主动失效,并且每次主动失效检测间隔为5s 默认值60s
eureka.server.eviction-interval-timer-in-ms= 5000
## 设置eureka注册中心的响应更新时间
eureka.server.responseCacheUpdateIntervalMs=3000
eureka.server.responseCacheAutoExpirationInSeconds=60
eureka.server.expected-client-renewal-interval-seconds=60
eureka.server.renewal-percent-threshold=0.85
eureka.server.wait-time-in-ms-when-sync-empty=10000
## 配置注册中心的主机名
eureka.instance.instance-id = eureka-2
eureka.instance.hostname = Mysql02
## 服务刷新时间配置,每隔这个时间会主动心跳一次
eureka.instance.lease-renewal-interval-in-seconds= 5
## 服务提供者被认定为丢失心跳超时,失效多久后被删除
eureka.instance.lease-expiration-duration-in-seconds=15
## 配置定时获取|抓取注册中心的数据时间
eureka.client.registry-fetch-interval-seconds= 5
eureka.client.instance-info-replication-interval-seconds= 5
## 配置集群中其他eureka实例,用于本eureka实例的注册方。
eureka.client.region=beijing
eureka.client.availability-zones.beijing=zone-1,zone-3
eureka.client.service-url.zone-1=http://Mysql01:1111/eureka/
eureka.client.service-url.zone-3=http://SparkTwo:1111/eureka/
application-eureka-3.properties
server.port=1111
# 指定当前注册中心的服务名称
spring.application.name=eureka-registry
## 启用注册中心主动失效,并且每次主动失效检测间隔为5s 默认值60s
eureka.server.eviction-interval-timer-in-ms= 5000
## 设置eureka注册中心的响应更新时间
eureka.server.responseCacheUpdateIntervalMs=3000
eureka.server.responseCacheAutoExpirationInSeconds=60
eureka.server.expected-client-renewal-interval-seconds=60
eureka.server.renewal-percent-threshold=0.85
eureka.server.wait-time-in-ms-when-sync-empty=10000
## 配置注册中心的主机名
eureka.instance.instance-id = eureka-3
eureka.instance.hostname = SparkTwo
## 服务刷新时间配置,每隔这个时间会主动心跳一次
eureka.instance.lease-renewal-interval-in-seconds= 5
## 服务提供者被认定为丢失心跳超时,失效多久后被删除
eureka.instance.lease-expiration-duration-in-seconds=15
## 配置定时获取|抓取注册中心的数据时间
eureka.client.registry-fetch-interval-seconds= 5
eureka.client.instance-info-replication-interval-seconds= 5
## 配置集群中其他eureka实例,用于本eureka实例的注册方。
eureka.client.region=beijing
eureka.client.availability-zones.beijing=zone-1,zone-2
eureka.client.service-url.zone-1=http://Mysql01:1111/eureka/
eureka.client.service-url.zone-2=http://Mysql02:1111/eureka/
配置好后 将其打jar 包 上传到 linux 节点上
并将其在不同节点上分别启动
java -jar Eureka-Server.jar --spring.profiles.active=eureka-1
java -jar Eureka-Server.jar --spring.profiles.active=eureka-2
java -jar Eureka-Server.jar --spring.profiles.active=eureka-3
注意:
启动一个时会报错很正常因为其他的还没启动连接不上,都启动后就好了
然后可以分别主机号:端口号 在网页查看注册中心信息!
然后是注册实例
在单机版的基础上只需要改一个配置 ,其他的不用动
# 设置注册中心的 url 就是告诉实例往哪注册
eureka.client.service-url.defaultZone=http://SparkTwo:1111/eureka/,http://Mysql01:1111/eureka/,http://Mysql02:1111/eureka/
在idea 上直接启动 看能否注册上
有就ok
然后将其打包上传到节点 并启动
java -jar userModel.jar --eureka.instance.instance-id=002
eureka.instance.instance-id=002 可以覆盖配置里的id
进注册中心查看下是否有实例
提醒:
如果出现在idea 运行实例能在注册中心显示,但打包后运行却没效果,可能是jar 包的问题 ,看下打jar 的依赖上有没有指定版本和spring Boot 仲裁中心的版本不符 , 将版本去掉就可以了!
web 消费
在单机版的基础上只需要改一个配置 ,其他的不用动
# 设置注册中心的 url 就是告诉实例往哪注册
eureka.client.service-url.defaultZone=http://SparkTwo:1111/eureka/,http://Mysql01:1111/eureka/,http://Mysql02:1111/eureka/
然后打war 包 上传到节点
运行 测试