SpringCloud教程一:Eureka(服务注册与发现)

一、简介

EureKa在Spring Cloud全家桶中担任着服务的注册与发现的落地实现。Netflix在设计EureKa时遵循着AP原则,它基于R 
EST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,功能类似于Dubbo的注册中心Zookeeper。

二、原理

EureKa采用C-S的设计架构,即包括了Eureka Server(服务端),EureKa client(客户端)。
1.EureKa Server 提供服务注册,各个节点启动后,在EureKa server中进行注册;

2 EureKa Client 是一个Java客户端,用于和服务端进行交互,同时客户端也是一个内置的默认使用轮询负载均衡算法的负载均衡器。在应用启动后,会向Eueka Server发送心跳(默认30秒)。如果EUR额卡 Server在多个心跳周期内没有接受到某个节点的心跳,EureKa Server将会从服务注册表中将这个服务移出(默认90秒)。
三、实践

1、创建一个Project

2、右键Project---new Module

3、选择Spring Cloud Discovery-----Eurka Server

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 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.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ma</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RC2</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

4、将resource下application.properties改成application.yml(yml更简洁)

      yml配置如下:

       默认情况下erureka server也是一个eureka client ,必须要指定一个 server,eureka.client.registerWithEureka:false和fetchRegistry:false来表明是一个eureka server.

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
View Code

5、启动服务注册中心

  在EurekaServerApplication中添加注解:@EnableEurekaServer

package com.eureka.eurekaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}
View Code

 

5、启动Module,访问http://localhost:8761/

 

 

 表明没有服务注册。

6、创建一个服务提供者(Eureka Client)

创建过程类似Eureka Server,当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

yml:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: eureka-client

EurekaClientApplication:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {

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

    @Value("${server.port}")
    String port;
    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi "+name+",i am from port:" +port;
    }

}

spring.application.name,服务与服务之间相互调用都是根据这个name 。 启动client工程,打开 eureka server 的网址:

http://localhost:8761/

 

 

 

发现一个服务已经注册在服务中了,服务名为RUREKA-CLIENT ,端口为8762

这时打开 http://localhost:8762/hello?name=eurekaclient ,你会在浏览器上看到 :

四、参考资料

SpringCloud eureka server官方

SpringCloud eureka clients官方

五、源码

加入qq群:Spring全家桶技术交流①群196165973,免费获取源码。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-11-19 15:56  吾码的博客  阅读(733)  评论(0编辑  收藏  举报