#yyds干货盘点# springcloud整合eureka实现服务注册与发现
Posted 灰太狼_cxh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#yyds干货盘点# springcloud整合eureka实现服务注册与发现相关的知识,希望对你有一定的参考价值。
springcloud整合eureka实现服务注册与发现
1.项目目录:
2.代码实现:
创建eureka父类服务,pom添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-cloud-dependencies.version>2021.0.0</spring-cloud-dependencies.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>$spring-cloud-dependencies.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
创建module模块eureka-server服务
添加依赖
<parent>
<groupId>com.cxh</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
yml配置
server:
port: 8001
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:$server.port/eureka/
启动器添加注解
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication
public static void main(String[] args)
SpringApplication.run(ServerApplication.class, args);
创建module模块eureka-client服务
添加依赖
<parent>
<groupId>com.cxh</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
yml配置
server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
spring:
application:
name: eureka-client
启动器添加注解
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication
public static void main(String[] args)
SpringApplication.run(EurekaClientApplication.class, args);
3.实现效果:
运行eureka-server, eureka-client项目, 浏览器打开http://localhost:8001/
以上是关于#yyds干货盘点# springcloud整合eureka实现服务注册与发现的主要内容,如果未能解决你的问题,请参考以下文章
#yyds干货盘点# springcloud整合gateway实现网关全局过滤器功能
#yyds干货盘点# springcloud整合stream实现同一通道根据消息内容分发不同的消费逻辑
#yyds干货盘点# springcloud整合ribbon实现服务负载均衡
#yyds干货盘点# springcloud整合eureka实现服务注册与发现