Eureka service registration and discovery of springcloud notes

1. What is eureka?

Official explanation:
Eureka is a service discovery framework developed by Netflix. It is a REST-based service and is mainly used to locate middle-tier services running in the AWS domain to achieve load balancing and middle-tier service failover. SpringCloud integrates it in its sub-project spring-cloud-netflix to realize the service discovery function of SpringCloud.

Its implementation principle is like this, eureka provides a registry, this registry is used to register services, the service provider provides services to the registry (the service provider configures the eureka registry address), and then consumers are not directly from When the service provider gets the service, the consumer needs to get the service data from the eureka registry first, and then make a remote call to the service provider.

Keep it simple

  • Provider registration service to eureka
  • Consumers get services from eureka
  • Call service provider

Second, use steps

Insert picture description here

1. Introduce dependencies

<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. Write a configuration file

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. Open the main startup class

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);
    }
}

But as soon as I started the springboot main startup class, I found that I encountered a problem, and the error content was as follows:

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

According to the answers of netizens, the reason for the error is that some related attributes of the datasource are not configured in the application, such as: address value, database driver, user name, password, etc., but in order to save trouble, you can add it to the main startup class of springboot ** @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})**Means ignore the configuration of the data source.

After adding a comment, after restarting, localhost:7001you can find that you have successfully entered the page of eureka service registration and center.
Insert picture description here

to sum up

In fact, I have come to the springcloud stage of learning. It will become easier and easier to learn. Basically add some dependencies, write a little configuration file, and add corresponding annotations to the startup class. The task is basically completed.

Congratulations after reading, I know a little bit again!

The more you know, the more you don't know!

Thank you for reading, your attention and comments are the greatest support for my study, come on, strangers, work hard together.

Guess you like

Origin blog.csdn.net/qq_41486775/article/details/114385464