SpringCloud之服务注册与发现

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


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

maven多模块

SpringCloud之服务注册与发现_第1张图片

SpringCloud之服务注册与发现_第2张图片

目录

    • 服务注册发现
    • 项目创建:
    • 注册中心:
    • 服务提供方
    • 启动顺序: 注册中心(EurekaRegisterApplication)==>>服务提供端(EurekaProviderApplication)


服务注册发现

  • 注册中心: 提供服务注册、暴露相关服务信息
  • 服务提供方: 注册到服务注册中心提供服务
    SpringCloud之服务注册与发现_第3张图片

项目创建:

pom.xml


<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.0modelVersion>
    <groupId>org.example.cloudgroupId>
    <artifactId>spring-cloud-exampleartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.5.RELEASEversion>
    parent>

    
    <modules>
        <module>eureka-servermodule>
        <module>eureka-clientmodule>
    modules>

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

注册中心:

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

pom.xml


<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-exampleartifactId>
        <groupId>org.example.cloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <artifactId>eureka-serverartifactId>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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


<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-exampleartifactId>
        <groupId>org.example.cloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <artifactId>eureka-clientartifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        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)

SpringCloud之服务注册与发现_第4张图片

SpringCloud之服务注册与发现_第5张图片


你可能感兴趣的:(Eureka,springcloud,服务注册发现,springcloud)