spring-boot&spring-cloud系列专题Spring-BootSpringBoot极简教程 · Spring Boot

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

2017-05-23  本文已影响3864人  cqf_

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

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

我们会在接下去的章节中接触这些内容,本章节就开始讲注册中心与服务发现——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
@SpringBootApplication
public class Application {

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

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

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

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

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

具体详见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
@SpringBootApplication
class Application {

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

3、配置文件如下

spring.application.name=cqf-service

server.port=2222

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

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

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

你喜欢就是我最大的动力——cqf

上一篇下一篇

猜你喜欢

热点阅读