springcloud笔记之eureka服务注册与发现

一、eureka是什么?

官方解释:
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

它的实现原理是这样的,eureka提供一个注册中心,这个注册中心就是用来注册服务的,服务提供者向注册中心提供服务(服务提供者配置eureka注册中心地址),然后消费者也不是直接从服务提供者拿到服务,消费者需要先从eureka注册中心拿到服务的数据,然后再对服务提供者进行远程的调用。

说的简单点

  • 提供者注册服务到eureka
  • 消费者从eureka拿到服务
  • 调用服务提供者

二、使用步骤

在这里插入图片描述

1.引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

2.编写配置文件

server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: eureka7001.com #Eureka服务的实例名称
  client:
    register-with-eureka: false #表示是否向注册中心注册自己
    fetch-registry: false #false表示自己为注册中心
    service-url: #监控页面
      #单机:
      defaultZone: http://${
    
    eureka.instance.hostname}:${
    
    server.port}/eureka/

3.开启主启动类

package com.lhh;

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

@SpringBootApplication
@EnableEurekaServer// Eureka服务端启动
public class EurekaServer_7001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(EurekaServer_7001.class, args);
    }
}

但是我一开启springboot主启动类,发现遇见了一个问题,报错内容如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-05 11:10:24.834 ERROR 21968 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1

根据网友的回答,报错原因是因为应用中没有配置datasource的一些相关属性,例如:地址值啊,数据库驱动啊,用户名啊,密码等等,但是为了省事,可以在springboot的主启动类上加入**@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})**代表忽略关于数据源的配置。

加上注解那么再次启动之后,访问localhost:7001就可以发现,成功进入了eureka服务注册与中心的页面。
在这里插入图片描述

总结

其实现在已经到了springcloud阶段的学习了,学起来会越来越轻松,基本上加一些依赖,写一点配置文件,在启动类上添加对应的注解,任务基本上就完成了。

看完恭喜你,又知道了一点点!

你知道的越多,不知道的越多!

感谢您的阅读,你的关注和评论,是对我学习的最大的支持,加油,陌生人,一起努力。

猜你喜欢

转载自blog.csdn.net/qq_41486775/article/details/114385464