SpringCloud学习笔记(三、创建父子项目、注册中心)

创建父子项目

创建父项目

在IDEA中创建一个maven项目:
SpringCloud学习笔记(三、创建父子项目、注册中心)_第1张图片pom.xml:



    4.0.0

    edu.hpu.springcloud
    springcloud
    1.0-SNAPSHOT
    springcloud
    
        eureka-server
    
    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
            
        
    


父项目的src用不上,可以删除。

创建子项目

SpringCloud学习笔记(三、创建父子项目、注册中心)_第2张图片SpringCloud学习笔记(三、创建父子项目、注册中心)_第3张图片pom.xml:




    
        springcloud
        edu.hpu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    eureka-server

    eureka-server
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    



创建启动类

EurekaServer 启动类。
这个类充当注册中心,用于注册各种微服务,以便于其他微服务找到和访问。
EurekaServer 本身就是个 Springboot 微服务, 所以它有 @SpringBootApplication 注解。
@EnableEurekaServer 表示这是个 EurekaServer 。

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

package edu.hpu.springcloud;

import cn.hutool.core.util.NetUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args){
        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);
    }
}

配置文件

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

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eureka-server

启动与访问

运行EurekaServeApplication,访问网址:http://localhost:8761/
SpringCloud学习笔记(三、创建父子项目、注册中心)_第4张图片看Instances currently registered with Eureka,
在这里插入图片描述现在是没有微服务的。


参考:

【1】、http://how2j.cn/k/springcloud/springcloud-eureka-server/2038.html#nowhere
【2】、http://how2j.cn/k/idea/idea-parent-child/2051.html

你可能感兴趣的:(SpringCloud)