Spring Boot集成Spring Cloud Eureka实现服务注册与发现

Spring Boot集成Spring Cloud Eureka实现服务注册与发现

大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!

在微服务架构中,服务的注册与发现是构建分布式系统的基础。Spring Cloud Eureka是一个基于REST的服务,用于服务注册与发现。通过Spring Boot集成Spring Cloud Eureka,我们可以轻松实现服务的注册与发现。

环境准备

首先,确保你的开发环境已经安装了Java 8或更高版本,以及Maven或Gradle作为构建工具。

搭建Eureka Server

  1. 创建一个Spring Boot项目,添加spring-cloud-starter-netflix-eureka-server依赖。
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
  1. 在主类上添加@EnableEurekaServer注解,启动Eureka Server。
package cn.juwatech.eureka;

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. 配置application.properties文件,设置Eureka Server的基本配置。
server.port=8761
eureka.instance.hostname=localhost

开发服务提供者

  1. 创建一个新的Spring Boot项目,添加spring-cloud-starter-netflix-eureka-client依赖。
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
  1. 在主类上添加@EnableDiscoveryClient注解,使应用成为Eureka Client。
package cn.juwatech.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}
  1. 配置application.properties文件,设置服务提供者的基本信息以及Eureka Client的配置。
server.port=8080
spring.application.name=my-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

开发服务消费者

  1. 创建服务消费者项目,同样添加spring-cloud-starter-netflix-eureka-client依赖。

  2. 在主类上添加@EnableDiscoveryClient注解。

  3. 配置application.properties文件,设置服务消费者的基本信息以及Eureka Client的配置。

  4. 使用@LoadBalanced注解的RestTemplate或WebClient来调用服务。

package cn.juwatech.client;

import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ClientConfig {

    @Bean
    public RestTemplateCustomizer restTemplateCustomizer(LoadBalancerClient loadBalancerClient) {
        return restTemplate -> {
            restTemplate.setRequestFactory(new LoadBalancerHttpRequestFactory(loadBalancerClient));
        };
    }
}

服务调用示例

  1. 在服务提供者中创建一个RestController。
package cn.juwatech.service.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyServiceController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello from my-service!";
    }
}
  1. 在服务消费者中调用服务提供者的接口。
package cn.juwatech.client.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MyClientController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getHello")
    public String getHello() {
        return restTemplate.getForObject("http://my-service/hello", String.class);
    }
}

总结

通过上述步骤,我们成功地搭建了一个基于Spring Cloud Eureka的服务注册与发现系统。服务提供者在启动时会向Eureka Server注册自己的信息,服务消费者通过Eureka Server获取服务列表,并进行负载均衡调用。这种方式大大简化了微服务架构中的服务管理。

本文著作权归聚娃科技微赚淘客系统开发者团队,转载请注明出处!

posted @ 2024-08-16 14:41  省赚客开发者团队  阅读(2)  评论(0编辑  收藏  举报