为了账号安全,请及时绑定邮箱和手机立即绑定

[微服务系列] 2. 服务治理--Eureka

标签:
微服务

一、服务治理

所谓服务治理就是用来实现各个微服务实例的自动化注册于发现。当多个微服务互相调用时,就需要维护一个静态的配置文件,来说明各自所承担的服务内容、地址、端口等信息,其他服务调用时,就需要去查询这个配置文件,以获取服务。但是随着业务的发展,系统功能越发变得复杂,之前的静态配置文件维护起来也越发的耗费精力时间,并且由于需求的不管变更,使得服务的命名、地址都会发生变化,所以为了解决这样的问题,在微服务架构中,会使用服务治理来注册和发现微服务中的实例,并进行统一的管理。

二、服务的注册与发现

webp

1. 服务注册于发现.png

关系调用说明:

  • 服务生产者启动时,向服务注册中心注册自己提供的服务

  • 服务消费者启动时,在服务注册中心订阅自己所需要的服务

  • 注册中心返回服务提供者的地址信息个消费者

  • 消费者从提供者中调用服务

三、Eureka

Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来。
Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。
客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。

四、使用Eureka进行服务治理

1. 搭建服务注册中心

单独创建一个Springboot项目,并添加如下的依赖

<dependencies>
  <!--添加Eureka服务器端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency></dependencies>

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

在Springboot项目中的main入口,添加@EnableEurekaServer注解,来开启服务注册中心

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

在默认情况下,服务注册中心也会把自己当做是一个服务,将自己注册进服务注册中心,所以我们可以通过配置来禁用他的客户端注册行为,在application.properties中添加如下配置:

server.port=3333

eureka.instance.hostname=localhost#不要向注册中心注册自己eureka.client.register-with-eureka=false#禁止检索服务eureka.client.fetch-registry=falseeureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

启动应用,并访问http://localhost:3333/即可看到Eureka信息面板,如下:

webp

2. Eureka信息面板.png

从上图看到,在"Instances currently registered with Eureka"信息中,没有一个实例,说明目前还没有服务注册。

2. 注册服务

基于文章[微服务系列] 微服务构建框架--Spring Boot中构建的第一个Springboot项目,来进行改造。首先在pom文件中添加Eureka客户端相关的依赖:

<dependencies>
        <!---->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency></dependencies><dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RS5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

在之前的TestRestful方法中添加如下代码,将org.springframework.cloud.client.discovery.DiscoveryClient;对象注入,并且在日志中打印出与服务相关的一些信息。

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

在主类上添加@EnableEurekaClient注解以实现Eureka中的DiscoveryClient实现。

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

最后在配置文件application.yml中配置与服务相关的一些基本信息,如服务名、注册中心地址(即上一节中设置的注册中心地址http://localhost:3333/eureka)

#  设置服务名spring:
  application:
    name: hello-service#  设置注册中心地址eureka:
  client:
    service-url:
      defaultZone: http://localhost:3333/eureka
3. 测试
  1. 启动注册中心服务

  2. 启动springbootdemo项目,可以看到如下的信息,说明此服务已经注册在了注册中心

webp

3 服务启动日志.png

同时访问http://localhost:3333/可以在Instances currently registered with Eureka中看到已经有一个服务注册了进来,并且名称为HELLO-SERVICE的服务

webp

4 服务启动.png

  1. 访问http://localhost:8088/hello可以在控制台中看到如下日志信息:

webp

5 日志.png

五、 小结

在本章中,主要介绍在微服务中服务治理的基本概念,通过简单的实践,搭建单节点服务注册中心,并实现了服务的注册,了解服务治理的基本工作流程。



作者:Xiangdong_She
链接:https://www.jianshu.com/p/92c10b2ff8a2


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
39
获赞与收藏
123

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消