Spring Cloud(一):发现和注册服务(eureka)

墨蓝 2022-04-18 01:33 285阅读 0赞

最近的项目需要将原来的项目重构为Spring Boot项目,正好也能利用一些Spring Cloud的工具,就正好学习学习_(:з」∠*)_。
首先需要新建2个Spring Boot项目,一个作为服务注册中心,一个作为服务生产者/提供者。
如下
在这里插入图片描述
PS.我这里用的是Spring Boot 2.X版本

一.服务注册中心
新建Spring Boot项目SpringCloudServiceCenter
(1)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. <groupId>com.service.center</groupId>
  6. <artifactId>SpringCloundServiceCenter</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>SpringCloundServiceCenter</name>
  10. <description>com.service.center</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.1.0.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>Greenwich.M1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>
  34. <dependencyManagement>
  35. <dependencies>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-dependencies</artifactId>
  39. <version>${spring-cloud.version}</version>
  40. <type>pom</type>
  41. <scope>import</scope>
  42. </dependency>
  43. </dependencies>
  44. </dependencyManagement>
  45. <build>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. </plugin>
  51. </plugins>
  52. </build>
  53. <repositories>
  54. <repository>
  55. <id>spring-milestones</id>
  56. <name>Spring Milestones</name>
  57. <url>https://repo.spring.io/milestone</url>
  58. <snapshots>
  59. <enabled>false</enabled>
  60. </snapshots>
  61. </repository>
  62. </repositories>
  63. </project>

(2)application.properties配置

  1. #服务端口
  2. server.port=8761
  3. #服务名称
  4. eureka.instance.hostname=serviceCenter
  5. #禁止本身注册
  6. eureka.client.register-with-eureka=false
  7. #禁止本身注册
  8. eureka.client.fetch-registry=false
  9. #服务中心地址
  10. eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

(3)SpringCloundServiceCenterApplication.java启动文件中添加@EnableEurekaServer

  1. package com.service.center;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer
  7. public class SpringCloundServiceCenterApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringCloundServiceCenterApplication.class, args);
  10. }
  11. }

来表明该服务为服务发现和注册中心
整体结构
在这里插入图片描述
(4)配置完成后启动,然后访问http://localhost:8761/ 就能进入eureka server监控界面,可以看到红框中的信息,现在还没有服务注册。
在这里插入图片描述


二.服务生产者
新建spring boot项目SpringCloudServiceI
(1)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. <groupId>com.my.serviceI</groupId>
  6. <artifactId>SpringCloundServiceI</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>SpringCloundServiceI</name>
  10. <description>com.my.serviceI</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.1.0.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>Greenwich.M1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. </dependencies>
  38. <dependencyManagement>
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.cloud</groupId>
  42. <artifactId>spring-cloud-dependencies</artifactId>
  43. <version>${spring-cloud.version}</version>
  44. <type>pom</type>
  45. <scope>import</scope>
  46. </dependency>
  47. </dependencies>
  48. </dependencyManagement>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. <repositories>
  58. <repository>
  59. <id>spring-milestones</id>
  60. <name>Spring Milestones</name>
  61. <url>https://repo.spring.io/milestone</url>
  62. <snapshots>
  63. <enabled>false</enabled>
  64. </snapshots>
  65. </repository>
  66. </repositories>
  67. </project>

(2)application.properties配置

  1. server.servlet.context-path=/myServiceI
  2. server.port=8762
  3. spring.application.name=myServiceI #服务名,在注册时所用
  4. eureka.client.service-url.defautZone=http://serviceCenter:8761/eureka/

(3)在启动文件SpringCloundServiceIApplication.java中添加@EnableEurekaClient

  1. package com.my.serviceI;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.context.config.annotation.RefreshScope;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. @SpringBootApplication
  7. @EnableEurekaClient
  8. public class SpringCloundServiceIApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(SpringCloundServiceIApplication.class, args);
  11. }
  12. }

(4)新建一个controller文件,提供服务
例ServiceApiController.java

  1. package com.my.serviceI.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. @Controller
  6. @RequestMapping(value="/Api")
  7. public class ServiceApiController {
  8. @ResponseBody
  9. @RequestMapping(value="/getInfo")
  10. public String getInfo() {
  11. return "123+";
  12. }
  13. }

整体结构如下
在这里插入图片描述
可以启动测试下是否能访问接口服务,因为spring boot会内置一个tomcat容器,且在启动时会省略掉项目名,所以我这里在application.properties中配置server.servlet.context-path=/myServiceI 来把项目名加上了,所以现在接口的访问路径为http://localhost:8762/myServiceI/Api/getInfo
在这里插入图片描述
然后刷新下eureka server监控界面,就能看到刚才的服务了,能从信息中得知服务的名字和端口号,上面红条是另一个服务。
在这里插入图片描述

发表评论

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

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

相关阅读