springCloud学习四之通过Eureka实现服务调用

Demo project for Spring Boot

<java.version>11</java.version>

<spring-cloud.version>2020.0.3</spring-cloud.version>

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

配置文件:

server.port=6060

spring.application.name=Provider1

#设置服务注册中心的URL,用于client和server端交流,这里是客户端,寻找主机

eureka.client.service-url.defaultZone=http://euk1.com:8081/eureka/

实现一个Controller:

package com.learn.eurekaprovider1.controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class MainController {

@GetMapping("/getHi")

public String getHi() {

return “Hello”;

}

}

http://localhost:6060/getHi

springCloud学习四之通过Eureka实现服务调用

查看eureka服务器:

springCloud学习四之通过Eureka实现服务调用

springCloud学习四之通过Eureka实现服务调用

Rest服务调用


官方文档

https://github.com/Netflix/eureka/wiki/Eureka-REST-operations

| Operation | HTTP action | Description |

| — | — | — |

| Register new application instance | POST /eureka/v2/apps/appID | Input: JSON/XMLpayload HTTPCode: 204 on success |

| De-register application instance | DELETE /eureka/v2/apps/appID/instanceID | HTTP Code: 200 on success |

| Send application instance heartbeat | PUT /eureka/v2/apps/appID/instanceID | HTTP Code: * 200 on success * 404 if instanceIDdoesn’t exist |

| Query for all instances | GET /eureka/v2/apps | HTTP Code: 200 on success Output: JSON/XML |

| Query for all appID instances | GET /eureka/v2/apps/appID | HTTP Code: 200 on success Output: JSON/XML |

| Query for a specific appID/instanceID | GET /eureka/v2/apps/appID/instanceID | HTTP Code: 200 on success Output: JSON/XML |

| Query for a specific instanceID | GET /eureka/v2/instances/instanceID | HTTP Code: 200 on success Output: JSON/XML |

| Take instance out of service | PUT /eureka/v2/apps/appID/instanceID/status?value=OUT_OF_SERVICE | HTTP Code: * 200 on success * 500 on failure |

| Move instance back into service (remove override) | DELETE /eureka/v2/apps/appID/instanceID/status?value=UP (The value=UP is optional, it is used as a suggestion for the fallback status due to removal of the override) | HTTP Code: * 200 on success * 500 on failure |

| Update metadata | PUT /eureka/v2/apps/appID/instanceID/metadata?key=value | HTTP Code: * 200 on success * 500 on failure |

| Query for all instances under a particular vip address | GET /eureka/v2/vips/vipAddress | * HTTP Code: 200 on success Output: JSON/XML * 404 if the vipAddressdoes not exist. |

| Query for all instances under a particular secure vip address | GET /eureka/v2/svips/svipAddress | * HTTP Code: 200 on success Output: JSON/XML * 404 if the svipAddressdoes not exist. |

查询所有的服务:

http://localhost:8081/eureka/apps

![](https://www.icode9.com/i/ll/?i=20210725170700978.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NvZGluZ1RvTWFzdGVy,size_16,co

【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

lor_FFFFFF,t_70)

或者某个服务:

http://localhost:8081/eureka/instances/DESKTOP-GRIPBVL:Provider1:6060

springCloud学习四之通过Eureka实现服务调用

http://localhost:8081/eureka/apps/PROVIDER1

springCloud学习四之通过Eureka实现服务调用

服务状态

http://localhost:8081/eureka/status

springCloud学习四之通过Eureka实现服务调用

如果需要json格式 可以加个请求头Accept:application/json

{

“generalStats”: {

“environment”: “test”,

“num-of-cpus”: “16”,

“total-avail-memory”: “517mb”,

“current-memory-usage”: “45mb (8%)”,

“server-uptime”: “00:03”

},

“applicationStats”: {

“registered-replicas”: “”,

“available-replicas”: “”,

“unavailable-replicas”: “”

},

“instanceInfo”: {

“instanceId”: “localhost”,

“hostName”: “localhost”,

“app”: “UNKNOWN”,

“ipAddr”: “192.168.29.1”,

“status”: “UP”,

“overriddenStatus”: “UNKNOWN”,

“port”: {

“$”: 8080,

“@enabled”: “true”

},

“securePort”: {

“$”: 443,

“@enabled”: “false”

},

“countryId”: 1,

“dataCenterInfo”: {

“@class”: “com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo”,

“name”: “MyOwn”

},

“leaseInfo”: {

“renewalIntervalInSecs”: 30,

“durationInSecs”: 90,

“registrationTimestamp”: 0,

“lastRenewalTimestamp”: 0,

“evictionTimestamp”: 0,

“serviceUpTimestamp”: 0

},

“metadata”: {

“management.port”: “8080”,

“jmx.port”: “7649”

},

“homePageUrl”: “http://localhost:8080/”,

“statusPageUrl”: “http://localhost:8080/actuator/info”,

“healthCheckUrl”: “http://localhost:8080/actuator/health”,

“vipAddress”: “unknown”,

“secureVipAddress”: “unknown”,

“isCoordinatingDiscoveryServer”: “false”,

“lastUpdatedTimestamp”: “1586328420409”,

“lastDirtyTimestamp”: “1586328420519”

}

注册到eureka的服务信息查看

get: {ip:port}/eureka/apps

注册到eureka的具体的服务查看

get: {ip:port}/eureka/apps/{appname}/{id}

服务续约

put:{ip:port}/eureka/apps/{appname}/{id}?lastDirtyTimestamp={}&status=up

更改服务状态

put:{ip:port}/eureka/apps/{appname}/{id}/status?lastDirtyTimestamp={}&value={UP/DOWN} 对应eureka源码的:InstanceResource.statusUpdate

删除状态更新

delete:{ip:port}/eureka/apps/{appname}/{id}/status?lastDirtyTimestamp={}&value={UP/DOWN}

删除服务

delete: {ip:port}/eureka/apps/{appname}/{id}

元数据

Eureka的元数据有两种:标准元数据和自定义元数据。 标准元数据:主机名、IP地址、端口号、状态页和健康检查等信息,这些信息都会被发布在服务注册表中,用于服务之间的调用。

自定义元数据:可以使用eureka.instance.metadata-map配置,这些元数据可以在远程客户端中访问,但是一般不改变客户端行为,除非客户端知道该元数据的含义。

可以在配置文件中对当前服务设置自定义元数据,可后期用户个性化使用

元数据可以配置在eureka服务器和eureka的客户端上

eureka.instance.metadata-map.daniu=dalao

服务消费方:

======

springCloud学习四之通过Eureka实现服务调用

springCloud学习四之通过Eureka实现服务调用

POM依赖

<?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”

上一篇:docker导入导出镜像和容器,离线部署项目


下一篇:Spring Cloud - Nacos与Eureka区别及如何选型