Spring Cloud 的 eureka 服务注册与发现-----F05

Spring Cloud 的 eureka 服务注册与发现

我们在之前的课程中已经介绍过阿里的分布式服务框架 dubbo,但是小伙伴们应该也看到了,阿里的 dubbo 无法单独完成工作,我们还要借助于 Apache 上的开源框架 zookeeper (不是必须,但是最佳实践莫过于 zookeeper),使用 zookeeper 作为服务注册中心,才能实现一个较好的分布式应用。与 dubbo 不同的是,Spring Cloud 是一个一站式分布式框架,Spring Cloud 为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)。分布式系统的协调导致了样板模式, 使用 Spring Cloud 开发人员可以快速地支持实现这些模式的服务和应用程序。他们将在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数据中心,以及 Cloud Foundry 等托管平台。那么今天我希望通过一个简单的案例带小伙伴们来了解下
Spring Cloud。

本文主要介绍在 Spring Cloud 中使用 Eureka 搭建一个服务注册中心,然后再向其中注册服务。由于 Spring Cloud 是在 Spring Boot 的基础上构建分布式服务的,所以阅读本文需要有一点 Spring Boot 的知识储备,如果小伙伴们对 Spring Boot 尚不太熟悉的话,可以先参考下 Spring Boot 入门教程。在此基础之上我们来看看怎么样利用 Spring Cloud 中的 Eureka 来搭建服务注册中心。

Spring Cloud 包含了许多个子项目,我们今天要看的 Eureka 只是其中的一个子项目, Eureka 的功能有点类似于我们之前写过的 zookeeper,它是一个服务治理组件,包含了服务注册中心、服务注册与发现机制。

1、创建服务注册中心

1.1、创建聚合工程


image.png
image.png

1.2、创建 Eureka 子项目(服务注册中心)

image.png
image.png
image.png

1.3、添加 Eureka 依赖

Eureka pom.xml

版本****为****老版本****1.5.10



   4.0.0
   
      org.springframework.boot
      spring-boot-starter-parent
      1.5.10.RELEASE
       
   
   com.shsxt
   eureka
   0.0.1-SNAPSHOT
   eureka
   Demo project for Spring Boot

   
      1.8
   

   
      
         org.springframework.boot
         spring-boot-starter-web
      

      
         org.springframework.boot
         spring-boot-starter-test
         test
      
      
      
         org.springframework.cloud
         spring-cloud-starter-eureka-server
      

   
   
   
      
         
            org.springframework.cloud
            spring-cloud-dependencies
            Dalston.SR3
            pom
            import
         
      
   

   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   




1.4、服务注册中心注解

启动一个服务注册中心的方式很简单,就是在 Spring Boot 的入口类上添加一个 @EnableEurekaServer 注解

//开启服务注册中心注解
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

}


1.5****、配置服务注册中心

application.properties

# server.port=1111 表示设置该服务注册中心的端口号
server.port=1111
# eureka.instance.hostname=localhost 表示设置该服务注册中心的 hostname
eureka.instance.hostname=localhost
# eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,
# 默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为 false 表示禁止这种默认行为
eureka.client.register-with-eureka=false
# eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务
eureka.client.fetch-registry=false
# 对外暴露的注册地址
eureka.client.serviceurl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/


1.6****、测试

image.png

OK,做完这一切之后,我们就可以启动这一个 Spring Boot 服务,服务启动成功之后,在浏览器中输入:http://localhost:1111 就能够看到如下页面

image.png

OK,看到上面这个页面之后,表示你的服务注册中心已经搭建好了。

我们之前专门学习了如何在 Linux 上安装 zookeeper 以及一些注意事项,但是对于 Eureka 却不存在这样的问题,因为 Eureka 中的服务注册中心实际上是一个 Spring Boot 工程,而 Spring Boot 工程我们知道可以直接打成一个 jar 包,然后 java -jar 命令就可以运行,不管 Windows 还是 Linux 上,运行方式都是一致的。

2、服务提供者

现在服务注册中心有了之后,我们可以考虑向这个服务注册中心注册一个服务提供者了。

2.1、创建 provider 子项目(服务提供者)


image.png
image.png

2.2、添加 Eureka 依赖

provider 的 pom.xml



   4.0.0
   
      org.springframework.boot
      spring-boot-starter-parent
      1.5.10.RELEASE
       
   
   com.shsxt
   provider
   0.0.1-SNAPSHOT
   provider
   Demo project for Spring Boot

   
      1.8
   

   
      
         org.springframework.boot
         spring-boot-starter-web
      

      
         org.springframework.boot
         spring-boot-starter-test
         test
      

      
      
         org.springframework.cloud
         spring-cloud-starter-eureka
      

   


   
   
      
         
            org.springframework.cloud
            spring-cloud-dependencies
            Dalston.SR3
            pom
            import
         
      
   
   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   




2.3、注册服务注解

向eureka服务注册中心注册服务

/**
 * 客户端
 */

@EnableEurekaClient
@SpringBootApplication
public class ProviderApplication {

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

}


2.4、编写服务

package com.shsxt.controller;

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

/**
 * 服务提供者
 */

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        System.out.println("服务提供者被调用!");
        return "hello world!";
 }


2.5、配置服务名称和注册中心地址

application.properties

# 访问端口
server.port=8081
# 为你的应用起个名字,该名字将注册到 eureka 注册中心
spring.application.name=hello-service
# 注册中心地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka


2.6****、测试

image.png

3、服务消费者

3.1、创建 consumer 子项目(服务消费者)

image.png
image.png
image.png

3.2、添加 Eureka 依赖

consumer 的 pom.xml



   4.0.0
   
      org.springframework.boot
      spring-boot-starter-parent
      1.5.10.RELEASE
       
   
   com.shsxt
   consumer
   0.0.1-SNAPSHOT
   consumer
   Demo project for Spring Boot

   
      1.8
   

   
      
         org.springframework.boot
         spring-boot-starter-web
      

      
         org.springframework.boot
         spring-boot-starter-test
         test
      

      
      
         org.springframework.cloud
         spring-cloud-starter-eureka
      
   

   
   
      
         
            org.springframework.cloud
            spring-cloud-dependencies
            Dalston.SR3
            pom
            import
         
      
   


   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   




3.3、发现服务注册

通过该注解发现服务

/**
 * 服务消费者
 */
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApplication {

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

}


3.4、消费服务


/**
 * 消费服务
 */
@RestController
public class ConsumerController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/consumer")
    public String consumer() {
        // 获取服务列表
        List serviceList = discoveryClient.getServices();
        // 根据服务名称获取服务
        List serviceInstances = discoveryClient.getInstances(serviceList.get(0));
        // 处理要远程调用的接口
        ServiceInstance serviceInstance = serviceInstances.get(0);
        // http://localhost:8081/hello
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hello";
        // 通过RestTemplate发起请求
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        System.out.println("服务提供者返回的数据是:" + result);
        return result;
    }

}


3.5、配置消费者

Application.properties配置

# 访问端口
server.port=8082
# 默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为 false 表示禁止这种默认行为
eureka.client.register-with-eureka=false
# 为你的应用起个名字,该名字将注册到 eureka 注册中心
spring.application.name=consumer
# 注册中心地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka


3.6、测试


image.png
image.png

你可能感兴趣的:(Spring Cloud 的 eureka 服务注册与发现-----F05)