之前一章我们介绍了如何搭建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. Django 的ORM 表间操作

    Django之ORM表间操作   之前完成了简单的数据库数据增加操作.这次学习更多的表间操作. 单表操作 增加 方式一 b = Book(title="Python基础", pub ...

  2. Linux 解决Deepin深度系统无法在root用户启动Google Chrome浏览器的问题

    解决Deepin无法在root用户启动Google Chrome浏览器的问题,步骤如下. 前提:如何用root用户登录系统?编辑 vim /etc/lightdm/lightdm.conf , 找到并 ...

  3. python pip 升级 或者换源

    1. 临时换源python -m pip install --upgrade pip -i https://pypi.douban.com/simple pip国内的一些镜像   阿里云  https ...

  4. 5-4 可视化库Seaborn-回归分析

    In [2]: %matplotlib inline import numpy as np import pandas as pd from scipy import stats,integrate ...

  5. 游戏设计模式——C++单例类

    前言: 本文将探讨单例类设计模式,单例类的懒汉模式/饿汉模式,单例类的多线程安全性,最后将利用C++模板减少单例类代码量. 本文假设有一个Manager管理类,并以此为探究单例类的设计模式. 懒汉模式 ...

  6. 全网最新方法:Win10下如何安装和搭建appium自动化测试环境

    为了方便大家,下面是本人安装和搭建appium所需要的软件,自取. 链接:https://pan.baidu.com/s/1wl5Xnos4BmnSZsBRd6B5YA#list/path=%2F  ...

  7. 【Eureka篇三】Eureka常用配置说明(7)

    服务注册中心配置(Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean) #关闭注册中心的保护机制, ...

  8. JDOJ3011 铺地板III

    JDOJ3011 铺地板III https://neooj.com/oldoj/problem.php?id=3011 题目描述 有3 x N (0 <= N <= 105)的网格,需要用 ...

  9. jQuery3.0+报错Uncaught TypeError: e.indexOf is not a function

    jQuery3.0+报错Uncaught TypeError: e.indexOf is not a function 使用.load()绑定事件时报错,Uncaught TypeError: e.i ...

  10. Codeforces Round #573 (Div. 1)

    Preface 军训终于结束了回来补一补之前的坑发现很多题目题意都忘记了 这场感觉难度适中,F由于智力不够所以弃了,E的话石乐志看了官方英文题解才发现自己已经胡了一大半就差实现了233 水平下降严重. ...