Spring Cloud服务发现/注册
服务发现/注册
服务发现(Service Discovery)
在计算机网络中,一种自发现设备或者服务的技术,通过服务发现协议(Service Discovery Protocol)实现。
常见协议
- java:jini(Apache River)
- REST:HATEOAS
- Web Services:UDDI(Universal Description Discovery and lntegration)
服务注册(Service Registration)
在计算机网络中,为了更好地治理多个设备或者服务,这些设备或者服务主动或者被动注册到管理中心,以便服务被发现和消费。
常见注册中心
- Apache Zookeper 性能最差
- Netflix Eureka 高可用,高 一致性差
- Consul 高可用,高一致性(某些方面)
高可用架构
高可用(High Availability)
一种系统特性,致力于确保可接受程度的执行操作,通常采用上线时间作为基准。其中,以一年内的上线时间与自然时间的比率来描述可用性。
基本原则
- 消灭单点故障
- 可靠性交迭
- 故障探测
Spring Cloud Netflix Eureka
服务发现:Eureka
Eureka是由Netflix公司发明的服务发现中间件,包括服务发现服务器和客户端的。
核心组件
- Eureka Server
- Eureka Client
服务端:Eureka Server
Eureka Server是Eureka Client的注册服务中心,管理所有注册服务、以及其实例信息和状态。
运行Eureka Server
- 依赖:org.springframework.cloud:spring-cloud-starter-eureka-server
- 激活:@EnableEurekaServer
1.引入Maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.激活Erueka服务器
package com.example.springcloudlesson4eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudLesson4EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudLesson4EurekaServerApplication.class, args);
}
}
3.调整Eureka服务器配置
application.properties
##服务器应用名
spring.application.name=spring-cloud-eureka-server
##服务器端口
server.port=9090
##管理端口安全失效
management.security.enabled=false
检查Eureka Server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.7.2.jar:1.7.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.7.2.jar:1.7.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.7.2.jar:1.7.2]
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.7.2.jar:1.7.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.7.2.jar:1.7.2]
at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1027) [eureka-client-1.7.2.jar:1.7.2]
检查Eureka Server
问题原因:Eureka Server即是注册服务器,也是客户端,默认情况,也需要配置注册中心地区。
客户端:Eureka Client
Eureka Client为当前服务提供注册、同步、查找服务以及其实例信息或状态等能力。
运行Eureka Client
- 依赖:org.springframework.cloud:spring-cloud-starter-eureka
- 激活:@EnableEurekaClient或者@EnableDiscoveryClient
1.引入Maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.激活Eureka客户端
package com.example.springcloudlesson4eurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SpringCloudLesson4EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudLesson4EurekaClientApplication.class, args);
}
}
@EnableDiscoveryClient比@EnableEurekaClient好一些
因为不绑定某种实现,注册不到,服务不会发现而这个重启一下就好了
3.配置Euraka 客户端
##客户端应用名称
spring.application.name=spring-cloud-eureka-client
##客户端端口
server.port=8080
##管理端口安全失效
management.security.enabled=false
4.启动时会报连接拒绝错误
检验Eureka客户端
发现与Eureka服务器端出现相同异常
需要再次调整Eureka客户端
##客户端应用名称
spring.application.name=spring-cloud-eureka-client
##客户端端口
server.port=8080
##管理端口安全失效
management.security.enabled=false
##Spring Cloud Eureka客户端 注册到服务器
eureka.client.serviceUrl.defaultZone=http://localhost:9090/eureka
配置完成后出现下面截图情况表示连接成功

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
记一次使用阿里云图像搜索功能
记一次使用阿里云图像搜索功能 阿里云提供的图像搜索业务: 业务介绍地址: https://ai.aliyun.com/imagesearch?spm=5176.10695662.1280361.130.11a61e6d5n1K8L 提供两种服务: 商品图像搜索; 通用图像搜索; 开发文档 文档地址: https://help.aliyun.com/document_detail/66616.html 目前在该页支持的sdk有: Java Nodejs PHP 从文档来看使用起来十分复杂,想用python实现的话,借鉴这份资料真是费了很大力气,一直没有跑通,提示构建的签名与它计算的签名不匹配。 后面找寻python sdk。 python sdk使用指南 https://help.aliyun.com/document_detail/67117.html 按照这个介绍,自己安装了核心库,并歪打正着的通过pip安装找到了该服务的隐藏python sdk! pip install aliyun-python-sdk-imagesearch 然后去aliyun的gihub页面找相关的包介绍: ...
-
下一篇
Python骚操作:动态定义函数
标题:Python Tips: Dynamic function definition 作者:Philip Trauner 译者:豌豆花下猫 链接:https://philip-trauner.me/blog/post/python-tips-dynamic-function-definition 基于 MIT 许可协议 在 Python 中,没有可以在运行时简化函数定义的语法糖。然而,这并不意味着它就不可能,或者是难以实现。 from types import FunctionType foo_code = compile('def foo(): return "bar"', "<string>", "exec") foo_func = FunctionType(foo_code.co_consts[0], globals(), "foo") print(foo_func()) 输出: bar 剖析 逐行检视代码,你会发现语言/解释器的屏障是多么脆弱。 >>> from types import FunctionType Python 文档通常不会列出那...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Docker容器配置,解决镜像无法拉取问题
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- 2048小游戏-低调大师作品
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- MySQL数据库在高并发下的优化方案