前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud 入门教程1、服务注册与发现(Eureka)

Spring Cloud 入门教程1、服务注册与发现(Eureka)

作者头像
wuweixiang
发布2018-12-24 11:41:53
5150
发布2018-12-24 11:41:53
举报
文章被收录于专栏:吴伟祥吴伟祥

一、前言

1、什么是Eureka?

Eureka是Netflix开源的服务注册与发现框架,Eureka由两个组件组成:Eureka服务器和Eureka客户端。

Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。

Eureka客户端是一个Java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。

2、Eureka概览

Eureka服务注册与发现
Eureka服务注册与发现

行为

ken.io 的说明

1. Registry

服务启动时,Eureka客户端会将Service(ServiceName、IP、Port)注册到Eureka Server

2. Query

应用通过Eureka客户端调用注册在Eureka Server的服务时,会通过ServiceName查询到服务实例列表

3. Call

根据拿到的服务实例信息,通过负载均衡或其他策略访问指定服务实例

3、本篇环境信息

框架

版本

Spring Boot

2.0.0.RELEASE

Spring Cloud

Finchley.BUILD-SNAPSHOT

JDK

1.8.x

二、搭建Eureka Server

1、创建Eureka Server项目

  • 创建一个Spring Boot工程

使用maven-archtype-quickstart模板创建项目

IDEA Maven QuickStart 创建项目
IDEA Maven QuickStart 创建项目

说明

GroupId

io.ken.springcloud.eurekaserver

ArtifactId

eurekaserver

  • 在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>

    <groupId>io.ken.springcloud.eurekaserver</groupId>
    <artifactId>eurekaserver</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>eurekaserver</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.BUILD-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <build>
        <finalName>eurekaserver</finalName>
    </build>

</project>

2、配置EurekaServer启动类

修改\src\main\java\io\ken\springcloud\eurekaserver\App.java 基于Spring Boot创建启动类,添加上 @EnableEurekaServer 注解

package io.ken.springcloud.eurekaserver;

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


@EnableEurekaServer
@SpringBootApplication
public class App {

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

3、配置Eureka Server

在\src\main下创建文件夹resources文件夹并设置为Resources Root

在resources文件夹下创建application.yml文件并配置Eureka Server

server:
  port: 8800

spring:
  application:
    name: EurekaServer

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

Eureka默认会以客户端来注册自己,所以registerWithEureka、fetchRegistry要配置为false。

4、Eureka Server信息预览

启动Eureka Server项目后,访问:http://localhost:8800/ ,可以看到下面的页面,其中还没有发现任何服务。

Eureka Server Portal
Eureka Server Portal

三、搭建服务并注册(Eureka Client)

1、创建Eureka Client项目

  • 创建一个Spring Boot工程

使用maven-archtype-quickstart模板创建项目

IDEA Maven QuickStart 创建项目
IDEA Maven QuickStart 创建项目

说明

GroupId

io.ken.springcloud.helloservice

ArtifactId

helloservice

  • 在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>

    <groupId>io.ken.springcloud.helloservice</groupId>
    <artifactId>helloservice</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>helloservice</name>
    <url>http://ken.io</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.BUILD-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <build>
        <finalName>helloservice</finalName>
    </build>

</project>

2、配置Eureka Client启动类

修改\src\main\java\io\ken\springcloud\helloservice\App.java 基于Spring Boot创建启动类,添加上 @EnableDiscoveryClient 注解

package io.ken.springcloud.helloservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class App {

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

3、创建测试服务

在\src\main\java\io\ken\springcloud\helloservice创建package:controller 然后创建HelloController.java

package io.ken.springcloud.helloservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    DiscoveryClient discoveryClient;

    @RequestMapping("/")
    public Object index() {
        return "hello service";
    }

    @RequestMapping("/info")
    public Object info() {
        return discoveryClient.getServices();
    }
}

4、配置Eureka Client

在\src\main下创建文件夹resources文件夹并设置为Resources Root

在resources文件夹下创建application.yml文件并配置Eureka Client

server:
  port: 8601

spring:
  application:
    name: helloservice

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8800/eureka/

5、服务注册&访问测试

HelloService项目成功启动后,访问 http://localhost:8601 ,或者 http://localhost:8601/info 查看是否正常启动

访问 http://localhost:8800 ,会发现服务已注册

eureka instance registered
eureka instance registered

搞定!

四、备注

  • 本篇示例代码

https://github.com/ken-io/springcloud-course/tree/master/chapter-01

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、什么是Eureka?
  • 2、Eureka概览
  • 3、本篇环境信息
  • 二、搭建Eureka Server
    • 1、创建Eureka Server项目
      • 2、配置EurekaServer启动类
        • 3、配置Eureka Server
          • 4、Eureka Server信息预览
          • 三、搭建服务并注册(Eureka Client)
            • 1、创建Eureka Client项目
              • 2、配置Eureka Client启动类
                • 3、创建测试服务
                  • 4、配置Eureka Client
                    • 5、服务注册&访问测试
                    • 四、备注
                    相关产品与服务
                    负载均衡
                    负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档