Spring

三)SpringCloud微服务搭建学习|服务注册(Eureka注册中心)

一、注册中心(Eureka Server)

搭建注册中心步骤
  • 新建maven模块,继承父pom,并命名为eureka-server

  • 引入spring-cloud-starter-netflix-eureka-server的jar包依赖

  • 添加注册中心启动类

  • 添加eureka注册中心配置文件(端口、注册地址等)

  • 启动并访问注册中心管理界面

1. eureka注册中心模块pom.xml配置
<?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>
    <parent>
        <groupId>com</groupId>
        <artifactId>longlonggo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.longlonggo</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring cloud</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies></project>
2. 启动类(EurekaServerApplication)设置
package com.eurekaserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication {

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

}
3. 配置文件(application.yml)设置
server:
  port: 8888

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

spring:
  application:
    name: eurka-server
4. 访问配置中心
# 访问地址http://localhost:8888/eureka/

因为原文用有道笔记编辑,特将笔记分享出来,有道笔记中持续更新,笔记地址:https://note.youdao.com/ynoteshare1/index.html?id=44ab1180e2d994218eae1617806486a7&type=notebook#/

留言

您的电子邮箱地址不会被公开。 必填项已用*标注