Spring Cloud系列之 第三篇:服务提供者Eureka + 服务消费者(rest + Ribbon)

引言

在微服务架构中,服务提供者和服务消费者是微服务系统的两个主要角色。服务提供者负责提供特定的服务,服务消费者则通过调用服务提供者的接口来获得相应的功能。本文将介绍如何在Spring Cloud中使用Eureka作为服务注册中心,并通过RestTemplate和Ribbon来实现服务消费。

第一部分:服务提供者的配置

  1. 引入依赖

在服务提供者的pom.xml文件中添加以下依赖:

<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>
  1. 配置文件

在服务提供者的application.properties文件中添加以下配置:

spring.application.name=my-service
server.port=8081
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

其中,spring.application.name指定服务的名称,server.port指定服务的端口号,eureka.client.serviceUrl.defaultZone指定Eureka Server的地址。

  1. 启动类

在服务提供者的启动类上添加@EnableEurekaClient注解,将其作为Eureka Client启动:

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

@SpringBootApplication
@EnableEurekaClient
public class MyServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyServiceApplication.class, args);
    }
}

第二部分:服务消费者的配置

  1. 引入依赖

在服务消费者的pom.xml文件中添加以下依赖:

<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>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
  1. 配置文件

在服务消费者的application.properties文件中添加以下配置:

spring.application.name=my-consumer
server.port=8082
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

其中,spring.application.name指定服务的名称,server.port指定服务的端口号,eureka.client.serviceUrl.defaultZone指定Eureka Server的地址。

  1. 启动类

在服务消费者的启动类上添加@EnableEurekaClient注解,并使用@LoadBalanced注解配置RestTemplate,实现负载均衡:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class MyConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

第三部分:服务消费

在服务消费者中可以通过RestTemplate来调用服务提供者的接口。RestTemplate是Spring Framework提供的一个用于调用RESTful服务的模板类,它封装了HTTP请求和响应。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MyConsumerService {
    private final RestTemplate restTemplate;

    @Autowired
    public MyConsumerService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String callMyService() {
        String url = "http://my-service/hello";
        return restTemplate.getForObject(url, String.class);
    }
}

在上面的代码中,我们使用RestTemplate的getForObject方法来发送GET请求,并获取服务提供者的响应数据。

结论

通过本文的介绍,读者应该了解了在Spring Cloud中使用Eureka作为服务注册中心,并通过RestTemplate和Ribbon来实现服务消费的方法。服务提供者将自己注册到Eureka Server中,服务消费者通过RestTemplate调用服务提供者的接口。同时,Ribbon提供了负载均衡的能力,使得服务消费者可以实现对多个服务提供者的负载均衡调用。在后续的文章中,我们将继续探讨Spring Cloud中其他功能组件的使用方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值