spring cloud 服务治理

Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务治理可以说是微服务架构中最为核心和基础的模块,他主要用来实现各个微服务实例的自动化注册与发现

image

● 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,注册中心按照服务名分类组织服务清单,服务注册中心还需要以心跳的方式去监控清单中的服务是否可用,若不可用需要从服务清单中剔除,达到排除故障服务的效果。
● 服务发现:由于在服务治理框架下运行,服务间的调用不再通过指定具体的实例地址来实现,而是通过向服务名发起请求调用实现。

Spring Cloud Eureka 使用 Netflix Eureka 来实现服务注册与发现,即包括了服务端组件,也包含了客户端组件,并且服务端和客户端均采用Java编写,所以Eureka主要适用与通过Java实现的分布式系统,或是与JVM兼容语言构建的系统,但是,由于Eureka服务端的服务治理机制提供了完备的RESTful API,所以他也支持将非Java语言构建的微服务纳入Eureka的服务治理体系中来。

● Eureka服务端:我们也称为服务注册中心,他同其他服务注册中心一样,支持高可用配置。
● Eureka客户端:主要处理服务的注册和发现,客户端服务通过注册和参数配置的方式,嵌入在客户端应用程序的代码中,在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新他的服务租约。
一:创建服务注册中心(3部曲):
 第一步:新建maven项目,引入如下pom:
<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.lagou.springcloud.register</groupId>
  <artifactId>register-server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>register-server</name>
  <url>http://maven.apache.org</url>

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

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
  </parent>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Camden.SR6</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

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

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

  </dependencies>
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
      <defaultGoal>compile</defaultGoal>
    </build>
</project>
 第二步:新加如下启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {

public static void main(String[] args) {

    SpringApplication.run(EurekaServer.class, args);

    }

}
 第三步:配置application.properties文件
server.port=8888

#eureka.instance.hostname=localhost
#eureka.instance.preferIpAddress=true

#eureka.client.registerWithEureka=false

#eureka.client.fetchRegistry=false
eureka.instance.hostname=eureka-server-001

eureka.client.serviceUrl.defaultZone=http://eureka-server-001:${server.port}/eureka/,http://eureka-server-002:${server.port}/eureka/,http://eureka-server-003:${server.port}/eureka/

spring.application.name=eureka-server

参数说明

server.port=8888 :指定注册中心的对外端口号,用于客户端链接

eureka.instance.preferIpAddress 表示使用IP进行配置,
这里我们不用

eureka.client.registerWithEureka=false
配置是否把自己注册到注册中心,单节点可使用,我们使用集群形式,这里使用默认,true

eureka.client.fetchRegistry=false
配置是否从注册中心获取注册的服务列表,单节点可使用,我们使用集群形式,这里使用默认,true

eureka.instance.hostname=eureka-server-001
当前节点的host名字

eureka.client.serviceUrl.defaultZone=http://eureka-server-001: server.port/eureka/,http://eurekaserver002: s e r v e r . p o r t / e u r e k a / , h t t p : / / e u r e k a − s e r v e r − 002 : {server.port}/eureka/,http://eureka-server-003:${server.port}/eureka/
注册中心地址,因为我们采用的是高可用的集群形式,如上配置,代表我们有3个注册中心。互相注册发现,同步注册信息

spring.application.name=eureka-server
服务名,Eureka客户端 通过他实现服务的注册和发现

==注意==:
需要配置如下hosts:

ip1  eureka-server-001
ip2  eureka-server-002
ip3  eureka-server-003

3台节点的eureka.instance.hostname分别为:

eureka-server-001

eureka-server-002

eureka-server-003

启动3台服务:打开链接

http://10.1.200.134:8888

这里写图片描述

这里,我们的服务注册中心就搭建完成了,接下来我们讲解怎么使用注册中心进行服务的注册与发现

二: 搭建Eureka客户端并使用注册中心

原文链接

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值