Spring Cloud集成Eurake

Spring Cloud 集成 Eureka 是一个常见的微服务架构实现,用于服务发现和注册。Eureka 是 Netflix 开源的一个服务注册和发现工具,Spring Cloud Netflix 提供了对 Eureka 的支持。下面是如何在 Spring Cloud 项目中集成 Eureka 的步骤:

先创建一个maven项目父项目模块,然后在父模块下创建各种服务

1. 创建 Eureka Server

首先,你需要创建一个 Eureka Server,它将负责管理服务注册表并处理服务实例的注册和注销。

1.1 引入依赖

在你的 pom.xml 中引入 Eureka Server 的依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
1.2 配置 Eureka Server

在主应用类上添加 @EnableEurekaServer 注解,使该应用成为 Eureka Server:

package com.example.eurekaserver;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
1.3 配置文件

application.ymlapplication.properties 文件中添加配置:

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  server:
    enable-self-preservation: false

这将配置 Eureka Server 在 localhost:8761 上运行,并禁用自我注册。

2. 创建 Eureka Client

现在,你需要创建一个或多个 Eureka Client,即你的微服务应用,它们将向 Eureka Server 注册自己,并从中获取其他服务的地址。

2.1 引入依赖

在你的 pom.xml 中引入 Eureka Client 的依赖:

<dependencies>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
2.2 配置 Eureka Client

在你的微服务主应用类上添加 @EnableEurekaClient 注解,使其成为 Eureka Client:

package com.example.eurekaclient;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}
2.3 配置文件

application.ymlapplication.properties 中配置 Eureka Client:

server:
  port: 8080

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true
    instance-id: eurake-client1
spring:
  application:
    name: eurake-client

这个配置文件指定 Eureka Server 的地址为 http://localhost:8761/eureka/,并将服务实例注册到该地址。

3. 启动 Eureka Server 和 Client

  1. 启动 Eureka Server(在 localhost:8761 运行)。
  2. 启动 Eureka Client,客户端应用会自动注册到 Eureka Server。
  3. 你可以访问 http://localhost:8761 查看 Eureka Dashboard,看到所有注册的服务实例。

4. 调用其他服务(可选)

你可以使用 @LoadBalanced 注解的 RestTemplate 或 Feign 客户端来调用其他已注册的服务。

使用 RestTemplate
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MyService {

    @Autowired
    private RestTemplate restTemplate;

    public String callService() {
        return restTemplate.getForObject("http://SERVICE-NAME/endpoint", String.class);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
使用 Feign 客户端
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "SERVICE-NAME")
public interface MyFeignClient {

    @GetMapping("/endpoint")
    String callService();
}

总结

以上步骤详细介绍了如何在 Spring Cloud 项目中集成 Eureka,包含了 Eureka Server 和 Eureka Client 的配置及使用。通过这些配置,你可以实现微服务之间的自动发现和通信。

Spring Cloud 中,Eureka 是一种服务发现框架,可以让微服务之间相互注册和发现。具体来说,一个微服务可以将自己注册到 Eureka 服务器上,其他微服务可以通过 Eureka 服务器来发现并调用该服务。下面是 Spring Cloud 中使用 Eureka 进行相互注册的步骤: 1. 引入 Eureka 相关依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ``` 2. 在应用程序的配置文件中配置 Eureka 服务器地址: ```yaml eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 3. 在应用程序的启动类上添加 `@EnableDiscoveryClient` 注解,表示该应用程序是一个 Eureka 客户端: ```java @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 在应用程序的配置文件中配置应用程序的名称: ```yaml spring: application: name: my-service ``` 5. 在应用程序中使用 `@RestController` 注解定义一个 RESTful API: ```java @RestController public class MyController { @GetMapping("/hello") public String hello() { return "Hello, world!"; } } ``` 6. 启动应用程序,访问 `http://localhost:8761` 可以看到 Eureka 服务器的管理界面,可以看到该应用程序已经注册到了 Eureka 服务器上。 7. 在其他微服务中,可以通过 `@Autowired` 注解来注入 `DiscoveryClient` 对象,然后使用该对象来发现并调用其他微服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御风行云天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值