SpringCloud系列教材 (五)- 服务注册中心

 

步骤1:先运行,看到效果,再学习
步骤2:模仿和排错
步骤3:创建父项目
步骤4:创建子项目
步骤5:pom.xml
步骤6:EurekaServerApplication
步骤7:application.yml
步骤8:启动并访问

步骤 1 : 先运行,看到效果,再学习

老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 
运行 EurekaServerApplication,并访问:
http://127.0.0.1:8761/

先运行,看到效果,再学习

步骤 2 : 模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

步骤 3 : 创建父项目

不会在 eclipse 或者 idea 里创建父子项目的同学请先去预热:父子项目
创建好了之后把 src 目录删了,因为父项目里用不到。
一下是 pom.xml 文件。
这个pom里有几点重要信息:
1. 依赖 springboot 版本是 2.0.3
2. 有基于 hutool 的依赖, hutool 是一个工具类,用起来很方便,这是 hutool 系列教材
3. springcloud 用的版本是 Finchley

创建父项目

<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>cn.how2j.springcloud</groupId>

  <artifactId>springcloud</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>springcloud</name>

  <description>springcloud</description>

  <packaging>pom</packaging>

 

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.3.RELEASE</version>

        <relativePath/>

    </parent>

 

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.8</java.version>

        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

    </properties>

           

    <dependencies>

      <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-test</artifactId>

          <scope>test</scope>

      </dependency>

      <dependency>

        <groupId>cn.hutool</groupId>

        <artifactId>hutool-all</artifactId>

        <version>4.3.1</version>

      </dependency>

    </dependencies> 

 

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>${spring-cloud.version}</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

        </dependencies>

    </dependencyManagement>

 

</project>

步骤 4 : 创建子项目

不会在 eclipse 或者 idea 里创建父子项目的同学请先去预热:父子项目
创建子项目 eureka-server

创建子项目

步骤 5 : pom.xml

子项目 pom.xml ,增加 spring-cloud-starter-netflix-eureka-server jar 包

<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>

  <parent>

    <groupId>cn.how2j.springcloud</groupId>

    <artifactId>springcloud</artifactId>

    <version>0.0.1-SNAPSHOT</version>

  </parent>

  <artifactId>eurekaServer</artifactId>

   

    <dependencies>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

        </dependency>

    </dependencies>

       

</project>

步骤 6 : EurekaServerApplication

EurekaServer 启动类。
这是一个 EurekaServer ,它扮演的角色是注册中心,用于注册各种微服务,以便于其他微服务找到和访问。 所以 Eureka 这个单词是 “找到啦” 的意思。
EurekaServer 本身就是个 Springboot 微服务, 所以它有 @SpringBootApplication 注解。
@EnableEurekaServer 表示这是个 EurekaServer 。
启动的时候,端口号没有在配置文件里,而是直接放在代码里,这么做是为了提示同学这个端口号是否被占用了,否则有时候端口号被占用了,同学老是启动不了,搞得自己晕头转向的~

NetUtil 是 Hutool 的工具,在父项目的 pom.xml 里已经依赖了。

EurekaServerApplication

package cn.how2j.springcloud;

 

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

 

import cn.hutool.core.util.NetUtil;

 

@SpringBootApplication

@EnableEurekaServer

public class EurekaServerApplication {

     

    public static void main(String[] args) {

        //8761 这个端口是默认的,就不要修改了,后面的子项目,都会访问这个端口。

        int port = 8761;

        if(!NetUtil.isUsableLocalPort(port)) {

            System.err.printf("端口%d被占用了,无法启动%n", port );

            System.exit(1);

        }

        new SpringApplicationBuilder(EurekaServerApplication.class).properties("server.port="+ port).run(args);

    }

}

步骤 7 : application.yml

配置文件,提供 eureka 的相关信息。
hostname: localhost 表示主机名称。
registerWithEureka:false. 表示是否注册到服务器。 因为它本身就是服务器,所以就无需把自己注册到服务器了。
fetchRegistry: false. 表示是否获取服务器的注册信息,和上面同理,这里也设置为 false。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 自己作为服务器,公布出来的地址。 比如后续某个微服务要把自己注册到 eureka server, 那么就要使用这个地址: http://localhost:8761/eureka/

name: eurka-server 表示这个微服务本身的名称是 eureka-server

application.yml

eureka:

  instance:

    hostname: localhost

  client:

    registerWithEureka: false

    fetchRegistry: false

    serviceUrl:

      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

spring:

  application:

    name: eureka-server

步骤 8 : 启动并访问

运行 EurekaServerApplication,并访问:
http://127.0.0.1:8761/
这就是注册中心的管理界面,主要看 :Instances currently registered with Eureka, 可以发现信息是:No instances available。
这表示 暂时还没有微服务注册进来。
接下来的课程,就会讲解如何注册微服务进来啦。

启动并访问


更多内容,点击了解: https://how2j.cn/k/springcloud/springcloud-eureka-server/2038.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值