Eureka服务注册与服务发现

野性酷女 2022-11-03 05:55 117阅读 0赞

一、环境准备

首先创建了一个Maven环境。修改Pom文件,导入SpringCloud的依赖。用Eureka作为服务注册与发现的组件。

  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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.safesoft</groupId>
  5. <artifactId>main</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <name>main</name>
  8. <url>http://www.example.com</url>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.0.3.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. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <scope>test</scope>
  26. </dependency>
  27. </dependencies>
  28. <dependencyManagement>
  29. <dependencies>
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-dependencies</artifactId>
  33. <version>${spring-cloud.version}</version>
  34. <type>pom</type>
  35. <scope>import</scope>
  36. </dependency>
  37. </dependencies>
  38. </dependencyManagement>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>

二、创建服务注册中心 Eureka – Server

2.1 注册中心导入依赖

  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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <parent>
  4. <artifactId>main</artifactId>
  5. <groupId>com.safesoft</groupId>
  6. <version>1.0-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>server</artifactId>
  10. <dependencies>
  11. <dependency>
  12. <groupId>org.springframework.cloud</groupId>
  13. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  14. </dependency>
  15. </dependencies>
  16. </project>

2.2 编写配置文件

eureka server的配置文件application.yml
如果yml不显示树叶图标,请参考
IDEA创建yml文件不显示小树叶创建失败问题的解决方法

  1. #使用8761端口
  2. server:
  3. port: 8761
  4. eureka:
  5. instance:
  6. hostname: localhost
  7. client:
  8. #下面两个false说明自己是一个 Eureka Server
  9. registerWithEureka: false
  10. fetchRegistry: false
  11. #服务中心的地址
  12. serviceUrl:
  13. defaultZone: http://${ eureka.instance.hostname}:${ server.port}/eureka/
  14. #此服务器名称
  15. spring:
  16. application:
  17. name: eurka-server

2.3 编写启动类

@SpringBootApplication表明这个类是SpringBoot启动的入口。
@EnableEurekaServer 表明这是一个注册中心。

  1. package com.safesoft.server;
  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 ApplicationServer {
  8. public static void main(String[] args) {
  9. SpringApplication.run(ApplicationServer.class, args);
  10. }
  11. }

2.4 启动注册中心

启动后,在浏览器访问http://localhost:8761/,就能看到注册中心
在这里插入图片描述

三、服务提供者 Eureka - Client

刚才的注册中心项目不要关。然后再创建一个新的Module,同样右击项目最上级目录。New – Module – 选择Maven ,点击next – 输入提供者名字 client。

3.1 导入服务提供者项目的依赖

  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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <parent>
  4. <artifactId>main</artifactId>
  5. <groupId>com.safesoft</groupId>
  6. <version>1.0-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>client</artifactId>
  10. <dependencies>
  11. <!-- 客户端依赖 -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. </dependencies>
  21. </project>

3.2 编写配置文件

为服务提供者 Eureka - Client 编写 application.yml

  1. #当前项目部署的端口
  2. server:
  3. port: 8762
  4. #配置注册中心的位置,并把自己注册进去
  5. eureka:
  6. client:
  7. serviceUrl:
  8. defaultZone: http://localhost:8761/eureka/
  9. #当前项目名字
  10. spring:
  11. application:
  12. name: service-client

3.3 编写启动类

@SpringBootApplication注解是SpirngBoot项目启动的入口。
@EnableEurekaClient注解说明这是一个服务提供者Eureka–Client。

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

3.4 编写一个Controller

  1. package com.safesoft.client.web;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. public class IndexController {
  7. @Value("${server.port}")
  8. private String port;
  9. @RequestMapping("/hello")
  10. public String hello(String name) {
  11. return "hello " + name + " , This is a " + port;
  12. }
  13. }

3.5 启动服务提供者

启动以后,访问浏览器http://localhost:8761/,就能在注册中心中,找到刚才注册的服务提供者项目了。注意哦,注册中心项目Eureka–Server 与 服务提供者项目 Eureka–Client 这两个项目都要启动哦,先启动注册中心,再启动服务提供者。
在这里插入图片描述
访问Controller代码,输入http://localhost:8762/hello?name="zhangsanfeng"
结果如下
在这里插入图片描述

四、项目目录结构及代码

4.1项目目录结构

在这里插入图片描述

4.2源码下载地址

spring-cloud-01

参考文章
https://www.jianshu.com/p/19b6553eb603
https://blog.csdn.net/yanluandai1985/article/details/86625348
https://zhoutianyu.blog.csdn.net/article/details/89632721
https://blog.csdn.net/xiaojin21cen/article/details/86704954

发表评论

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

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

相关阅读