赞
踩
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>
在父项目中创建maven项目模块,名字为:cloud-eureka(自定义)
编写pom.xml文件,加入以下依赖
- <dependencies>
- <!--eureka server-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
编写application.yml
- server:
- port: 7001
- spring:
- application:
- name: eureka
- eureka:
- client:
- # 表示是否从Eureka Server获取注册的服务信息
- fetch-registry: false
- # 表示是否将自己注册到Eureka Server
- register-with-eureka: false
-
- logging:
- pattern:
- console: '%d{MM/dd HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
编写主启动类
- @SpringBootApplication
- @EnableEurekaServer//开启Eureka服务
- public class Eureka7001 {
- public static void main(String[] args) {
- SpringApplication.run(Eureka7001.class,args);
- }
- }
访问
http://localhost:7001/
在父项目中创建maven子项目cloud-eureka-pro
修改pom.xml
- <dependencies>
- <!-- Eureka client-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <!-- SpringMVC-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- lombok-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.22</version>
- </dependency>
- </dependencies>
编写application.yml
- server:
- port: 80
- spring:
- application:
- name: cloud-eureka-pro
- eureka:
- client:
- service-url:
- # Eureka server 地址
- defaultZone: http://localhost:7001/eureka/
编写主启动类
- @SpringBootApplication
- @EnableEurekaClient
- public class EurekaPro80 {
- public static void main(String[] args) {
- SpringApplication.run(EurekaPro80.class,args);
- }
- }
Pom.xml引入插件依赖【记得修改dockerHost的ip】
- <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>
打包成jar包
利用maven制作docker镜像【docker记得开启远程访问】
创建并启动容器
docker run -d -p 7001:7001 --name eureka 容器镜像名
访问
Ip:7001/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。