Spring Cloud (2): Eureka Server Registration Center

1. What is Eureka

Eureka is Netflix's open source REST-based service governance solution. Spring Cloud integrates Eureka to provide service governance and service discovery functions, and can be easily integrated with microservice applications built on Spring Boot.

2. The composition of Spring Cloud Eureka

  1. Eureka Server, Registry

  2. Eureka Client, all microservices to be registered connect to Eureka Server through Eureka Client to complete registration

Three, actual combat! Quickly build a registration center

  1. Create a parent project, the pom.xml configuration is as follows
<parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>2.0.7.RELEASE</version>
  </parent>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  </dependencies>
  
  <dependencyManagement>
  	<dependencies>
  		<dependency>
  			<groupId>org.springframework.cloud</groupId>
  			<artifactId>spring-cloud-dependencies</artifactId>
  			<version>Finchley.SR2</version>
  			<type>pom</type>
  			<scope>import</scope>
  		</dependency>
  	</dependencies>
  </dependencyManagement>
  1. Create a sub-project Module under the parent project, and the pom.xml configuration is as follows
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>
  1. Create application.yml under the subproject and add Eureka related configuration as follows
server:
  # 当前Eureka Server服务端口
  port: 8761
eureka:
  client:
    # 是否将当前的Eureka Server服务作为客户端进行注册
    register-with-eureka: false
    # 是否获取其他Eureka Server服务的数据
    fetch-registry: false
    
    service-url:
      # 注册中心的访问地址
      defaultZone: http://localhost:8761/eureka/

property description

* server.port:当前 Eureka Server 服务端口

* eureka.client.register-with-eureka:是否将当前的 Eureka Server 服务作为客户端进行注册

* eureka.client.fetch-registry:是否获取其他 Eureka Server 服务的数据

* eureka.client.service-url.defaultZone:注册中心的访问地址
  1. Create a startup class
package com.frr;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekServerApplication {
    
    
	public static void main(String[] args) throws Exception {
    
    
		SpringApplication.run(EurekServerApplication.class, args);
	}

}

Notes

* @SpringBootApplication:声明该类是 Spring Boot 服务的入口

* @EnableEurekaServer:声明该类是一个 Eureka Server 微服务,提供服务注册和服务发现的功能,即注册中心
  1. The interface after successful startup
    insert image description here

Four. Summary

  1. Create a new parent project and add public dependencies to the pom file

  2. Create a subproject Module in the parent project, and add the component dependencies you need in the pom file of the Module

  3. Add its port and Eureka-related configuration in Spring Boot

  4. Finally, create a startup class, add annotations to the startup class, and make the current project a Eureka Server

From a business point of view, the services we register with the service center can be divided into service providers and service consumers. So how do service providers provide services to other services? Let us look forward to the next article Spring Cloud (3): Eureka Client service provider

One development engineer is also in the continuous learning stage, and the usual small experiences are shared from time to time. I hope that those who read the text I wrote can avoid detours and wish you success in work and study.
Bloggers have limited experience, if there are any shortcomings, welcome to communicate and improve together~ I hope to make progress together with you who are also in CSDN.

Author | Sweet Little Sweet Potato
Produced | Little Sweet Potato

Guess you like

Origin blog.csdn.net/qq_36836370/article/details/130870018