Spring Cloud 入门教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)

目录

 

一、spring cloud简介

二、创建服务注册中心

2.1 首先创建一个maven父工程

2.2 创建子工程

2.3 启动服务注册中心

2.4 配置注册中心的application.properties

2.5 启动工程查看注册中心

三、创建一个服务提供者 (eureka client)

3.1 创建子工程

3.2 配置服务提供者信息

3.3 启动服务提供者


一、spring cloud简介

Finchley版本的官方文档

spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解。另外对于“微服务架构” 不了解的话,可以通过搜索引擎搜索“微服务架构”了解下。

二、创建服务注册中心

在这里,还是采用Eureka作为服务注册与发现的组件。

2.1 首先创建一个maven父工程

首先创建一个主Maven工程,在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE。这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom。这一系列文章全部采用这种模式,其他文章的pom跟这个pom一样。再次说明一下,以后不再重复引入。代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>springcloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>eureka-server</module>
        <module>service-hi</module>
    </modules>

    <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>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.2 创建子工程

一个model工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client。

下面以创建server为例子,详细说明创建过程:

右键工程->创建model-> 选择spring initialir 如下图:

在这里插入图片描述

下一步->选择cloud discovery->eureka server ,然后一直下一步就行了。

在这里插入图片描述

创建完后的工程,其pom.xml继承了父pom文件,并引入spring-cloud-starter-netflix-eureka-server的依赖,代码如下:
 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- 基本信息 -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <description>服务注册中心</description>

    <!-- 继承本项目的父工程 -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>


2.3 启动服务注册中心

只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
 * @Auther: yaohongan
 * @Description //服务注册中心 
 * @Date: 2020/4/26 16:25
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

2.4 配置注册中心的application.properties

eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件application.properties:

##################################### common config : ####################################
server.port=8761
spring.application.name=eurka-server
#服务中心地址
eureka.instance.hostname=localhost
#表示是否将自己注册到Eureka Server,默认为true
eureka.client.registerWithEureka=false
#表示是否从Eureka Server获取注册信息,默认为true
eureka.client.fetchRegistry=false
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

通过eureka.client.registerWithEureka:falsefetchRegistry:false来表明自己是一个eureka server

2.5 启动工程查看注册中心

eureka server 是有界面的,启动工程,打开浏览器访问:http://localhost:8761 ,界面如下:

No application available 没有服务被发现 ……,因为没有注册服务当然不可能有服务被发现了。

三、创建一个服务提供者 (eureka client)

3.1 创建子工程

当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

创建过程同server类似,创建完pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- 基本信息 -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-hi</name>
    <description>服务提供者</description>

    <!-- 继承本项目的父工程 -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

3.2 配置服务提供者信息

在启动类ServiceHiApplication上加上@EnableEurekaClient注解,

通过注解@EnableEurekaClient 表明自己是一个eurekaclient.

package com.example.servicehi;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: yaohongan
 * @Description //服务提供者 
 * @Date: 2020/4/26 16:42
 */
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

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

    @Value("${server.port}")
    String port;
    
    /**
     * @Auther: yaohongan
     * @Description //测试 
     * @Date: 2020/4/26 16:41
     */
    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "springcloud") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }


}

仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.properties配置文件如下:

##################################### common config : ####################################
server.port=8762
spring.application.name=service-hi
#设置与Eureka Server交互的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。

3.3 启动服务提供者

服务中心的工程必须处于启动状态,然后才能启动服务提供者工程,然后打开http://localhost:8761 ,即eureka server 的网址:

 

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为8762

这时打开 http://localhost:8762/hi?name=springcloud,你会在浏览器上看到 :

hi springcloud ,i am from port:8762

更多阅读:

Spring Cloud 微服务架构教程 | 汇总 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值