当前位置:   article > 正文

Eureka单机搭建_eureka本地搭建服务

eureka本地搭建服务

Eureka详解

Eureka

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

Eureka3个角色

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

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

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

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

springcloud架构

搭建父项目

创建Maven项目

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

  1. <packaging>pom</packaging>
  2. <!-- 统一管理jar包版本 -->
  3. <properties>
  4.     <maven.compiler.source>11</maven.compiler.source>
  5.     <maven.compiler.target>11</maven.compiler.target>
  6.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  7.     <spring-cloud.version>2021.0.0</spring-cloud.version>
  8.     <spring-boot.version>2.6.3</spring-boot.version>
  9. </properties>
  10. <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version  -->
  11. <dependencyManagement>
  12.     <dependencies>
  13.         <!--spring boot 2.6.3-->
  14.         <dependency>
  15.             <groupId>org.springframework.boot</groupId>
  16.             <artifactId>spring-boot-starter-parent</artifactId>
  17.             <version>${spring-boot.version}</version>
  18.             <type>pom</type>
  19.             <scope>import</scope>
  20.         </dependency>
  21.         <!--spring cloud 2021.0.0-->
  22.         <dependency>
  23.             <groupId>org.springframework.cloud</groupId>
  24.             <artifactId>spring-cloud-dependencies</artifactId>
  25.             <version>${spring-cloud.version}</version>
  26.             <type>pom</type>
  27.             <scope>import</scope>
  28.         </dependency>
  29.     </dependencies>
  30. </dependencyManagement>

开启Run Dashboard面板

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

  1.  <component name="RunDashboard">
  2.  <option name="ruleStates">
  3.   <list>
  4.    <RuleState>
  5.     <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
  6.    </RuleState>
  7.    <RuleState>
  8.     <option name="name" value="StatusDashboardGroupingRule" />
  9.    </RuleState>
  10.   </list>
  11.  </option>
  12.  <option name="configurationTypes">
  13.  <set>
  14.   <option value="SpringBootApplicationConfigurationType" />
  15.  </set>
  16. </option>
  17. </component>

搭建单机的Eureka

在父项目中创建maven项目模块,名字为:cloud-eureka(自定义)
编写pom.xml文件,加入以下依赖

  1. <dependencies>
  2. <!--eureka server-->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  6. </dependency>
  7. </dependencies>

编写application.yml

  1. server:
  2. port: 7001
  3. spring:
  4. application:
  5. name: eureka
  6. eureka:
  7. client:
  8. # 表示是否从Eureka Server获取注册的服务信息
  9. fetch-registry: false
  10. # 表示是否将自己注册到Eureka Server
  11. register-with-eureka: false
  12. logging:
  13. pattern:
  14. console: '%d{MM/dd HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'


编写主启动类

  1. @SpringBootApplication
  2. @EnableEurekaServer//开启Eureka服务
  3. public class Eureka7001 {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Eureka7001.class,args);
  6. }
  7. }

访问

http://localhost:7001/

子项目整合eureka

在父项目中创建maven项目cloud-eureka-pro
修改pom.xml

  1. <dependencies>
  2.     <!-- Eureka client-->
  3.     <dependency>
  4.         <groupId>org.springframework.cloud</groupId>
  5.         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  6.     </dependency>
  7.     <!-- SpringMVC-->
  8.     <dependency>
  9.         <groupId>org.springframework.boot</groupId>
  10.         <artifactId>spring-boot-starter-web</artifactId>
  11.     </dependency>
  12.     <!-- lombok-->
  13.     <dependency>
  14.         <groupId>org.projectlombok</groupId>
  15.         <artifactId>lombok</artifactId>
  16.         <version>1.18.22</version>
  17.     </dependency>
  18. </dependencies>

编写application.yml

  1. server:
  2.   port: 80
  3. spring:
  4.   application:
  5.     name: cloud-eureka-pro
  6. eureka:
  7.   client:
  8.     service-url:
  9.       # Eureka server 地址
  10.       defaultZone: http://localhost:7001/eureka/

编写主启动类

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class EurekaPro80 {
  4.     public static void main(String[] args) {
  5.         SpringApplication.run(EurekaPro80.class,args);
  6.     }
  7. }

制作eureka的docker镜像并运行

Pom.xml引入插件依赖【记得修改dockerHost的ip】

  1. <build>
  2.     <plugins>
  3.         <plugin>
  4.             <groupId>org.springframework.boot</groupId>
  5.             <artifactId>spring-boot-maven-plugin</artifactId>
  6.             <version>2.5.6</version>
  7.         </plugin>
  8.         <plugin>
  9.             <groupId>com.spotify</groupId>
  10.             <artifactId>docker-maven-plugin</artifactId>
  11.             <configuration>
  12.                 <imageName>livegoods/eurekaserver:1.0</imageName>
  13.                 <baseImage>openjdk:11</baseImage>
  14.                 <dockerHost>http://192.168.66.101:2375</dockerHost>
  15.                 <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
  16.                 <exposes>
  17.                     <expose>8761</expose>
  18.                 </exposes>
  19.                 <resources>
  20.                     <resources>
  21.                         <targetPath>/</targetPath>
  22.                         <directory>${project.build.directory}</directory>
  23.                         <include>${project.build.finalName}.jar</include>
  24.                     </resources>
  25.                 </resources>
  26.             </configuration>
  27.         </plugin>
  28.     </plugins>
  29. </build>

打包成jar包

利用maven制作docker镜像【docker记得开启远程访问】

创建并启动容器

docker run -d -p 7001:7001 --name eureka 容器镜像名

访问

Ip:7001/

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/839048
推荐阅读
相关标签
  

闽ICP备14008679号