SpringCloud的Eureka注册中心

时间:2019-09-16
本文章向大家介绍SpringCloud的Eureka注册中心,主要包括SpringCloud的Eureka注册中心使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务。

Eureka包含两个组件:Eureka Server和Eureka Client。

内容来自 : https://www.jianshu.com/p/c18d140ad9f6

 

可以使用 IDEA 的 spring initializr 来新建这种 微服务的项目。

 

 

 

添加依赖。

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

 

在启动类,加注解  @EnableEurekaServer,使用eureka。


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

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}

 

添加配置 , 名称和port是基础配置,在多个服务组成的微服务项目中,是必须的。

spring:
  application:
    name: eureka

server:
  port: 8761

eureka服务server端的配置。在实际的项目中,为了搞可用,会使用多个 eureka 注册中心。

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: false
  • service-url是一个map。而且指定了 defaultZone 。按住ctrl,鼠标点击 service-url ,在源码里可以找到 规则。

    private Map<String, String> serviceUrl = new HashMap();
    
    this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
        
    
        public void setServiceUrl(Map<String, String> serviceUrl) {
            this.serviceUrl = serviceUrl;
        }

     

  •  register-with-eureka 默认是 true,表示会被 eureka发现。false,表示不在  http://localhost:8761/eureka/  这个注册中心上发现自己。

 

启动

 

 

完整的pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jin</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

原文地址:https://www.cnblogs.com/wuyicode/p/11528655.html