多语言展示
当前在线:548今日阅读:103今日分享:49

SpringCloud中Eureka中如何发现服务?

Spring Cloud实战开发系列课程-分布式开发简介是系列课程第十一课。Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring没有重复制造轮子,它只开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
工具/原料
1

spring cloud

2

spring boot

3

restful

4

maven

5

intellij idea

6

Eureka

方法/步骤
1

在实际的项目运行过程之中需要通过Eureka作为所有微服务的监控处理程序,但是对于监控程序那么就必然要面临以下的问题。1、新服务追加的时候应该立刻可以进行注册;2、当某个服务下线之后应该可以进行清理;

2

【microcloud-eureka-7001】目前的设置Eureka一直报一个错误,因为Eureka自己写需要注册到Eureka服务上去。server:  port: 7001eureka:  instance: #eureka实例定义    hostname: eureka-7001.com #定义Eureka实例所在的主机名称  client: #客户端进行Eureka注册的配置      service-url:         defaultZone: http://eureka-7001.com:7001/eureka      register-with-eureka: false #当前的微服务不注册到eureka之中      fetch-registry: false spring:  application:    name: microcloud-eureka-7001

3

【microcloud-eureka-7001】设置服务的清理间隔,修改application.yml配置文件。server:  port: 7001eureka:  instance: #eureka实例定义    hostname: eureka-7001.com #定义Eureka实例所在的主机名称  server:    eviction-interval-timer-in-ms: 1000 #设置清理的间隔时间,而后这个时间使用的毫秒单单位(默认是60秒)  client: #客户端进行Eureka注册的配置      service-url:         defaultZone: http://eureka-7001.com:7001/eureka      register-with-eureka: false #当前的微服务不注册到eureka之中      fetch-registry: false  #不通过eureka获取注册信息spring:  application:    name: microcloud-eureka-7001

4

一旦配置了清理的间隔为1秒的时间,则会在每秒的时候进行一次服务的清理过程,会出现如下“ AbstractInstanceRegistry  - Running the evict task with compensationTime 0ms”错误信息。该配置不建议进行修改,默认就是60秒,也就是说你的微服务如果60秒没有心跳了,那就认为可以清理掉了。

5

【microcloud-eureka-7001】在Eureka里面有一个问题,这个问题就是它默认支持有保护模式,的概念,所谓的保护模式就是即便现在某一个微服务不可用了,eureka不会清理,依然会进行该微服务信息的保存。如果现在要想去改变这种保护模式的启用,则可以修改application.yml配置文件:server:  port: 7001eureka:  instance: #eureka实例定义    hostname: eureka-7001.com #设置清理的间隔时间,而后这个时间使用的毫秒单单位(默认是60秒)  client: #客户端进行Eureka注册的配置      service-url:         defaultZone: http://eureka-7001.com:7001/eureka      register-with-eureka: false #当前的微服务不注册到eureka之中      fetch-registry: false  server:    enable-self-preservation: false #是否要设置成保护模式      eviction-interval-timer-in-ms: 60000  #不通过eureka获取注册信息spring:  application:    name: microcloud-eureka-7001

6

理论上只有关闭了保护模式之后才可以进行无效微服务的清理操作,但是很多时候Eureka里面也会自带清除过程。

8

由于所有的服务都注册到了Eureka之中,这样如果配置了“lease-expiration-duration-in-seconds'此选项,标识距离上一次发送心跳之后等待下一次发送心跳的间隔时间,如果超过了此间隔时间,则认为该微服务已经宕机了。

9

【microcloud-provider-dept-8001】现在对于注册到Eureka上的微服务端也可以通过发现服务来进行一些服务信息的获取,修改DeptRest程序类,追加一个控制调用方法:package com.gwolf.microcloud.rest;import com.gwolf.microcloud.service.IDeptService;import com.gwolf.vo.Dept;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;@RestControllerpublic class DeptRest {    @Resource    private IDeptService deptService;        @Resource    private DiscoveryClient client; //进行Eureka的发现服务        @RequestMapping('/dept/discover')    public Object discover() {//直接返回发现服务信息        return this.client;    }        @RequestMapping('/dept/id')    public Object id(HttpServletRequest request) {        return request.getSession().getId();    }    @RequestMapping(value = '/dept/get/{id}',method = RequestMethod.GET)    public Object get(@PathVariable('id') long id) {        return this.deptService.get(id);    }    @RequestMapping(value = '/dept/add',method = RequestMethod.POST)    public Object add(@RequestBody Dept dept) {        return this.deptService.add(dept);    }    @RequestMapping(value = '/dept/list',method = RequestMethod.GET)    public Object list() {        return this.deptService.list();    }}

10

【microcloud-provider-dept-8001】在主程序之中,启用Eureka的发现服务。package com.gwolf.microcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class Dept_8001_StartSpringCloudApplication {    public static void  main(String[] args) {        SpringApplication.run(Dept_8001_StartSpringCloudApplication.class,args);    }}

11

输入访问地址:http://dept-8001.com:8001//dept/discover

12

Eureka里面就是根据这些信息来进行应用列表显示的。

注意事项

SpringCloud实战开发系列课程第十课请参考:https://jingyan.baidu.com/article/d2b1d102c6af805c7f37d478.html

推荐信息