Spring Cloud - Eureka Client

本文详细介绍了如何使用SpringCloud Eureka进行服务的注册、发现及服务间调用,通过创建服务生产者与消费者项目,演示了EurekaClient的搭建过程,并实现了跨服务调用。

在这里插入图片描述

一.前言

Spring Cloud 之Eureka Server中,我们介绍了Eureka Server的搭建,今天我们来搭建Eureka Client。然后完成下面的案例
在这里插入图片描述

二.服务生产者

2.1 创建项目

img

经过上面的一顿操作,我们已经成功创建项目,下面我们来看下项目结构

在这里插入图片描述

2.2 pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.milo</groupId>
        <artifactId>milgenius-springcloud</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.milo</groupId>
    <artifactId>springcloud-service-provider</artifactId>
    <version>1.0.0</version>
    <name>springcloud-service-provider</name>
    <description>服务提供者</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.3 添加注解@EnableDiscoveryClient

package com.milo.provider;

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


@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceProviderApplication.class, args);
    }

}

2.4 配置文件


#服务命名
spring:
  application:
    name: service-provider

#服务注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

server:
  port: 8762

2.5 模拟一个服务接口

/**
 * @author: Milogenius
 * @create: 2019-06-28 12:14
 * @description:
 **/
@RestController
@Slf4j
public class ProviderController {

    @Autowired
    private DiscoveryClient client;

    /**
     * 模拟一个服务接口
     * @return
     */
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){
        List<ServiceInstance> instances = client.getInstances("service-provider");
        ServiceInstance instance = instances.get(0);
        log.info("hello, host:" + instance.getHost()+",service_id:"+instance.getServiceId());
        return "hello world";
    }
}

三.服务消费者

3.1 创建项目

img

经过上面的一顿操作,我们已经成功创建项目,下面我们来看下项目结构

在这里插入图片描述

3.2 pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.milo</groupId>
        <artifactId>milgenius-springcloud</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.milo</groupId>
    <artifactId>springcloud-service-consumer</artifactId>
    <version>1.0.0</version>
    <name>springcloud-service-consumer</name>
    <description>服务消费者</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.3 添加注解@EnableDiscoveryClient

package com.milo.consumer;

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

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class, args);
    }

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

3.4 配置文件


eureka:
  client:
  serviceUrl:
    defaultZone: http://localhost:8761/eureka/

server:
  port: 8764

spring:
  application:
    name: service-consumer

3.5 模拟一个业务

/**
 * @author: Milogenius
 * @create: 2019-06-28 13:45
 * @description:
 **/
@RestController
public class ConsumerController {

    @Autowired
    private IConsumerService consumerService;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(@RequestParam String name){
        return  consumerService.hello(name);
    }
}
/**
 * @author: Milogenius
 * @create: 2019-06-28 13:48
 * @description:
 **/
public interface IConsumerService {

    String hello(String name);
}
/**
 * @author: Milogenius
 * @create: 2019-06-28 13:49
 * @description:
 **/
@Service
public class ConsumerServiceImpl implements IConsumerService {

    @Autowired
    RestTemplate restTemplate;
    @Override
    public String hello(String name) {
        return restTemplate.getForObject("http://SERVICE-PROVIDER:8762/hello?name="+name,String.class);
    }
}

四.服务间调用

首次我们启动springcloud-eureka-serverEureka Server

在这里插入图片描述

浏览器访问http://localhost:8761/

在这里插入图片描述
接着我们启动springcloud-service-provider 服务提供者Eureka Client

在这里插入图片描述

浏览器访问http://localhost:8761/

在这里插入图片描述

最后,我们启动springcloud-service-consumer服务消费者Eureka Client

在这里插入图片描述

浏览器访问http://localhost:8761/

在这里插入图片描述

至此,我们完成了项目的搭建和服务的注册,接下来我们测试服务间的调用;

在我们大家服务消费者项目时候,我们模拟了一个业务,在ConsumerServiceImpl类的hello方法中我们模拟调用了另外一个服务;

浏览器访问http://localhost:8764/hello?name=milogenius

在这里插入图片描述

我们发现,页面成功响应helloworld;现在我们去服务生产者看一下后台日志;

在这里插入图片描述

五.结论

经过上面的学习,我们完成了Spring Cloud之Eureka的服务的注册和发现,并完成服务之间的调用;

### Spring Cloud Netflix Eureka ClientSpring Cloud Starter Eureka Client 的区别使用 Spring Cloud 提供了多种工具和依赖项来支持微服务架构中的服务注册发现功能。以下是关于 `Spring Cloud Netflix Eureka Client` 和 `Spring Cloud Starter Eureka Client` 的详细信息,包括它们的区别、使用场景以及相关依赖配置。 #### 1. 定义背景 `Spring Cloud Netflix Eureka Client` 是基于 Netflix 开源项目 Eureka 的实现[^1]。它允许微服务应用程序将自身注册Eureka Server,并从 Eureka Server 中发现其他服务。 `Spring Cloud Starter Eureka Client` 是 Spring Cloud 提供的一个启动器(Starter),封装了 Eureka Client 的核心功能,简化了依赖管理和配置过程[^2]。 #### 2. 区别 - **命名范围** `Spring Cloud Netflix Eureka Client` 是具体的客户端实现,直接 Eureka Server 进行交互。而 `Spring Cloud Starter Eureka Client` 是一个更高层次的抽象,包含 `Spring Cloud Netflix Eureka Client` 及其相关的依赖项[^3]。 - **依赖管理** 使用 `Spring Cloud Starter Eureka Client` 时,开发者无需手动添加 Eureka Client 的依赖,因为该启动器已经包含了所有必要的依赖项。例如: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ``` 上述代码片段展示了如何通过 Maven 引入 `Spring Cloud Starter Eureka Client`[^4]。 - **配置复杂度** `Spring Cloud Starter Eureka Client` 提供了开箱即用的功能,减少了手动配置的工作量。相比之下,直接使用 `Spring Cloud Netflix Eureka Client` 需要更多的自定义配置[^5]。 #### 3. 使用场景 - **Spring Cloud Netflix Eureka Client** 如果项目需要更精细的控制或定制化功能,可以选择直接使用 `Spring Cloud Netflix Eureka Client`。这种情况下,开发者可能需要手动处理一些依赖项和配置[^6]。 - **Spring Cloud Starter Eureka Client** 对于大多数标准微服务应用场景,推荐使用 `Spring Cloud Starter Eureka Client`,因为它能够快速集成 Eureka 功能,减少开发时间[^7]。 #### 4. 示例代码 以下是一个简单的 Spring Boot 应用程序示例,展示如何使用 `Spring Cloud Starter Eureka Client` 将服务注册Eureka Server: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class MyEurekaClientApplication { public static void main(String[] args) { SpringApplication.run(MyEurekaClientApplication.class, args); } } ``` 在 `application.yml` 或 `application.properties` 文件中,需要指定 Eureka Server 的地址: ```yaml eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ ``` #### 5. 注意事项 - 确保使用的 Spring Cloud 版本 Spring Boot 版本兼容[^8]。 - 如果使用的是较新的 Spring Cloud 版本,需要注意 Netflix 组件(如 Eureka)可能已被标记为维护模式[^9]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值