Spring Cloud 如何使用Eureka注册服务

悠悠 2021-12-10 12:11 377阅读 0赞

要使用Eureka实现服务发现,需要项目中包含Eureka的服务端发现组件以及客户端发现组件。

搭建Maven父工程

创建一个Maven父工程xcservice-springcloud,并在工程的pom.xml中添加Spring Cloud的版本依赖等信息,如文件4-1所示。
  文件4-1 pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  3. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.xc</groupId>
  6. <artifactId>xcservice-springcloud</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.5.6.RELEASE</version>
  13. <relativePath/>
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencyManagement>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-dependencies</artifactId>
  25. <version>Dalston.SR3</version>
  26. <type>pom</type>
  27. <scope>import</scope>
  28. </dependency>
  29. </dependencies>
  30. </dependencyManagement>
  31. <build>
  32. <plugins>
  33. <!--Spring Boot的编译插件 -->
  34. <plugin>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-maven-plugin</artifactId>
  37. </plugin>
  38. </plugins>
  39. </build>
  40. </project>

搭建服务端工程

在父工程xcservice-springcloud中,创建Maven子模块xcservice-eureka-server作为服务端工程,该模块是一个基础的Spring Boot工程,其主要文件代码的实现过程如下。
  (1)添加依赖。
  在pom.xml中添加Eureka Server的依赖,如文件4-2所示。
  文件4-2 pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. 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. <parent>
  6. <groupId>com.xc</groupId>
  7. <artifactId>xcservice-springcloud</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.xc</groupId>
  11. <artifactId>xcservice-eureka-server</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>xcservice-eureka-server</name>
  14. <description>服务端工程</description>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <plugins>
  28. <plugin>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-maven-plugin</artifactId>
  31. </plugin>
  32. </plugins>
  33. </build>
  34. </project>

(2)编写配置文件。
  在配置文件中增加端口号等配置信息,如文件4-3所示。
  文件4-3 application.yml

  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. register-with-eureka: false
  8. fetch-registry: false
  9. service-url:
  10. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  11. #server:
  12. #enable-self-preservation: false #关闭保护机制,以确保注册中心可以将不可用的实例正确删除

上述代码中,首先配置了端口号为8761,所有服务的实例都需要向此端口注册。接下来配置了实例名为localhost。由于本项目是一个注册中心,是不需要向自己注册和检索服务的,所以register-with-eureka和fetch-registry都需要设置为false。最后defaultZone中的地址是注册中心的地址。

(3)修改服务端Java代码。
  在项目的引导类上添加注解@EnableEurekaServer,该注解用于声明标注类是一个EurekaServer,如文件4-4所示。
  文件4-4 EurekaApplication.java

  1. package com.xc.xcserviceeurekaserver;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * http://localhost:8761/
  7. */
  8. @SpringBootApplication
  9. @EnableEurekaServer
  10. public class XcserviceEurekaServerApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(XcserviceEurekaServerApplication.class, args);
  13. }
  14. }

(4)启动应用,查看信息。
  完成上述配置后,启动应用程序并在浏览器中访问地址http://localhost:8761/即可看到Eureka的信息面板。

aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xMzM0MTYzMS1hY2UzMzI1MTcyNGNiMTI2LnBuZw

可以看出,Eureka Server的信息页面已经成功显示,但此时“Instances currently registered with Eureka”下的显示信息为“No instances available”,这表示该注册中心还没有注册任何可用的实例。

在这里给大家风向一波免费Java资源包括分布式,微服务,并发,面试,Java架构师成长路线等等资源
加QQ:2995457287 或 vx:gupao-cola 免费获取。

搭建客户端工程

在父工程xcservice-springcloud中,创建Maven子模块xcservice-eureka-user作为客户端工程,该模块也是一个基础的Spring Boot工程,其主要文件代码的实现过程如下。
  (1)添加依赖。在pom.xml中添加Eureka依赖,如文件4-5所示。
  文件4-5 pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. 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. <parent>
  6. <groupId>com.xc</groupId>
  7. <artifactId>xcservice-springcloud</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.xc</groupId>
  11. <artifactId>xcservice-eureka-user</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>xcservice-eureka-user</name>
  14. <description>客户端工程</description>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-eureka</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <plugins>
  28. <plugin>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-maven-plugin</artifactId>
  31. </plugin>
  32. </plugins>
  33. </build>
  34. </project>

(2)编写配置文件。在配置文件中添加Eureka服务实例的端口号、服务端地址等信息,如文件4-6所示。
  文件4-6 application.yml

  1. server:
  2. port:8000 # 指定该Eureka实例的端口号
  3. eureka:
  4. instance:
  5. prefer-ip-address: true # 是否显示主机的IP
  6. #instance-id: ${spring.cloud.client.ipAddress}:${server.port} #将Status中的显示内容也以“IP:端口号”的形式显示
  7. client:
  8. service-url:
  9. defaultZone: http://localhost:8761/eureka/ # 指定Eureka服务端地址
  10. spring:
  11. application:
  12. name: microservice-eureka-user # 指定应用名称

(3)修改客户端Java代码。
  在项目的引导类上添加注解@EnableEurekaClient,该注解用于声明标注类是一个Eureka客户端组件,如文件4-7所示。
  文件4-7 Application.java

  1. package com.xc.xcserviceeurekauser;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @SpringBootApplication
  8. @EnableEurekaClient
  9. @RestController
  10. public class XcserviceEurekaUserApplication {
  11. @RequestMapping("/hello")
  12. public String home() {
  13. return "hello world!";
  14. }
  15. public static void main(String[] args) {
  16. SpringApplication.run(XcserviceEurekaUserApplication.class, args);
  17. }
  18. }

(4)启动应用,查看信息。
  完成上述配置后,分别启动服务器工程和客户端工程,在浏览器中访问地址http://local-host:8761/后,我们可以从Eureka的信息面板中看到注册的服务信息,如图4-5所示。
  
[外链图片转存失败(img-WiZFWg7q-1563022278362)(https://upload-images.jianshu.io/upload_images/13341631-44ae0e749eba128a.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

从图4-5中可以看出,服务已经成功注册到了注册中心,注册后的服务就可以直接被其他服务调用了。

发表评论

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

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

相关阅读