SpringCloud学习(五)- 服务注册中心

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

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

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

SpringCloud学习(五)- 服务注册中心_第1张图片

步骤 2 : 模仿和排错

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

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

步骤 3 : 创建父项目

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

SpringCloud学习(五)- 服务注册中心_第2张图片

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

  4.0.0

  cn.how2j.springcloud

  springcloud

  0.0.1-SNAPSHOT

  springcloud

  springcloud

  pom

 

    

        org.springframework.boot

        spring-boot-starter-parent

        2.0.3.RELEASE

        

    

 

    

        UTF-8

        UTF-8

        1.8

        Finchley.RELEASE

    

           

    

      

          org.springframework.boot

          spring-boot-starter-test

          test

      

      

        cn.hutool

        hutool-all

        4.3.1

      

    

 

    

        

            

                org.springframework.cloud

                spring-cloud-dependencies

                ${spring-cloud.version}

                pom

                import

            

        

    

 

步骤 4 : 创建子项目

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

SpringCloud学习(五)- 服务注册中心_第3张图片

步骤 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.0modelVersion>

  <parent>

    <groupId>cn.how2j.springcloudgroupId>

    <artifactId>springcloudartifactId>

    <version>0.0.1-SNAPSHOTversion>

  parent>

  <artifactId>eurekaServerartifactId>

   

    <dependencies>

        <dependency>

            <groupId>org.springframework.cloudgroupId>

            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>

        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

SpringCloud学习(五)- 服务注册中心_第4张图片

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。
这表示 暂时还没有微服务注册进来。
接下来的课程,就会讲解如何注册微服务进来啦。

SpringCloud学习(五)- 服务注册中心_第5张图片

 

你可能感兴趣的:(Java)