Springcloud框架学习笔记

Eureka服务注册与实现

2020-10-19  本文已影响0人  gona

Eureka服务注册与实现

什么是Eureka

原理讲解

4.png

eureka使用

导入依赖

<?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>jgnspringcloud</artifactId>
        <groupId>com.jgn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-7001</artifactId>
    <!-- 导包 -->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.3.6.RELEASE</version>
        </dependency>
        <!-- 热部署工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

</project>

对eureka进行配置

server:
  port: 7001
#Eureka配置
eureka:
  instance:
    hostname: localhost #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面 不配置默认访问端口8761
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #动态配置

编写启动类

package com.jgn.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
// 启动之后,访问http://localhost:7001/
@SpringBootApplication
@EnableEurekaServer // EnableEurekaServer 服务端的启动类,可以接受别人注册进来
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}

注册

在要注册的module的依赖中加入eureka相关依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

在该module的applction.yml文件中配置,让defaultZone与注册中心保存一致。

#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

在该module的启动类添加注解来启动注册

package com.jgn.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class,args);
    }
}

测试,先启动服务端,再启动客户端

5.png

Eureka集群

分别配置三个eurekaServer,使用不同的端口号,分别为7001,7002,7003

6.png

在各自的配置文件进行配置

server:
  port: 7001
#Eureka配置
eureka:
  instance:
    hostname: eureka7001.com #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面 不配置默认访问端口8761
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #动态配置
      # 单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #动态配置
      # 集群(管连) defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #动态配置
server:
  port: 7002
#Eureka配置
eureka:
  instance:
    hostname: eureka7002.com #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面 不配置默认访问端口8761
      defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7001.com:7001/eureka/ #动态配置
server:
  port: 7003
#Eureka配置
eureka:
  instance:
    hostname: eureka7003.com #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面 不配置默认访问端口8761
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ #动态配置

将提供者provider在集群中完成注册,修改application.yml

#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 #修改eureka上的默认描述信息

CAP原则

ACID:RDBMS(MySQL、Oracle、sqlServer)

CAP:NoSQL(redis、mongdb)

CAP的三进二:CA、AP、CP

CAP的核心

一个分布式系统不可能同时很好的满足一致性、可用性和分区容错性这三个需求

根据CAP原理,将NoSQL数据库分成了满足CA原则、满足CP原则和满足AP原则三大类:

zookeeper保证的是CP

Eureka保证的是AP

上一篇下一篇

猜你喜欢

热点阅读