zoukankan      html  css  js  c++  java
  • 微服务springcloud入门系列一(Eureka)

    最近在学习springcloud,感觉知识点比较多,所以特意写一个系列的文章来记录下来,如果大家发现问题,欢迎留言!

    本栗子使用的IDE是idea。

    这个栗子包含4个工程,分别是:

    1,父工程(springcloud-parent),它添加springboot的坐标和管理其他组件的依赖

    2,注册中心工程(eureka-server),它的作用类似一个中介,让服务提供者发布服务到这里来,然后消费者就可以定时来这里拉取服务提供者的清单,租用服务。

    3,服务工程(user-service),它提供服务。

    4,消费工程(user-consumer),它使用服务。

    首先在idea中创建好着4个工程,并且编写对应的文件。

    springcloud-parent的pom:

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.xiami</groupId>
        <artifactId>springcloud-parent</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <modules>
            <module>eureka-server</module>
            <module>user-service</module>
            <module>user-consumer</module>
        </modules>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencyManagement>
            <dependencies>
                <!-- springCloud -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    </project>

    eureka-server的pom:

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <parent>
            <artifactId>springcloud-parent</artifactId>
            <groupId>com.xiami</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>eureka-server</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        </dependencies>
    </project>

    eureka-server的application.properties:

    server.port=8888
    spring.application.name=eureka-server
    eureka.client.service-url.defaultZone=http://localhost:8888/eureka
    #这个项目是注册中心,不用再注册到注册中心上面去
    eureka.client.register-with-eureka=false
    #这个项目是注册中心,不用从注册中心中获取服务列表
    eureka.client.fetch-registry=false

    eureka-server的EurekaServerApplication:

    package com.xiami;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer//这个注解意思是启动Eureka注册中心
    @SpringBootApplication
    public class EurekaServerApplication {
        public static void main(String[] args) {
            System.out.println("EurekaServerApplication|run");
            SpringApplication.run(EurekaServerApplication.class,args);
        }
    }

     最后右键启动服务中心,并访问localhost:8888,显示如下图:

    微服务springcloud入门系列一(Eureka),大功告成!

  • 相关阅读:
    常见错误
    mac安装cocoapods
    WPS复制时删除超链接
    Network | 协议栈
    Leetcode | Remove Duplicates from Sorted Array I && II
    Leetcode | Search in Rotated Sorted Array I & II
    Leetcode | Jump Game I && II
    Leetcode | Combination Sum I && II
    Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
    Leetcode | Trapping Rain Water
  • 原文地址:https://www.cnblogs.com/qq2083587182/p/14681823.html
Copyright © 2011-2022 走看看