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

Spring-Cloud 快速入门——注册中心与服务发现

标签:
Spring Cloud

从本章节开始,我们会接触Spring-Cloud的相关内容,SpringCloud分布式开发有五大利器,
服务发现——Netflix Eureka
客服端负载均衡——Netflix Ribbon
断路器——Netflix Hystrix
服务网关——Netflix Zuul
分布式配置——spring Cloud Config
这五大利器都是由Netflix公司开发的,并捐赠给了Apache开源组织。

先给大家看一下大概的微服务架构图,有一个整体的框架概念。

webp

我们会在接下去的章节中接触这些内容,本章节就开始讲注册中心与服务发现——Netflix Eureka

<strong>注册中心建立</strong>

1、pom.xml中引入 Eureka相关jar包

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <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.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies></dependencyManagement>

2、启动类中引入注解,具体如下:

package com.cqf.chapter3;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/**
 * 
 * @author qifu.chen
 * @version 1.0.0
 * @date May 16, 2017 3:11:16 PM
 */@EnableEurekaServer@SpringBootApplicationpublic class Application {    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3、application.properties配置注册中心相关属性,这个文件的存放目录是src.main.resources

server.port=1111eureka.instance.hostname=localhost

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

一个简单的注册中心就创建好了,需要注意,服务启动后,端口已经修改为1111,所以服务注册中心的访问地址是http://localhost:1111/

webp

具体详见demo <a href="https://github.com/qifuchen/cqf-demo">chapter3-eureka-server</a>

注册中心建好了,但是此时还没有任何的服务注册上来,下面就来讲解一下,怎么把服务注册到注册中心——服务发现。

<strong>服务发现</strong>

1、pom.xml文件引入相关jar包

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</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>Brixton.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies></dependencyManagement>

2、编写启动类,这里和会中类的前面加入@EnableDiscoveryClient注解,服务启动时通过这个注解就能在注册中心发现此服务。

package com.cqf.chapter3;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/**
* 
* @author qifu.chen
* @version 1.0.0
* @date May 18, 2017 3:33:55 PM
*/@EnableDiscoveryClient@SpringBootApplicationclass Application {  public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
  }
}

3、配置文件如下

spring.application.name=cqf-service

server.port=2222eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

这样,一个服务就提供出去了。需要注意,这里的服务启动端口是2222。
接下来,我们来启动一下这个服务,在注册中心就能看到这个服务,效果如下:

webp

具体详见demo <a href="https://github.com/qifuchen/cqf-demo">chapter3-cqf-service</a>



作者:cqf_
链接:https://www.jianshu.com/p/e6338b35d63e


点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

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

关注作者,订阅最新文章

阅读免费教程

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

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消