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

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

标签:
微服务

一、 高可用注册中心

在上一篇文章中,介绍了服务注册中心的单节点搭建部署。然而在常用的微服务结构设计中,一般都是采用分布式的环境,以避免单机故障导致服务调用不可用,所以需要对微服务中的各个组件进行高可用的部署,服务治理中的服务注册中心也是一样。

二、 服务的发现与消费

在上一篇文章中,我们仅仅对服务发现与消费做了简单的流程说明,在本文中,将基于已构建好的注册中心和服务生产者,来构建服务消费者。通过服务消费者,来发现注册在注册中心的服务并消费服务。本章将结合以下的拓扑图构建高可用的服务治理。

webp

1. 高可用服务治理.png

三、 快速实践

1. 构建高可用注册中心

Eureka的高可用就是将自己作为服务想其他服务注册中心注册自己,这样就可以形成一组互相注册的服务注册中心,已实现服务清单的互相同步,达到高可用的目的。

基于上一节创建的项目,将配置文件application.properties修改为application-peer1.properties,同时修改配置内容如下:

spring.application.name=eureka-server
server.port=1111eureka.instance.hostname=registry1# 向注册中心注册自己eureka.client.register-with-eureka=false# 允许使用IP地址的形式来定义注册中心地址eureka.instance.ip-address=true# 关闭保护机制eureka.server.enable-self-preservation=false# 检索服务eureka.client.fetch-registry=true# 将serviceUrl指向其他的注册中心eureka.client.service-url.defaultZone=http://127.0.0.1:1112/eureka

再创建一个配置文件application-peer2.properties,配置内容如下:

spring.application.name=eureka-server
server.port=1112eureka.instance.hostname=registry2# 向注册中心注册自己eureka.client.register-with-eureka=false# 允许使用IP地址的形式来定义注册中心地址eureka.instance.ip-address=true# 关闭保护机制eureka.server.enable-self-preservation=false# 检索服务eureka.client.fetch-registry=true# 将serviceUrl指向其他的注册中心eureka.client.service-url.defaultZone=http://127.0.0.1:1111/eureka

使用Maven命令clean package将项目eureka-server打包成Jar包,并使用如下两个命令来别调用不同的配置文件,并启动项目:

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

webp

2. 服务注册中心 (1).png


webp

2. 服务注册中心 (2).png

2.  服务的发现与消费
1. 生产服务

服务发现与消费主要涉及到两个内容:一是发现服务,而是对发现的服务进行消费(即调用)。服务的发现由Eureka的客户端完成,服务的消费则有Ribbon完成。

首先,通过java -jar xxx.jar --server.port=xxxx的方式在不同端口启动之前搭建的hello-service服务。

java -jar springbootdemo-0.0.1-SNAPSHOT.jar --server.port=8088 --management.port=60000
java -jar springbootdemo-0.0.1-SNAPSHOT.jar --server.port=8089 --management.port=60001

如下图,启动hello-service之后在注册中心的出现了两个实例单元。

[图片上传失败...(image-6946e7-1510659484119)]

2. 消费服务

服务消费者采用Ribbon负载均衡器来实现。重新构建一个基于SpringBoot的基础工程ribbon-consumer来实现服务消费者,并且在pom.xml文件中添加如下的依赖文件。

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-start-eureka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</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>

在主类上添加@EnableDiscoveryClient注解,使其作为Eureka客户端,已获取服务发现的能力。在主类中注入RestTemplateJavaBean,并使用@LoadBalanced注解,使其开启客户端负载均衡能力。

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

在配置文件application.yml中配置注册中心地址并设置服务的名称和端口

# 服务名称spring:
  application:
    name: hello-consumer# 服务端口号server:
  port: 9000# 注册中心地址eureka:
  client:
    service-url:
      defaultZone: http://localhost:3333/eureka

创建ConsumerController并在一个接口方法中调用HELLO-SERVICE中的/hello接口,具体如下:

import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class ConsumerController {    private Logger logger=Logger.getLogger(ConsumerController.class);    @Autowired
    private RestTemplate restTemplate;    @RequestMapping(value = "helloConsume",method = RequestMethod.GET)    public String helloConsume(){
        logger.info("============Start consume========");        return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
    }
}

启动服务,并查看注册中心信息面板,可以看到服务已经被注册进了注册中心:

java -jar consumer-0.0.1-SNAPSHOT.jar --server.port=9000

访问地址localhost:9000/helloConsume可以看到输出结果。

四、小结

通过本文简单介绍了Eureka集群的配置中以及如何在集群中发布服务和消费服务。



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


点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消