【微服务架构 - 08 - Spring Cloud】03 创建服务提供者

约定不等于承诺〃 2022-03-22 15:14 288阅读 0赞

概述


当 Client 向 Server 注册时,它会提供一些元数据,例如主机和端口,URL,主页等。
Eureka Server 从每个 Client 实例接受心跳消息。如果心跳超时,则通常将该实例从注册 Server 中删除。

POM


  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.yuu</groupId>
  7. <artifactId>hello-spring-cloud-dependencies</artifactId>
  8. <version>1.0.0-SNAPSHOT</version>
  9. <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
  10. </parent>
  11. <artifactId>hello-spring-cloud-service-admin</artifactId>
  12. <packaging>jar</packaging>
  13. <name>hello-spring-cloud-service-admin</name>
  14. <inceptionYear>2019-Now</inceptionYear>
  15. <dependencies>
  16. <!-- Spring Boot Begin -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <scope>test</scope>
  21. </dependency>
  22. <!-- Spring Boot End -->
  23. <!-- Spring Cloud Begin -->
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  27. </dependency>
  28. <!-- Spring Cloud End -->
  29. </dependencies>
  30. <build>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-maven-plugin</artifactId>
  35. <configuration>
  36. <mainClass>com.funtl.hello.spring.cloud.service.admin.ServiceAdminApplication</mainClass>
  37. </configuration>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

Application


通过注解 @EnableEurekaClient 表明自己是一个 Eureka Client

  1. package com.yuu.hello.spring.cloud.service.admin;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. /**
  6. * @Classname ServiceAdminApplication
  7. * @Date 2019/1/28 22:50
  8. * @Created by Yuu
  9. */
  10. @SpringBootApplication
  11. @EnableEurekaClient
  12. public class ServiceAdminApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(ServiceAdminApplication.class, args);
  15. }
  16. }

application.yml


  1. spring:
  2. application:
  3. name: hello-spring-cloud-service-admin
  4. server:
  5. port: 8762
  6. eureka:
  7. client:
  8. servcieUrl:
  9. defaultZone: http://localhost:8761/eureka/

注意: 需要指明 spring.application.name,因为这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个 name

Controller


  1. package com.yuu.hello.spring.cloud.service.admin.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @Classname AdminController
  9. * @Date 2019/1/28 23:00
  10. * @Created by Yuu
  11. */
  12. @RestController
  13. public class AdminController {
  14. @Value("${server.port}")
  15. private String port;
  16. @RequestMapping(value = "hi", method = RequestMethod.GET)
  17. public String sayHi(@RequestParam(value = "message") String message) {
  18. return String.format("Hi, your message is : %s : i am from port: %s", message, port);
  19. }
  20. }

启动工程,打开 http://localhost:8761,即 Eureka Server 的网址

在这里插入图片描述

你会发现一个服务已经注册在服务中了,服务名为 HELLO-SPRING-CLOUD-SERVICE-ADMIN ,端口为 8762

这是打开 http://localhost:8762/hi?message=HelloSpringCloud,你会在浏览器上看到:

在这里插入图片描述

发表评论

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

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

相关阅读