Spring Cloud 服务注册与发现

Spring Cloud Netflix  的 Eureka (服务注册和发现模块)

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <parent>
        <groupId>com.funtl</groupId>
        <artifactId>hello-spring-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
    </parent>
 
    <artifactId>hello-spring-cloud-eureka</artifactId>
    <packaging>jar</packaging>
 
    <name>hello-spring-cloud-eureka</name>
    <inceptionYear>2018-Now</inceptionYear>
 
    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->
 
        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.lxq.hello.spring.cloud.eureka.EurekaApplication</mainClass> 
<!--mainClass的路径为SpringBoot启动类-->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在Pom.xml中配置好、启动一个服务中心只需一个注解
在SpringBootApplication.java  类上启用  @EnableEurekaServer 注解

package com.lxq.hello.spring.cloud.eureka;

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

/**
 * @Author Lxq
 * @Date 2020/3/29 12:20
 * @Version 1.0
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

Eureka 是一个高可用的组件,它没有后端缓存,每个实例注册之后需要向注册中心发送心跳(可以在内存中完成),在默认情况下
Eureka Server 也是一个Eureka Client,必须要指定一个Server

Eureka是一个服务 所以需要在application.yml文件中配置一个Eureka的服务

application.yml文件

spring:
  application:
    name: hello-spring-cloud-eureka   
 
server:
    port: 8761
 
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false   # registerWithEureka 和 fetchRegistry 为false 代表Eureka此时
    fetchRegistry: false        # 是服务  
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值