Spring Cloud之Eureka与Spring Boot项目的集成 (单机版和集群版)

Eureka与Spring Boot的集成

单机版

首先: 创建一个新的项目 作为Eureka的注册中心服务器

需要导入的有

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
  </parent>

<!--  Spring Cloud 的主坐标-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
  </dependencies>

创建入口类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/ 查看注册中心情况
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200330193756752.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl
补充下计算期望心跳值 的方法
一分钟内 能收到几个心跳 然后 乘于 自我保护机制触发比例 取整数
以本期配置的举例
60 / 20 =3
3 * 0.85= 2.55 取整 就是为 2

下面就是设置服务实例

首先还是要添加的依赖

 <!--  Spring Cloud 的主坐标-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

<!--    注册服务依赖-->
    <!--集成Eureka-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <!--    SpringBoot 健康检查的依赖-->
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </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/

成功后启动 是可以在 http://localhost:9696/ 看到实例的
在这里插入图片描述

最后便是 web 消费者

首先还是添加依赖

 <!--  Spring Cloud 的主坐标-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

<!--    注册服务依赖-->
    <!--集成Eureka-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <!--    SpringBoot 健康检查的依赖-->
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

提醒: 注意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 包 上传到节点

运行 测试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值