您现在的位置是:首页 >其他 >Eureka单机搭建网站首页其他

Eureka单机搭建

是狼也会孤独 2023-05-21 12:00:02
简介Eureka单机搭建

Eureka简介

Eureka

Spring Cloud Eureka 是Netflix 开发的注册发现组件,本身是一个基于 REST 的服务。提供注册与发现,同时还提供了负载均衡、故障转移等能力

Eureka3个角色

服务中心,服务提供者,服务消费者

Eureka Server:服务器端。它提供服务的注册和发现功能,即实现服务的治理。

Service Provider:服务提供者。它将自身服务注册到Eureka Server中,以便“服务消费者”能够通过服务器端提供的服务清单(注册服务列表)来调用它。

Service Consumer:服务消费者。它从 Eureka 获取“已注册的服务列表”,从而消费服务

项目架构

 

搭建父项目

创建Maven项目

编写pom.xml文件,添加以下内容

<packaging>pom</packaging>

<!-- 统一管理jar包版本 -->
<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-cloud.version>2021.0.0</spring-cloud.version>
    <spring-boot.version>2.6.3</spring-boot.version>
</properties>


<!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version  -->
<dependencyManagement>
    <dependencies>
        <!--spring boot 2.6.3-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--spring cloud 2021.0.0-->
        <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>

开启Run Dashboard面板

打开项目文件夹所在目录,找到.idea/workspace.xml 添加以下配置

 <component name="RunDashboard">

 <option name="ruleStates">

  <list>

   <RuleState>

    <option name="name" value="ConfigurationTypeDashboardGroupingRule" />

   </RuleState>

   <RuleState>

    <option name="name" value="StatusDashboardGroupingRule" />

   </RuleState>

  </list>

 </option>

 <option name="configurationTypes">

 <set>

  <option value="SpringBootApplicationConfigurationType" />

 </set>

</option>

</component>

搭建单机的Eureka

在父项目中创建maven项目模块,名字为:cloud-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">
    <parent>
        <artifactId>books</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>books-eureka</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--配置中心服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.6</version>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <configuration>
                    <imageName>livegoods/eurekaserver:1.0</imageName>
                    <baseImage>openjdk:11</baseImage>
                    <dockerHost>http://192.168.66.101:2375</dockerHost>
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <exposes>
                        <expose>8761</expose>
                    </exposes>
                    <resources>
                        <resources>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resources>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


编写application.yml

server:
  port: 8761
spring:
  application:
    name: books-eureka-server
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false

logging:
  pattern:
    console: '%d{MM/dd HH:mm:ss.SSS} %clr(%-5level) ---  [%-15thread] %cyan(%-50logger{50}):%msg%n'


编写主启动类

package fz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ServiceEureka8761 {
    public static void main(String[] args) {
        SpringApplication.run(ServiceEureka8761.class, args);
    }
}


访问

http://localhost:8761/

 

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。