Spring Cloud Netflix之Eureka Clients服务提供者
之前一章我们介绍了如何搭建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
配置项说明
- spring.application.name:用于配置应用名,该名称会作为注册中心中的服务名,调用者也通过调用该服务名实现服务的调用,不同服务不同服务名称,相同服务配置为同一个,不区分大小写。
- eureka.client.serverUrl.defaultZone:配置注册中心地址,多个以逗号隔开。
- eureka.client.instance.lease-renewal-interval-in-seconds:配置心跳续约周期时间间隔,默认为30秒。注册中心以该心跳来识别实例状态。
- 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服务提供者的更多相关文章
- SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用
1. Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...
- Spring Cloud Netflix之Eureka服务消费者
Eureka服务消费者介绍 Eureka服务消费者用于发现服务和消费服务,发现服务通过Eureka Client完成,消费服务通过Ribbon完成,以实现负载均衡.在实际项目中,一个服务往往同时是服务 ...
- Spring Cloud Netflix之Eureka 相关概念
为什么不应该使用ZooKeeper做服务发现 英文链接:Eureka! Why You Shouldn’t Use ZooKeeper for Service Discovery:http://www ...
- SpringCloud学习笔记(5)----Spring Cloud Netflix之Eureka的服务认证和集群
1. Eureka服务认证 1. 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...
- SpringCloud学习笔记(4)----Spring Cloud Netflix之Eureka的配置
1. Eureka监控配置 Eureka的客户端需要加入依赖 <dependency> <groupId>org.springframework.boot</groupI ...
- Spring Cloud Netflix Eureka源码导读与原理分析
Spring Cloud Netflix技术栈中,Eureka作为服务注册中心对整个微服务架构起着最核心的整合作用,因此对Eureka还是有很大的必要进行深入研究. 本文主要分为四部分,一是对项目构建 ...
- Spring Cloud Netflix Eureka client源码分析
1.client端 EurekaClient提供三个功能: EurekaClient API contracts are:* - provide the ability to get Instance ...
- Spring Cloud 系列之 Eureka 实现服务注册与发现
如果你对 Spring Cloud 体系还不是很了解,可以先读一下 Spring Cloud 都有哪些模块 Eureka 是 Netflix 开源的服务注册发现组件,服务发现可以说是微服务架构的核心功 ...
- spring cloud连载第三篇之Spring Cloud Netflix
1. Service Discovery: Eureka Server(服务发现:eureka服务器) 1.1 依赖 <dependency> <groupId>org.spr ...
随机推荐
- 分析-flag被盗
用wireshark打开数据包 寻找http协议的数据 找一个post包进行TCP追踪流 寻找flag
- sudo apt-get 与 yum 常用命令
yum -RedHat:CentOS... -xxx.rpmsudo apt-get -Debian:Ubuntu... -xxx.deb 安装工具rpm -ivh yum-2.0.4-1.rh ...
- zz传统方法和深度学习结合的感知策略探索
今天分享下 Pony.ai 在感知探索的过程中,使用的传统方法和深度学习方法.传统方法不代表多传统,深度学习也不代表多深度.它们都有各自的优点,也都能解决各自的问题.我们希望发挥它们的优点,并且结合起 ...
- 玩转算法系列--图论精讲 面试升职必备(Java版)
第1章 和bobo老师一起,玩转图论算法欢迎大家来到我的新课程:<玩转图论算法>.在这个课程中,我们将一起完整学习图论领域的经典算法,培养大家的图论建模能力.通过这个课程的学习,你将能够真 ...
- x3
#include<stdio.h> int main() { char ch; printf("输入一个字符:\n"); scanf("%c",&a ...
- 【Java语言特性学习之六】扩展知识点
一.SPI机制 二.注解处理机制 三.java native关键字 https://www.cnblogs.com/KingIceMou/p/7239668.html
- CocoaPods 升级1.8.4的坑 CDN: trunk Repo update failed
之前升级了cocoaPods 版本1.8.4,今天pod install,然后问题就来了: 1.出现了下边的问题: Adding spec repo `trunk` with CDN `https:/ ...
- 某企业用友U8+中勒索病毒后数据修复及重新实施过程记录
近期某客户中了勒索病毒,虽然前期多次提醒客户注意异地备份,但始终未执行,导致悲剧. 经过几天的努力,该客户信息系统已基本恢复正常运行,现将相关过程记录如下,作为警示. 方案抉择 交赎金解密:风险过高, ...
- redis命令之 ----SortedSed(有序集合)
ZADD ZADD key score member [[score member] [score member] ...] 将一个或多个 member 元素及其 score 值加入到有序集 key ...
- [DP]Luogu 2014NOIP提高组 飞扬的小鸟题解
2014NOIP提高组飞扬的小鸟题解 题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一 ...