【Spring Cloud】分布式必学springcloud(二)——注册中心Eureka

川长思鸟来 2022-05-27 12:46 280阅读 0赞

一、前言

  1. 在上一篇博客中,小编简单的向大家介绍了springcloud的一些概念,大家可能对那些还是不太的了解,不过没有问题,盲人摸象嘛,慢慢的摸,你会了解的越来越多的。所以不用担心。在下面的博客,小编依次介绍,依次实现。
  2. 在这篇博客中,小编介绍一下注册中心——Eureka

二、注册中心Eureka

  1. 想必做过Dubbo的老铁们一定使用过Zookeeper吧。这里的zookeeper就起到了一个注册中心的作用。Dubbo架构中分成了提供者和消费者,提供者需要把自己的服务注册到注册中心,消费者订阅注册中心,注册中心有提供者后,会通知消费者。ZK起到了一个服务协调的作用。
  2. springcloud中,自己提供了一个注册中心——Eureka
  3. Eureka是一个基于REST(Representational State Transfer)的服务,主要用于AWS cloud 提供服务定位(locating services)、负载均衡(load balancing)、故障转移(failover of middle-tier servers)。我们把它叫做Eureka Server. Eureka也提供了基于Java的客户端组件,Eureka Client,内置的负载均衡器可以实现基本的round-robin负载均衡能力。在Netflix,一个基于Eureka的更复杂的负载均衡器针对多种因素(如流量、资源利用率、错误状态等)提供加权负载均衡,以实现高可用(superior resiliency).

三、服务注册中心

3.1 建立Maven项目

  1. 小编使用springboot的快速生产项目的网站 [http://start.spring.io/][http_start.spring.io],来生产项目:

这里写图片描述

  1. 导入idea

这里写图片描述

3.2 导入Eureka依赖和springcloud依赖

  1. mavenpom文件中,添加Eureka依赖:`spring-cloud-starter-eureka-server`
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <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">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.forezp</groupId>
  6. <artifactId>eurekaserver</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>eurekaserver</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.2.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Edgware.SR2</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <!--eureka server -->
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  28. </dependency>
  29. <!-- spring boot test-->
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>
  36. <dependencyManagement>
  37. <dependencies>
  38. <dependency>
  39. <groupId>org.springframework.cloud</groupId>
  40. <artifactId>spring-cloud-dependencies</artifactId>
  41. <version>${spring-cloud.version}</version>
  42. <type>pom</type>
  43. <scope>import</scope>
  44. </dependency>
  45. </dependencies>
  46. </dependencyManagement>
  47. <build>
  48. <plugins>
  49. <plugin>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-maven-plugin</artifactId>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. <repositories>
  56. <repository>
  57. <id>spring-milestones</id>
  58. <name>Spring Milestones</name>
  59. <url>https://repo.spring.io/milestone</url>
  60. <snapshots>
  61. <enabled>false</enabled>
  62. </snapshots>
  63. </repository>
  64. </repositories>
  65. </project>
  66. 添加完成的时候,如果要建立父子pom,需要把`${spring-cloud.version}`提取出来。这样子pom中跟springcloud相关的就不用设置版本号了。

3.3 代码编写

  1. springboot有一个application的启动类,我们这里就修改这个启动类:添加`@EnableEurekaServer`。通过这个注解,启动一个服务注册中心提供给其他应用进行对话。
  2. package com.wl.eureka;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  6. @SpringBootApplication
  7. @EnableEurekaServer
  8. public class EurekaApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(EurekaApplication.class, args);
  11. }
  12. }

3.4 修改配置文件

  1. 在默认情况下,注册中心会将自己作为客户端来尝试注册自己,所以我们需要禁用它的客户端注册行为。需要在application.properties文件或application.yml中添加配置。
  2. 注意:生成的maven项目默认是application.properties,没有application.yml,对比一下,官方很多的demo中使用的是application.yml,另外application.yml天然的形成了树结构。代码写的也少。
  3. server:
  4. port: 8761
  5. eureka:
  6. instance:
  7. hostname: localhost
  8. client:
  9. registerWithEureka: false
  10. fetchRegistry: false
  11. serviceUrl:
  12. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  13. 代码说明:
  • eureka.client.registerWithEureka:false ,由于应用为注册中心, 来表明自己是一个eureka server. 不会把自己注册到自己上。
  • eureka.client.fetchRegistry:由于注册中心的职责是维护服务实例,不需要去检索服务,所以也设置为false

3.5 运行

  1. 运行后,输入`http://localhost:8761/`,打开注册中心监控页面:

这里写图片描述

3.6 Eureka配置用户名密码登录

  1. pom文件中添加:
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-security</artifactId>
  5. </dependency>
  6. 修改配置文件:
  7. server:
  8. port: 8761
  9. security:
  10. basic:
  11. enabled: true
  12. user:
  13. name: admin
  14. password: admin
  15. eureka:
  16. instance:
  17. hostname: localhost
  18. client:
  19. registerWithEureka: false
  20. fetchRegistry: false
  21. serviceUrl:
  22. defaultZone: http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
  23. 再次登录的时候,会出现输入账户和密码的弹窗:

这里写图片描述

四、服务提供者

  1. 建立如下的结构:

这里写图片描述

  1. clientserver注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
  2. 创建过程同server类似,创建完pom.xml如下:
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <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">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.wl</groupId>
  7. <artifactId>client</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <name>client</name>
  11. <description>Demo project for Spring Boot</description>
  12. <parent>
  13. <groupId>com.wl</groupId>
  14. <artifactId>scf</artifactId>
  15. <version>0.0.1-SNAPSHOT</version>
  16. </parent>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-eureka</artifactId>
  21. </dependency>
  22. </dependencies>
  23. </project>
  24. 创建controller
  25. package com.wl.client.controller;
  26. import org.springframework.web.bind.annotation.GetMapping;
  27. import org.springframework.web.bind.annotation.RequestParam;
  28. import org.springframework.web.bind.annotation.RestController;
  29. /** * Created by Ares on 2018/4/11. */
  30. @RestController
  31. public class User {
  32. @GetMapping("/user/findById")
  33. public String findById(@RequestParam("id")String id){
  34. return "这个是springcloud的客户端"+id;
  35. }
  36. }
  37. 配置启动类:
  38. package com.wl.client;
  39. import org.springframework.boot.SpringApplication;
  40. import org.springframework.boot.autoconfigure.SpringBootApplication;
  41. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  42. @SpringBootApplication
  43. @EnableEurekaClient
  44. public class ClientApplication {
  45. public static void main(String[] args) {
  46. SpringApplication.run(ClientApplication.class, args);
  47. }
  48. }
  49. 类中@EnableEurekaClient@EnableDiscoveryClient注解,激活EurekaDiscoveryClient实现,自动化配置,创建DiscoveryClient接口针对Eureka客户端的EnableDiscoveryClient实例。
  50. 修改配置文件:
  51. server:
  52. port: 9001
  53. eureka:
  54. client:
  55. serviceUrl:
  56. defaultZone: http://${security.user.name}:${security.user.password}@localhost:8761/eureka/
  57. spring:
  58. application:
  59. name: client1
  60. security:
  61. basic:
  62. enabled: true
  63. user:
  64. name: admin
  65. password: admin
  66. 在配置文件中,使用spring.application.name配置服务名,如命名为client1;通过eureka.serviceUrl.defaultZone指定服务注册中心的地址,这里小编指定的是之前建立的注册中心地址。
  67. 依次启动注册中心`http://localhost:8761/`和提供者

这里写图片描述

  1. 我们可以通过访问 `http://localhost:9001//user/findById?id=1`来访问提供者:

这里写图片描述

五、小结

  1. Eureka服务注册发现,容错管理、失效剔除、自我保护、安全验证。功能真是很强大,使用也非常好用,多多用吧。

发表评论

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

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

相关阅读