介绍微服务中服务治理Spring-Cloud-Netflix-Eureka

£神魔★判官ぃ 2022-05-29 04:42 233阅读 0赞

1、概述

微服务框架中最为核心和基础的模块就是服务治理,它主要用来实现各个微服务实例的自动化注册与发现。在这个体系结构中有一个“中心点”——服务注册中心,每个服务必须注册到服务注册中心。而各个服务之间进行通讯并不需要知道具体服务的主机名和端口。这种实现的一个缺点是所有客户机必须实现某种逻辑来与这个中心点进行交互,这样在实现服务请求之前将增加一次额外的网络往返。

Spring Cloud 使用 Netflix Eureka 来实现服务注册与发现,它既包含了服务端组件,也包含了客户端组件。Eureka服务端也称为服务注册中心,支持高可用配置。它依托强一致性提供良好的服务实例可用性。Eureka客户端可以同时充当服务器,将其状态复制到一个连接的对等点上。换句话说,客户机检索服务注册中心所有连接的节点的列表,并通过负载平衡算法向所有其他服务发出请求。每个客户机为了声明自己的存活状态,他们必须向注册中心发送一个心跳信号。

在本例中为了体现服务治理功能,实现了三个微服务:

  • 一个服务注册中心 (Eureka Server)
  • 一个在注册中心注册的REST服务(Eureka Client)
  • 一个Web应用程序,服务的消费者(Spring Cloud Netflix Feign Client)

2、注册中心

使用Spring Cloud Netflix Eureka实现一个注册中心非常简单。在pom里增加spring-cloud-starter-eureka-server依赖,在启动类里添加@EnableEurekaServer注解。

首先创建一个Maven工程,并添加一些依赖关系

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.10.RELEASE</version>
  5. <relativePath />
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.cloud</groupId>
  10. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  11. </dependency>
  12. </dependencies>
  13. <dependencyManagement>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.cloud</groupId>
  17. <artifactId>spring-cloud-starter-parent</artifactId>
  18. <version>Edgware.SR2</version>
  19. <type>pom</type>
  20. <scope>import</scope>
  21. </dependency>
  22. </dependencies>
  23. </dependencyManagement>

然后创建一个启动类

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class EurekaServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaServerApplication.class, args);
  6. }
  7. }

在application.yml中加入配置信息

  1. server:
  2. port: 8888 #服务注册中心端口号
  3. eureka:
  4. instance:
  5. hostname: localhost #服务注册中心实例的主机名
  6. client:
  7. registerWithEureka: false #是否向服务注册中心注册自己
  8. fetchRegistry: false #是否检索服务
  9. serviceUrl:
  10. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

现在通过浏览器访问http://localhost:8888 可以看到Eureka的控制台,在那里能看到将来注册后的服务实例和一些状态和健康指标。
1.png

3、服务提供者

首先我们添加一些依赖关系

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>

然后实现主应用类

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @RestController
  4. public class EurekaClientApplication implements GreetingController {
  5. @Autowired
  6. @Lazy
  7. private EurekaClient eurekaClient;
  8. @Value("${spring.application.name}")
  9. private String appName;
  10. public static void main(String[] args) {
  11. SpringApplication.run(EurekaClientApplication.class, args);
  12. }
  13. public String greeting() {
  14. return String.format("Hello from '%s'!", eurekaClient.getApplication(appName).getName());
  15. }
  16. }

在application.yml文件里添加配置

  1. spring:
  2. application:
  3. name: spring-cloud-eureka-client
  4. server:
  5. port: 0
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: ${ EUREKA_URI:http://localhost:8888/eureka}
  10. instance:
  11. preferIpAddress: true

我们让Spring Boot为我们选择一个随机端口,因为后面是使用名称来访问这个服务。现在重新访问Eureka控制台可以看到新注册的这个服务。
2.png

4、服务消费者

最后我们实现第三个微服务,使用Spring Netflix Feign Client实现的REST消费Web应用。

在Feign Client工程的pom.xml文件里添加一些依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-feign</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  16. </dependency>

定义Feign Client 接口,引入要调用的服务名称

  1. @FeignClient("spring-cloud-eureka-client")
  2. public interface GreetingClient{
  3. @RequestMapping("/greeting")
  4. String greeting();
  5. }

定义主服务类,是一个controller控制器

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableFeignClients
  4. @Controller
  5. public class FeignClientApplication {
  6. @Autowired
  7. private GreetingClient greetingClient;
  8. public static void main(String[] args) {
  9. SpringApplication.run(FeignClientApplication.class, args);
  10. }
  11. @RequestMapping("/get-greeting")
  12. public String greeting(Model model) {
  13. model.addAttribute("greeting", greetingClient.greeting());
  14. return "greeting-view";
  15. }
  16. }

下面是HML模板可进行页面展现

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>Greeting Page</title>
  5. </head>
  6. <body>
  7. <h2 th:text="${greeting}"></h2>
  8. </body>
  9. </html>

配置文件内容

  1. spring:
  2. application:
  3. name: spring-cloud-eureka-feign-client
  4. server:
  5. port: 8080
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: ${ EUREKA_URI:http://localhost:8888/eureka}

启动运行这个服务,在浏览器里访问 http://localhost:8080/get-greeting 就会看到

  1. Hello from 'SPRING-CLOUD-EUREKA-CLIENT'!

5、结论

我们现在可以使用Spring Cloud Netflix Eureka Server实现服务注册中心,并注册一些Eureka Clients。在创建服务提供者的时候没有指定端口,这样服务将监听一个随机选择的端口。使用Feign Client通过服务名可以定位和消费这个REST服务,甚至在位置发生变化时也可以。

发表评论

表情:
评论列表 (有 0 条评论,233人围观)

还没有评论,来说两句吧...

相关阅读

    相关 服务架构之服务治理

    单体应用改造为微服务架构后,服务调用由本地调用变成远程调用,服务消费者A需要通过注册中心去查询服务提供者B的地址,然后发起调用,这个看似简单的过程就可能会遇到下面几种情况,比如

    相关 浅谈服务治理服务

    本篇文章先简单介绍了互联网[架构][Link 1]的演变,进而介绍了服务化,最后再介绍微服务,微服务是服务治理的升级也是互联网架构的进一步延伸。 互联网架构演变 一体