SpringCloud之服务注册与发现

本文详细介绍如何使用SpringCloud Finchley.SR1版本构建微服务架构,包括服务注册、发现、负载均衡等核心功能实现。通过创建Maven多模块项目,搭建Eureka注册中心和服务提供者,展示微服务的启动流程和配置细节。

springCloud是涵盖服务注册、发现、负载均衡、断路、路由等能力并快速构建分布式系统的微服务框架。


基于Finchley.SR1版本,官方文档:http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html

maven多模块

聚合工程

结构

目录


服务注册发现

  • 注册中心: 提供服务注册、暴露相关服务信息
  • 服务提供方: 注册到服务注册中心提供服务
    服务注册发现

项目创建:

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example.cloud</groupId>
    <artifactId>spring-cloud-example</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <!-- 继承SpringBoot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <!-- 子模块 -->
    <modules>
        <module>eureka-server</module>
        <module>eureka-client</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.SR1</spring-cloud.version>
    </properties>

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

注册中心:

  1. 创建子项目eureka-server
  2. pom.xml配置
  3. 注册中心启动类EurekaRegisterApplication.java
  4. 配置文件application.yml

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-example</artifactId>
        <groupId>org.example.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

启动类:EurekaRegisterApplication.java

注意:

  • 服务端使用@EnableEurekaServer

  • 客户端使用@EnableEurekaClient

package org.example.register;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author Cheng.Wei
 * @date 2018-09-15 02:21
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaRegisterApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaRegisterApplication.class).web(WebApplicationType.SERVLET).run(args);
    }
}

配置文件:application.yml

server:
  port: 7777 #启动端口

eureka:
  #实例配置
  instance:
    hostname: localhost
    appname: 注册中心

  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

服务提供方

  1. 创建子项目eureka-client
  2. pom.xml配置
  3. 创建启动类:EurekaProviderApplication
  4. 配置文件application.yml

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-example</artifactId>
        <groupId>org.example.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-client</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

启动类:EurekaProviderApplication

package org.example.provider;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author Cheng.Wei
 * @date 2018-09-15 02:42
 */

@EnableEurekaClient
@SpringBootApplication
public class EurekaProviderApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaProviderApplication.class).web(WebApplicationType.SERVLET).run(args);
    }
}
  • 配置文件:application.yml
spring:
  application:
    name: provider

server:
  port: 7778

eureka:
  instance:
    appname: 服务A
    #不使用主机名来定义注册中心的地址,而使用IP地址的形式
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name}
  client:
    serviceUrl:
      defaultZone: http://localhost:7777/eureka/

启动顺序: 注册中心(EurekaRegisterApplication)==>>服务提供端(EurekaProviderApplication)

这里写图片描述

这里写图片描述


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值