Spring Cloud初探——服务治理Spring Cloud Eureka

来源:互联网 发布:外汇数据下载 编辑:程序博客网 时间:2024/05/16 16:17

1.Spring Cloud介绍

  Spring Cloud 是一个基于Spring Boot实现的微服务架构开发工具,它同Spring Boot 一样,同样为微服务架构中涉及的配置管理,服务治理,断路器,智能路由,微代理,控制总线,全局锁,决策竞选,分布式会话和集群状态管理等操作提供了一种简单的开发方式。

2 .服务治理组件 Spring Cloud Eureka

  Spring Cloud Eureka是Spring Cloud Netfix微服务套件中的一部分,它基于 Netfix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能,因为Spring Cloud Eureka类似Spring Boot风格的自动化的配置,我们只需通过简单引入依赖和注解配置就能让Spring Boot构建的微服务应用轻松的与Eureka服务治理体系进行整合。

2.1 服务治理

  服务治理可以说是微服务架构中的最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与实现。随着业务的发展,系统功能越来越复杂,相应的微服务应用也不断增加。我们的静态配置就会变得越来越难以维护。并且面对不断发展的业务,我们的集群规模,服务的位置,服务的命名都有可能的发生变化。如果还是通过手工维护的方式。为了解决微服务架构的服务实例维护问题,就产生了大量的服务治理框架和产品。

2.2 Spring Cloud Eureka

  Spring Cloud Eureka既包含了服务端组件,也包含了客户端组件,并且服务端与客户端均采用Java编写,所以 Eureka主要适用于通过Java实现的分布式系统,或是JVM兼容语言构建的系统。

  • Eureka服务端,主要是服务注册中心,支持高可用配置。他依托于强一致性的提供良好的服务系统可用性,可以应对各种不同的故障场景。
  • Eureka客户端,主要处理服务的注册与发现,客户端服务通过注解和参数配置的方式,嵌入在客户端应用程序的代码中。

3.搭建服务注册中心

首先,创建一个基础的Spring Boot基础工程,在pom中引人

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.7.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>        <spring-cloud.version>Dalston.SR3</spring-cloud.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka-server</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>

在服务入口列加入Eurake的配置@EnableEurekaServer,

@EnableEurekaServer@SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

我们在配置文件中对端口和其他属性进行配置,

server.port=1111eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

启动工程,进入http://localhost:1111/,可见如图
这里写图片描述

4.搭建服务提供者

我们同样的新建一个Spring Boot项目,在pom文件中进行如下配置

    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka-server</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>

我们在SpringBoot的入口文件中开启EurekaClient的注解@EnableEurekaClient,

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

同时我们需要对application.properties进行配置:

spring.application.name=client-serviceeureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

在处理请求时,我们可以测试我们的调用是否成功,在Controller中加入调用的代码:

@RestControllerpublic class HomeController {    private final  Logger logger = Logger.getLogger(getClass());    @Autowired    private DiscoveryClient client;    @RequestMapping(value = "/hello",method = RequestMethod.GET)    public String index(){        ServiceInstance instance = client.getLocalServiceInstance();        logger.info("/hello, host:"+instance.getHost()+"service_id"+instance.getServiceId());        return  "hello_world";    }}

同时针对调用的上述的服务注册中心的服务在application.properties文件中进行配置。通过访问http://localhost:8080/hello,可以看到控制台和服务注册界面如图,表明服务确实被注册并调用成功了。
这里写图片描述

这里写图片描述

由此,我们创建了一个简单的服务注册中心的服务注册和调用的小例子。

参考书籍 :翟永超《Spring Cloud 微服务实战》
参考博客:史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)