之前一章我们介绍了如何搭建Eureka Server,这一章,我们介绍如何搭建服务提供者。

  Eureka Clients介绍

  服务的提供者,通过发送REST请求,将自己注册到注册中心(在高可用注册中心的情况下,提供者会分别注册到两台注册中心)。注册完成之后,会维护一个心跳来实现服务续约,告诉注册中心自己还存活,以防止被注册中心剔除。

  搭建Eureka服务提供者

  1、配置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>com.SCClient</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>SCClient</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <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-test</artifactId>
<scope>test</scope>
</dependency>
<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>${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>

  2、配置application.yml

spring:
application:
name: client
server:
port: 11112
eureka:
client:
service-url:
defaultZone: http://localhost:11110/eureka,http://localhost:11111/eureka
instance:
lease-renewal-interval-in-seconds: 10
register-with-eureka: true

  配置项说明

  1. spring.application.name:用于配置应用名,该名称会作为注册中心中的服务名,调用者也通过调用该服务名实现服务的调用,不同服务不同服务名称,相同服务配置为同一个,不区分大小写。
  2. eureka.client.serverUrl.defaultZone:配置注册中心地址,多个以逗号隔开。
  3. eureka.client.instance.lease-renewal-interval-in-seconds:配置心跳续约周期时间间隔,默认为30秒。注册中心以该心跳来识别实例状态。
  4. eureka.client.register-with-eureka:默认为true,表示将自己注册到注册中心。

  3、配置启动项,使用@EnableEurekaClient开启服务发现(2.0版本中可以不用该注释也可实现功能)。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient
@SpringBootApplication
public class ScClientApplication { public static void main(String[] args) {
SpringApplication.run(ScClientApplication.class, args);
}
}

  4、创建Controller,提供服务。

  

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class ClientController {
@RequestMapping("/hello")
public String hello() {
return "hello this is client1";
}
}

  5、搭建多个相同服务的实例。该步骤不赘述,搭建方法和以上步骤一致,需要在application.yml中修改为不同端口号。

  6、启动多个实例,访问http://localhost:{port}/{mapurl}来查看服务实例是否正确启动(如http://localhost:11112/hello)。并在两个注册中心中,可以看到服务实例被注册。至此,服务实例搭建成功。

Spring Cloud Netflix之Eureka Clients服务提供者的更多相关文章

  1. SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用

    1.  Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...

  2. Spring Cloud Netflix之Eureka服务消费者

    Eureka服务消费者介绍 Eureka服务消费者用于发现服务和消费服务,发现服务通过Eureka Client完成,消费服务通过Ribbon完成,以实现负载均衡.在实际项目中,一个服务往往同时是服务 ...

  3. Spring Cloud Netflix之Eureka 相关概念

    为什么不应该使用ZooKeeper做服务发现 英文链接:Eureka! Why You Shouldn’t Use ZooKeeper for Service Discovery:http://www ...

  4. SpringCloud学习笔记(5)----Spring Cloud Netflix之Eureka的服务认证和集群

    1. Eureka服务认证 1. 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  5. SpringCloud学习笔记(4)----Spring Cloud Netflix之Eureka的配置

    1. Eureka监控配置 Eureka的客户端需要加入依赖 <dependency> <groupId>org.springframework.boot</groupI ...

  6. Spring Cloud Netflix Eureka源码导读与原理分析

    Spring Cloud Netflix技术栈中,Eureka作为服务注册中心对整个微服务架构起着最核心的整合作用,因此对Eureka还是有很大的必要进行深入研究. 本文主要分为四部分,一是对项目构建 ...

  7. Spring Cloud Netflix Eureka client源码分析

    1.client端 EurekaClient提供三个功能: EurekaClient API contracts are:* - provide the ability to get Instance ...

  8. Spring Cloud 系列之 Eureka 实现服务注册与发现

    如果你对 Spring Cloud 体系还不是很了解,可以先读一下 Spring Cloud 都有哪些模块 Eureka 是 Netflix 开源的服务注册发现组件,服务发现可以说是微服务架构的核心功 ...

  9. spring cloud连载第三篇之Spring Cloud Netflix

    1. Service Discovery: Eureka Server(服务发现:eureka服务器) 1.1 依赖 <dependency> <groupId>org.spr ...

随机推荐

  1. markdown的语法

    目录 md格式是一个纯文本格式,对于一个程序员来说,用md格式代替word.txt等格式用来写说明文档或者blog,目前github以及CSDN都支持md格式书写blog了. md格式的语法: 无序列 ...

  2. win10输入法问题,已禁止IME 问题解决

    第一种较为简单的解决方法: windows+R打开「运行」,然后打ctfmon,确定. 另外一种解法: windows的老bug了解决办法: 1. I. WIN + X 打开控制面板 -> 管理 ...

  3. liteos 异常接管(十五)

    1 概述 1.1 基本概念 异常接管是操作系统对在运行期间发生异常的情况进行处理的一系列动作,譬如打印异常发生时当前函数调用栈信息. cpu现场信息.任务的堆栈情况等. 异常接管作为一种调测手段,可以 ...

  4. AtCoder - 2286 (数论——唯一分解定理)

    题意 求n!的因子数%1e9+7. 思路 由唯一分解定理,一个数可以拆成素数幂之积,即2^a * 3^b *……,n!=2*3*……*n,所以计算每个素因子在这些数中出现的总次数(直接对2~n素因子分 ...

  5. vue中显示原网页代码--codemirror

    在项目中遇到了一个需求,后台返回string类型的html源码,要求前端这边按照codeview这种类型把这个源码展示出来.现总结如下 1.如果没啥样式的需求,只是要求该缩进缩进的话,可以直接使用in ...

  6. Dockerfil

    Dockerfile简介 dockerfile 是一个文本格式的配置文件, 用户可以使用 Dockerfile 来快速创建自定义的镜像,另外,使用Dockerfile去构建镜像好比使用pom去构建ma ...

  7. Node.js 获取本机Mac地址

    首先我们要先加载一个包用于获取mac地址 npm install getmac 加载完毕会在node_modules文件夹下发现一个getmac文件夹,我们把对应的路径加载到程序中 源码如下: var ...

  8. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) G. Polygons 数论

    G. Polygons Description You are given two integers

  9. multer 文件后缀名

    我的代码是这样写的. var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uplo ...

  10. LeetCode 232:用栈实现队列 Implement Queue using Stacks

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...