温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Eureka搭建简单的服务端注册服务

发布时间:2021-12-29 09:25:42 来源:亿速云 阅读:148 作者:iii 栏目:软件技术

这篇文章主要讲解了“如何使用Eureka搭建简单的服务端注册服务”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用Eureka搭建简单的服务端注册服务”吧!

案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是我们上一篇的 Eureka 单节点启动既可。

流程如下:

启动注册中心

服务提供者生产服务并注册到服务中心中

消费者从服务中心中获取服务并执行

服务提供者
我们假设服务提供者有一个 hello() 方法,可以根据传入的参数,提供输出 “hello xxx + 当前时间” 的服务。

POM 包配置
创建一个基本的 Spring Boot 应用,命名为 eureka-producer,在 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>

配置文件
application.yml 配置如下

spring:
  application:
    name: eureka-producer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/
server:
  port: 8000

通过 spring.application.name 属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone 属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用 server.port 属性设置不同的端口。

启动类

保持默认生成的即可, Finchley.RC1 这个版本的 Spring Cloud 已经无需添加 @EnableDiscoveryClient 注解了。(那么如果我引入了相关的 jar 包又想禁用服务注册与发现怎么办?设置 eureka.client.enabled=false)

@EnableDiscoveryClient is no longer required. You can put a DiscoveryClient implementation on the classpath to cause the Spring Boot application to register with the service discovery server.

Spring Cloud - @EnableDiscoveryClient

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

启动工程后,就可以在注册中心 Eureka 的页面看到 EUERKA-PRODUCER 服务。

服务消费者

创建服务消费者根据使用 API 的不同,大致分为三种方式。虽然大家在实际使用中用的应该都是 Feign,但是这里还是把这三种都介绍一下吧,如果你只关心 Feign,可以直接跳到最后。

如何使用Eureka搭建简单的服务端注册服务

三种方式均使用同一配置文件,不再单独说明了

spring:
  application:
    name: eureka-consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/ # 指定 Eureka 注册中心的地址
server:
  port: 9000 # 分别为 9000、9001、9002

使用 LoadBalancerClient

从 LoadBalancerClient 接口的命名中,我们就知道这是一个负载均衡客户端的抽象定义,下面我们就看看如何使用 Spring Cloud 提供的负载均衡器客户端接口来实现服务的消费。

POM 包配置

我们先来创建一个服务消费者工程,命名为:eureka-consumer。pom.xml 同 Producer 的,不再赘述。

启动类

初始化 RestTemplate,用来发起 REST 请求。

@SpringBootApplication
public class EurekaConsumerApplication {
   @Bean 
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerApplication.class, args);
    }
}

Controller
创建一个接口用来消费 eureka-producer 提供的接口:

@RequestMapping ("/hello")
@RestController 
public class HelloController {
   @Autowired 
    private LoadBalancerClient client;
    @Autowired
    private RestTemplate restTemplate;
   @GetMapping ("/")
    public String hello(@RequestParam String name) {
        name += "!";
        ServiceInstance instance = client.choose("eureka-producer");
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/hello/?name=" + name;
        return restTemplate.getForObject(url, String.class);
    }
}

可以看到这里,我们注入了 LoadBalancerClient 和 RestTemplate,并在 hello 方法中,先通过 loadBalancerClient 的 choose 方法来负载均衡的选出一个 eureka-producer 的服务实例,这个服务实例的基本信息存储在 ServiceInstance 中,然后通过这些对象中的信息拼接出访问服务调用者的 /hello/ 接口的详细地址,最后再利用 RestTemplate 对象实现对服务提供者接口的调用。

另外,为了在调用时能从返回结果上与服务提供者有个区分,在这里我简单处理了一下,name+="!",即服务调用者的 response 中会比服务提供者的多一个感叹号(!)。

Spring Cloud Ribbon

之前已经介绍过 Ribbon 了,它是一个基于 HTTP 和 TCP 的客户端负载均衡器。它可以通过在客户端中配置 ribbonServerList 来设置服务端列表去轮询访问以达到均衡负载的作用。

当 Ribbon 与 Eureka 联合使用时,ribbonServerList 会被 DiscoveryEnabledNIWSServerList 重写,扩展成从 Eureka 注册中心中获取服务实例列表。同时它也会用 NIWSDiscoveryPing 来取代 IPing,它将职责委托给 Eureka 来确定服务端是否已经启动。

POM 包配置

将之前的 eureka-consumer 工程复制一份,并命名为 eureka-consumer-ribbon。

pom.xml 文件还用之前的就行。至于 spring-cloud-starter-ribbon,因为我使用的 Spring Cloud 版本是 Finchley.RC1,spring-cloud-starter-netflix-eureka-client 里边已经包含了 spring-cloud-starter-netflix-ribbon 了。

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

启动类
修改应用主类,为 RestTemplate 添加 @LoadBalanced 注解

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

Controller
修改 controller,去掉 LoadBalancerClient,并修改相应的方法,直接用 RestTemplate 发起请求

@GetMapping("/")
public String hello(@RequestParam String name) {
    name += "!";
    String url = "http://eureka-producer/hello/?name=" + name;
    return restTemplate.getForObject(url, String.class);
}

可能你已经注意到了,这里直接用服务名 eureka-producer 取代了之前的具体的 host:port。那么这样的请求为什么可以调用成功呢?因为 Spring Cloud Ribbon 有一个拦截器,它能够在这里进行实际调用的时候,自动的去选取服务实例,并将这里的服务名替换成实际要请求的 IP 地址和端口,从而完成服务接口的调用。

Spring Cloud Feign

在实际工作中,我们基本上都是使用 Feign 来完成调用的。我们通过一个例子来展现 Feign 如何方便的声明对 eureka-producer 服务的定义和调用。欢迎大家加我qq:1038774626探讨技术问题。

POM 包配置

创建一个基本的 Spring Boot 应用,命名为 eureka-producer-feign,在 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-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

启动类
在启动类上加上 @EnableFeignClients

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

此类中的方法和远程服务中 Contoller 中的方法名和参数需保持一致。

这里有几个坑,后边有详细说明。

Controller
修改 Controller,将 HelloRemote 注入到 controller 层,像普通方法一样去调用即可

@RequestMapping
@Restcontroller ("/hello")
public class HelloController {
    @Autowired
    HelloRemote helloRemote;
  @GetMapping  ("/{name}")
    public String index(@PathVariable("name") String name) {
        return helloRemote.hello(name + "!");
    }
}

通过 Spring Cloud Feign 来实现服务调用的方式非常简单,通过 @FeignClient 定义的接口来统一的声明我们需要依赖的微服务接口。而在具体使用的时候就跟调用本地方法一点的进行调用即可。由于 Feign 是基于 Ribbon 实现的,所以它自带了客户端负载均衡功能,也可以通过 Ribbon 的 IRule 进行策略扩展。另外,Feign 还整合的 Hystrix 来实现服务的容错保护。

感谢各位的阅读,以上就是“如何使用Eureka搭建简单的服务端注册服务”的内容了,经过本文的学习后,相信大家对如何使用Eureka搭建简单的服务端注册服务这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI