资讯专栏INFORMATION COLUMN

服务治理:Spring Cloud Eureka(上)

dackel / 909人阅读

摘要:服务治理上是由开源的一款基于的服务治理组件,包括及。由于种种原因,版本已经冻结开发,目前最新版本是年月份发布的版本。服务发现选型其中比较受众关注的就是和这两款产品,这两款产品各有所长,各有所适,开发者可用按需选择。

服务治理:Spring Cloud Eureka(上)

</>复制代码

  1. Netflix Eureka是由Netflix开源的一款基于REST的服务治理组件,包括Eureka ServerEureka Client。由于种种原因,Eureka 2.x版本已经冻结开发,目前最新版本是2018年8月份发布的1.9.4版本。
    Spring Cloud EurekaPivotal公司为Netflix Eureka整合于Spring Cloud生态系统提供的版本。
1. 服务发现 1.1 Eureka简介

</>复制代码

  1. EurekaNetflix公司提供的开源服务发现组件(现已闭源),最新版本是1.9.4,该组件提供的服务发现可以为负载均衡、failover等提供支持。Eureka包括Eureka ServerEureka ClientEureka Server提供REST服务,Eureka Clinet多数是使用Java编写的客户端(Eureka Client可以使用其他语言编写,比如Node.js.NET),用于简化和Eureka Server的交互。
1.2 Eureka Server简单案例

所有工程使用Spring Cloud的新版Greenwich.SR1和Maven构建。

1.2.1 创建Spring Cloud Eureka Server工程

pom.xml内容如下:

</>复制代码

  1. 4.0.0
  2. org.springframework.boot
  3. spring-boot-starter-parent
  4. 2.1.4.RELEASE
  5. watermelon.cloud
  6. eureka-server
  7. 0.0.1-SNAPSHOT
  8. eureka-server
  9. Spring Cloud Eureka Server
  10. 1.8
  11. Greenwich.SR1
  12. org.springframework.cloud
  13. spring-cloud-starter-netflix-eureka-server
  14. org.springframework.boot
  15. spring-boot-starter-test
  16. test
  17. org.springframework.cloud
  18. spring-cloud-dependencies
  19. ${spring-cloud.version}
  20. pom
  21. import
  22. org.springframework.boot
  23. spring-boot-maven-plugin

Finchley版本之后,Eureka的depenecy片段稍微有点不同

</>复制代码

  1. org.springframework.cloud
  2. spring-cloud-starter-eureka-server
  3. org.springframework.cloud
  4. spring-cloud-starter-netflix-eureka-server
1.2.2 开启EurekaServer支持

在启动类上加上@EnableEurekaServer注解。

</>复制代码

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class EurekaServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaServerApplication.class, args);
  6. }
  7. }
1.2.3 修改application.yml配置文件

</>复制代码

  1. server:
  2. port: 1111
  3. spring:
  4. application:
  5. name: eureka-server
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. client:
  10. register-with-eureka: false
  11. fetch-registry: false
  12. serviceUrl:
  13. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  14. server:
  15. waitTimeInMsWhenSyncEmpty: 0
  16. enableSelfPreservation: false
  17. # 后面详细讲配置
1.2.4 编译、启动

浏览器访问http://localhost:1111/出现如下页面就说明服务端简单案例构建成功。

1.3 Eureka Client简单案例 1.3.1 创建Eureka Client工程

pom.xml内容如下:

</>复制代码

  1. 4.0.0
  2. org.springframework.boot
  3. spring-boot-starter-parent
  4. 2.1.4.RELEASE
  5. watermelon.cloud
  6. eureka-client
  7. 0.0.1-SNAPSHOT
  8. eureka-client
  9. Spring Cloud Eureka Client
  10. 1.8
  11. Greenwich.SR1
  12. org.springframework.boot
  13. spring-boot-starter-web
  14. org.springframework.cloud
  15. spring-cloud-starter-netflix-eureka-client
  16. org.projectlombok
  17. lombok
  18. true
  19. org.springframework.boot
  20. spring-boot-starter-test
  21. test
  22. org.springframework.cloud
  23. spring-cloud-dependencies
  24. ${spring-cloud.version}
  25. pom
  26. import
  27. org.springframework.boot
  28. spring-boot-maven-plugin
1.3.2 启动类启用DiscoveryClient支持

</>复制代码

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class EurekaClientApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaClientApplication.class, args);
  6. }
  7. }
1.3.3 修改applicaiton.yml配置文件

</>复制代码

  1. server:
  2. port: 2222
  3. spring:
  4. application:
  5. name: eureka-client # 如果不配置name属性,在注册中心的实例名都将是UNKNOWN
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://localhost:1111/eureka/ # 服务注册中心地址
1.3.4 HelloController简单打印输出

</>复制代码

  1. @RestController
  2. @Slf4j
  3. public class HelloController {
  4. private final DiscoveryClient client;
  5. @Autowired
  6. public HelloController(DiscoveryClient client) {
  7. this.client = client;
  8. }
  9. @GetMapping("/hello")
  10. public String sayHello() {
  11. List serviceInstances = client.getInstances("eureka-client");
  12. serviceInstances.forEach(serviceInstance -> {
  13. log.info("Host: {}, Port: {}", serviceInstance.getHost(), serviceInstance.getPort());
  14. log.info("serviceId: {}, InstanceId: {}", serviceInstance.getServiceId(), serviceInstance.getInstanceId());
  15. });
  16. return "Hello, Spring Cloud Eureka. - " + LocalDateTime.now().toString();
  17. }
  18. }
先启动Eureka Server,再启动Eureka Client,测试

访问http://localhost:111/eureka/,如果Server和Client都启动成功并且配置正确的情况将会如下情况。

访问http://localhost:2222/hello/,会出现文字信息和日志输出。

简单的入门案例就此搭建结束,虽然没实现什么功能,但是我们可以从服务注册中心观察到可用的Eureka Client实例,和在日志中打印服务实例的一些简短信息。

1.4 Eureka Server的REST API

</>复制代码

  1. Eureka 在 GitHub 的 wiki 上专门写了一篇《 Eureka REST operations》来介绍 Eureka Server 的 REST API 接口,Spring Cloud Netfix Eureka 跟 Spring Boot 适配之后,提供的 REST API 与原始的 REST API 有一点点不同,其路径中的 {version} 值固定为 eureka,其他的变化不大.

以下简单演示apps的REST端点:访问http://localhost:1111/eureka/apps,会得到以下返结果。

</>复制代码

  1. 1
  2. UP_1_
  3. EUREKA-CLIENT
  4. localhost:eureka-client:2222
  5. localhost
  6. EUREKA-CLIENT
  7. 192.168.91.1
  8. UP
  9. UNKNOWN
  10. 2222
  11. 443
  12. 1
  13. MyOwn
  14. 30
  15. 90
  16. 1557113978372
  17. 1557114128293
  18. 0
  19. 1557113978373
  20. 2222
  21. http://localhost:2222/
  22. http://localhost:2222/actuator/info
  23. http://localhost:2222/actuator/health
  24. eureka-client
  25. eureka-client
  26. false
  27. 1557113978373
  28. 1557113978278
  29. ADDED

文末提供一些,服务发现选型对比,下篇文章介绍Eureka的核心类及其内容。

2. 服务发现选型


其中比较受众关注的就是Eureka和Consul这两款产品,这两款产品各有所长,各有所适,开发者可用按需选择。

个人微信公众号,欢迎交流鸭!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74427.html

相关文章

  • Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston

    摘要:属性对应服务注册中心的配置内容,指定服务注册中心的位置。项目是针对的服务治理实现。下面可以尝试让的服务提供者运行起来。我们可以用下面的命令启动的开发模式服务端启动完成之后,我们再将之前改造后的服务提供者启动起来。 已经有非常长的时间没有更新《Spring Cloud构建微服务架构》系列文章了,自从开始写Spring Cloud的专题内容开始就获得了不少的阅读量和认可,当然也有一些批评...

    djfml 评论0 收藏0
  • spring-cloud-eureka服务治理

    摘要:服务续约在服务注册完成之后,服务提供者需要维护一个心跳来告知注册中心服务实例处于正常运行状态中,防止注册中心将正常的服务实例剔除出注册中心。 Spring Cloud Eureka 目录 前言 构建服务注册中心 服务注册与发现 Eureka的基础架构 Eureka的服务治理机制 Eureka的配置 代码地址 前言 服务治理  随着微服务应用的不断增加,静态配置会越来越难以维护,并且...

    Clect 评论0 收藏0
  • SpringCloud核心教程 | 第三篇:服务注册与发现 Eureka

    摘要:下一篇介绍基于的服务注册与调用。服务提供者工程配置这里服务提供者是使用之前进阶教程第三篇整合连接池以及监控改造而来,这里一样的部分就不再重复说明,下面将说明新增的部分。 Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分...

    scq000 评论0 收藏0
  • Spring Cloud构建微服务架构:服务消费(基础)【Dalston版】

    摘要:下面的例子,我们将利用上一篇中构建的作为服务注册中心作为服务提供者作为基础。我们先来创建一个服务消费者工程,命名为。初始化,用来真正发起请求。注解用来将当前应用加入到服务治理体系中。 通过上一篇《Spring Cloud构建微服务架构:服务注册与发现》,我们已经成功地将服务提供者:eureka-client或consul-client注册到了Eureka服务注册中心或Consul服务端...

    livem 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<