Eureka服务注册与发现

Eureka服务注册与发现

概述:

学习springcloud的分布式和微服务架构,需要了解springcloud中五个核心的部分

Spring Cloud 五大组件

服务注册与发现——Netflix Eureka

负载均衡:

  • 客户端负载均衡——Netflix Ribbon

  • 服务端负载均衡:——Feign(其也是依赖于Ribbon,只是将调用方式RestTemplete 更改成Service 接口)

断路器——Netflix Hystrix

服务网关——Netflix Zuul

分布式配置——Spring Cloud Config

什么是Eureka

  • Springcloud 封装了Netflix公司开发的Eureka模块来实现服务注册与发现 (对比Zookeeper).
  • Eureka采用了C-S的架构设计,EurekaServer作为服务注册功能的服务器,他是服务注册中心.

在springcloud中配置Eureka

导入该微服务所需要的相关的依赖

出错点:在导入springcloud-Eureka的包的时候会出现版本的冲突问题需要在该模块的pom文件中来指定对应的版本号以自己的案例为例子:1.4.6.RELEASE(我导入的Eureka的版本号)

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

出错点二:配置文件中需要加入一定的配置idea中没有提示需要手动的来输入,如果在没有配置的情况下可能会出现

如下的错误信息。

eureka:
client:
 service-url:
   default: http://127.0.0.1:8761/eureka
 healthcheck:
   enabled: true

APPLICATION FAILED TO START


Description:

Parameter 3 of method eurekaRegistration in org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration required a bean of type ‘com.netflix.appinfo.HealthCheckHandler’ that could not be found.

  • Bean method ‘eurekaHealthCheckHandler’ not loaded because @ConditionalOnProperty (eureka.client.healthcheck.enabled) did not find property ‘eureka.client.healthcheck.enabled’

Action:

Consider revisiting the conditions above or defining a bean of type ‘com.netflix.appinfo.HealthCheckHandler’ in your configuration.

创建该微服务的步骤

新建一个模块springcloud-Eureka-7001

编写对应的配置文件

server:
  port: 7001

# Eureka配置
eureka:
  instance:
    # Eureka服务端的实例名字
    hostname: 127.0.0.1
  client:
    # 表示是否向 Eureka 注册中心注册自己(这个模块本身是服务器,所以不需要)
    register-with-eureka: false
    # fetch-registry如果为false,则表示自己为注册中心,客户端的化为 ture
    fetch-registry: false
    # Eureka监控页面~
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    healthcheck:
      enabled: true

编写启动类使用Eureka的对应注解将其注入到springboot的容器中

package com.kuang.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableEurekaServer// @EnableEurekaServer 服务端的启动类,可以接受别人注册进来
public class EuServer7001 {
    public static void main(String[] args) {
        SpringApplication.run(EuServer7001.class,args);
    }
}

开启此微服务进行测试观察Eureka的对应界面访问localhost:7001端口进行访问(进入Eureka的页面进行访问)

在这里插入图片描述

Eureka实现服务注册

将springcloud-provider-dept8001这个微服务注册到Eureka并通过后台的管理界面进行显示

8001微服务之中导入对应的依赖

<!--Eureka依赖-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

在8001的配置文件中进行配置(下面为添加的配置信息)

eureka:
  client:
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka/
    healthcheck:
      enabled: true

在主启动类添加@EnableEurekaClient注解并对其进行实现先开启8001对应的微服务然后在开启7001对应的Eureka对应的微服务信息

package com.kuang.springcloud;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

//启动类
@SpringBootApplication
@EnableEurekaClient
public class DeptApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeptApplication.class, args);
    }
}

启动服务观察对应的结果信息

先启动7001对应的微服务之后在启用8001对应的微服务在后台的管理页面中观察注册的信息

在这里插入图片描述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rdYsSVDh-1632108734049)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20210920104620689.png)]
服务保护的机制:当一个注册的服务停止掉之后(例如停掉位于9000端口的8001微服务后),后台的管理页面回出现对应的信息

即出现红色的提示信息

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
配置微服务的监控信息
  • 在pom文件中引入相关的依赖
<!--actuator完善监控信息-->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

  • 在application.yaml中添加相关的配置信息

    # info配置
    info:
    # 项目的名称
    app.name:
    # 公司的名称
    company.name:  
    
  • 进入监控页面查看相应的信息

注册进来的服务获取相关的信息(服务的发现)

在springcloud-dept-8001这个微服务的下面注册相关的信息。是这个微服务可以在团队的注册中被发现。

在控制器中增加相应的对应的方法(在增加对应的方法是需要先注入对应的接口的信息)

package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {

    /**
     * DiscoveryClient 可以用来获取一些配置的信息,得到具体的微服务!
     */
    @Autowired
    private DiscoveryClient client;
    //注入对应的接口
    @Autowired
    private DeptServiceimpl deptServiceimpl;

    @GetMapping("/dept/add")
    public boolean addDept(Dept dept) {
        return deptServiceimpl.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return deptServiceimpl.queryByid(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> queryall() {
        System.out.println("接收到请求消息");
        return deptServiceimpl.queryAll();
    }
    /**
     * 获取一些注册进来的微服务的信息~,
     *
     * @return
     */
    @GetMapping("/dept/discovery")
    public Object discovery() {
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:" + services);
        // 得到一个具体的微服务信息,通过具体的微服务id,applicaioinName;
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" + // 主机名称
                            instance.getPort() + "\t" + // 端口号
                            instance.getUri() + "\t" + // uri
                            instance.getServiceId() // 服务id
            );
        }
        return this.client;
    }

}

主启动类中加入@EnableDiscoveryClient 注解访问对应的后端的接口信息完成对应的信息的发现
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7W557ocr-1632108734052)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20210920112946155.png)]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序小旭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值