搭建eureka-server

本文介绍了如何使用Spring Cloud创建Eureka Server,并配置服务注册与发现。通过详细步骤和配置文件展示,读者将学会如何在Spring Boot项目中启用Eureka服务注册中心,以及客户端如何进行服务发现。

创建一个子模块用来搭建eureka-server

依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

给eureka-server服务编写一个启动类,一定要添加一个@EnableEurekaServer注解,开启eureka的注册中心功能

配置文件中写:

server:
  port: 10086
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url: 
      defaultZone: http://127.0.0.1:10086/eureka

服务端完成

服务注册:

先导入依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置文件中写:

spring:
  application:
    name: userservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

服务发现:

先导入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置文件

spring:
  application:
    name: orderservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

在order-service的OrderApplication中,给RestTemplate这个Bean添加一个@LoadBalanced注解

修改路径为

 

首先,你需要创建一个新的Spring Boot项目,例如命名为`hystrix-demo`。这个项目将作为客户端,依赖于Eureka Server来实现服务发现。以下是步骤: 1. **初始化Hystrix-Demo项目**: - 使用Maven或Gradle初始化新项目,选择合适的模板,如Spring Initializr网站生成一个基本的Spring Boot Web应用项目结构。 - 添加必要的依赖,包括Spring Cloud Hystrix(用于处理断路器和熔断机制)、Spring Cloud Netflix Eureka Client(用于与Eureka Server通信),以及Spring Cloud Config(如果需要外部配置管理)。 ```xml <!-- Maven --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> </dependencies> // Gradle implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' implementation 'org.springframework.cloud:spring-cloud-starter-hystrix' ``` 2. **添加Eureka-server依赖**: 如果Eureka-server项目已经存在并且使用的是2.2.1版本,将其作为一个远程仓库或者本地模块引入到hystrix-demo的pom.xml或build.gradle文件中。确保eureka-server提供了一个可用的注册中心服务。 3. **配置Eureka Client**: 在`hystrix-demo`项目的application.yml或application.properties中配置Eureka Client的相关信息,比如Eureka服务器的URL地址、服务名等。 ```yaml eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: hostname: localhost appname: your-app-name ``` 4. **启动和测试**: - 启动eureka-server项目,确认其正常运行。 - 配置好后,运行hystrix-demo项目,它应该能够自动去Eureka Server注册并从那里获取其他微服务的信息。 完成以上步骤后,你就成功地在hystrix-demo项目中集成了一个依赖于eureka-server的子项目,并实现了服务发现功能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值