SpringCloud之服务注册与发现

阳光穿透心脏的1/2处 2022-03-16 09:28 378阅读 0赞

看了一段时间的springcloud,虽然目前公司架构用的是dubbo,但是感觉springcloud的rest风格的调用模式,跨平台效果更好。现在介绍下springcloud的使用。
官网:https://spring.io/projects/spring-cloud

  • 什么是Spring Cloud?

    Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,主节点选举, 分布式session, 集群状态)。协调分布式环境中各个系统,为各类服务提供模板性配置。使用Spring Cloud, 开发人员可以搭建实现了这些样板的应用,并且在任何分布式环境下都能工作得非常好,小到笔记本电脑, 大到数据中心和云平台。

    Spring Cloud官网的定义比较抽象,我们可以从简单的东西开始。Spring Cloud是基于Spring Boot的, 最适合用于管理Spring Boot创建的各个微服务应用。要管理分布式环境下的各个Spring Boot微服务,必然存在服务的注册问题。所以我们先从服务的注册谈起。既然是注册,必然有个管理注册中心的服务器,各个在Spring Cloud管理下的Spring Boot应用就是需要注册的client

    Spring Cloud使用erureka server, 然后所有需要访问配置文件的应用都作为一个erureka client注册上去。eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。
    下面我们编写一个注册中心服务demo,和一个server

  • 创建服务注册中心

创建eureka-server-demo(springboot结构)

  • pom配置如下:

    <?xml version=”1.0” encoding=”UTF-8”?>
    <project xmlns=”http://maven.apache.org/POM/4.0.0“

    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. 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.emine.springcloud</groupId>
    5. <artifactId>eureka-server-demo</artifactId>
    6. <version>1.0-SNAPSHOT</version>
    7. <parent>
    8. <groupId>org.springframework.boot</groupId>
    9. <artifactId>spring-boot-starter-parent</artifactId>
    10. <version>1.5.2.RELEASE</version>
    11. <relativePath/> <!-- lookup parent from repository -->
    12. </parent>
    13. <properties>
    14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    15. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    16. <java.version>1.8</java.version>
    17. </properties>
    18. <dependencies>
    19. <dependency>
    20. <groupId>org.springframework.cloud</groupId>
    21. <artifactId>spring-cloud-starter-eureka-server</artifactId>
    22. </dependency>
    23. <dependency>
    24. <groupId>org.springframework.boot</groupId>
    25. <artifactId>spring-boot-starter-test</artifactId>
    26. <scope>test</scope>
    27. </dependency>
    28. </dependencies>
    29. <dependencyManagement>
    30. <dependencies>
    31. <dependency>
    32. <groupId>org.springframework.cloud</groupId>
    33. <artifactId>spring-cloud-dependencies</artifactId>
    34. <version>Dalston.RC1</version>
    35. <type>pom</type>
    36. <scope>import</scope>
    37. </dependency>
    38. </dependencies>
    39. </dependencyManagement>
    40. <build>
    41. <plugins>
    42. <plugin>
    43. <groupId>org.springframework.boot</groupId>
    44. <artifactId>spring-boot-maven-plugin</artifactId>
    45. </plugin>
    46. </plugins>
    47. </build>
    48. <repositories>
    49. <repository>
    50. <id>spring-milestones</id>
    51. <name>Spring Milestones</name>
    52. <url>https://repo.spring.io/milestone</url>
    53. <snapshots>
    54. <enabled>false</enabled>
    55. </snapshots>
    56. </repository>
    57. </repositories>
  1. </project>
  • application.yml内容:

    server:
    port: 8761

    eureka:
    instance:

    1. hostname: localhost

    client:

    1. registerWithEureka: false
    2. fetchRegistry: false
    3. serviceUrl:
    4. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 启动类:EurekaServerApplication.java

    package com.emine.springcloud;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(EurekaServerApplication.class, args);
    3. }

    }

到这里,一个注册中心已经创建好了。启动项目
访问:http://localhost:8762/,进入注册中心控制台
在这里插入图片描述

  • 注册服务demo

创建应用:service-demo

  • pom如下:

    <?xml version=”1.0” encoding=”UTF-8”?>
    <project xmlns=”http://maven.apache.org/POM/4.0.0“

    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. 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.emine.springcloud</groupId>
    5. <artifactId>service-demo</artifactId>
    6. <version>1.0-SNAPSHOT</version>
    7. <parent>
    8. <groupId>org.springframework.boot</groupId>
    9. <artifactId>spring-boot-starter-parent</artifactId>
    10. <version>1.5.2.RELEASE</version>
    11. <relativePath/> <!-- lookup parent from repository -->
    12. </parent>
    13. <properties>
    14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    15. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    16. <java.version>1.8</java.version>
    17. </properties>
    18. <dependencies>
    19. <dependency>
    20. <groupId>org.springframework.cloud</groupId>
    21. <artifactId>spring-cloud-starter-eureka</artifactId>
    22. </dependency>
    23. <dependency>
    24. <groupId>org.springframework.boot</groupId>
    25. <artifactId>spring-boot-starter-web</artifactId>
    26. </dependency>
    27. <dependency>
    28. <groupId>org.springframework.boot</groupId>
    29. <artifactId>spring-boot-starter-test</artifactId>
    30. <scope>test</scope>
    31. </dependency>
    32. </dependencies>
    33. <dependencyManagement>
    34. <dependencies>
    35. <dependency>
    36. <groupId>org.springframework.cloud</groupId>
    37. <artifactId>spring-cloud-dependencies</artifactId>
    38. <version>Dalston.RC1</version>
    39. <type>pom</type>
    40. <scope>import</scope>
    41. </dependency>
    42. </dependencies>
    43. </dependencyManagement>
    44. <build>
    45. <plugins>
    46. <plugin>
    47. <groupId>org.springframework.boot</groupId>
    48. <artifactId>spring-boot-maven-plugin</artifactId>
    49. </plugin>
    50. </plugins>
    51. </build>
    52. <repositories>
    53. <repository>
    54. <id>spring-milestones</id>
    55. <name>Spring Milestones</name>
    56. <url>https://repo.spring.io/milestone</url>
    57. <snapshots>
    58. <enabled>false</enabled>
    59. </snapshots>
    60. </repository>
    61. </repositories>
  1. </project>
  • application.yml

    eureka:
    client:

    1. serviceUrl:
    2. defaultZone: http://localhost:8761/eureka/

    server:
    port: 8763
    spring:
    application:

    1. name: service-demo

注意:8763是本程序http启动端口,访问时要用8763

  • 启动类:ServiceDemoApplication.java

    package com.emine.springcloud;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    @SpringBootApplication
    @EnableEurekaClient
    @RestController
    public class ServiceDemoApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(ServiceDemoApplication.class, args);
    3. }
    4. @Value("${server.port}")
    5. String port;
    6. @RequestMapping("/service")
    7. public String home(@RequestParam String name) {
    8. return "this is server,port:"+port;
    9. }

    }

  • 启动程序,访问:http://localhost:8763/service?name=emine
    在这里插入图片描述

发表评论

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

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

相关阅读