SpringCloud七、Eureka是什么、Eureka Server服务注册中心建立、将已有的部门微服务注册进Eureka服务中心。

①eureka是什么?

Netflix在设计Eureka时遵守的就是AP原则

Eureka是什么?

Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。服务注册与发现对于微服务架构来说是非常重要的,有了服务发现与注册(主管服务发现与注册),只需要使用服务的标识符(某一个微服务的名称,注册进eureka就可以通过eureka,找到这个微服务。),就可以访问到服务,而不需要修改服务调用的配置文件了。功能类似于dubbo的注册中心,比如Zookeeper。
 

②eureka的原理。

Eureka的基本架构是什么?
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper)。

Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。

而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。
 

请注意和Dubbo的架构对比

扫描二维码关注公众号,回复: 8832146 查看本文章

dubbo的zookeeper的架构如下:

Eureka包含两个组件:Eureka Server和Eureka Client
Eureka Server提供服务注册服务
各个节点(每个微服务)启动后,会在Eureka   Server中进行注册,这样Eureka    Server中的服务注册表中将会存储所有可用服务节点(每一个微服务)的信息,服务节点(每一个微服务)的信息可以在界面中直观的看到
 

Eureka  Client是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka   Server将会从服务注册表中把这个服务节点移除(默认90秒)

Eureka Server 提供服务注册和发现。

Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到。

Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务。

盘点下目前我们的工程情况:

总父工程、通用模块api、服务提供者Provider、服务消费者Consumer。

③Eureka  Server服务注册中心建立。

第一步:新建microservicecloud-eureka-7001

创建microservicecloud-eureka-7001,eureka服务注册中心Module,这个module相当于eureka server。

microservicecloud-eureka-7001相当于蓝色的eureka  server部分。

第二步:修改pom文件。

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.0</modelVersion>

  <parent>
   <groupId>com.lss.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>

  <artifactId>microservicecloud-eureka-7001</artifactId>

  <dependencies>
   <!--eureka-server服务端 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka-server</artifactId>
   </dependency>
   <!-- 修改后立即生效,热部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
  </dependencies>

</project>

然后update一下。

然后发现 update,spring cloud没有更新的jar包。需要更改配置文件:

setting.xml的部分内容改为:

 <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
	 <!--
    <mirror>
      <id>centralMaven</id>
      <mirrorOf>central</mirrorOf>
      <name>central maven</name>
      <url>http://central.maven.org/maven2/</url>
    </mirror>
-->
	 <mirror>
      <id>centralMaven02</id>
      <mirrorOf>central02</mirrorOf>
      <name>central maven02</name>
      <url>https://repo.spring.io/milestone/org/springframework/cloud/</url>
    </mirror>





  </mirrors>

 <mirror>
      <id>centralMaven02</id>
      <mirrorOf>central02</mirrorOf>
      <name>central maven02</name>
      <url>https://repo.spring.io/milestone/org/springframework/cloud/</url>
    </mirror>

 

然后重新update,这次所需时间较长,且可以下载相对应的jar包。 

第三步:写yml文件。

application.yml的内容是:

server: 
  port: 7001

eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    register-with-eureka: false #false表示不向注册中心注册自己。
    fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/     
   #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。

第四步:创建EurekaServer7001_App主启动类。

EurekaServer7001_App.java的内容是:

package com.lss.springcloud;

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

@SpringBootApplication
@EnableEurekaServer//EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001_App {

	public static void main(String[] args) {

		SpringApplication.run(EurekaServer7001_App.class, args);
	}

}

主要是要加注解@EnableEurekaServer

第五步:测试。

先要启动EurekaServer:

http://localhost:7001/

④将已有的部门微服务注册进Eureka服务中心。

修改microservicecloud-provider-dept-8001

第一步:修改pom文件:

  <!-- 将微服务provider侧注册进eureka,注意,没有-server,说明是client端 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   

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>

  <parent>
   <groupId>com.lss.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>

  <artifactId>microservicecloud-provider-dept-8001</artifactId>

  <dependencies>
   <dependency><!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
     <groupId>com.lss.springcloud</groupId>
     <artifactId>microservicecloud-api</artifactId>
     <version>${project.version}</version>
   </dependency>
   
    <!-- 将微服务provider侧注册进eureka,注意,没有-server,说明是client端 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   
   
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
   </dependency>
   <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
   </dependency>
   <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
   </dependency>
   <dependency>
     <groupId>ch.qos.logback</groupId>
     <artifactId>logback-core</artifactId>
   </dependency>
   <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
   </dependency>
   <!-- 修改后立即生效,热部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
  </dependencies>

</project>

第二步:修改yml文件。

修改部分:

eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://localhost:7001/eureka

修改以后yml的全部内容是:

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
spring:
   application:
    name: microservicecloud-dept 
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/clouddb01              # 数据库名称
    username: root
    password: lss19881218
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间

 
eureka:
  client:
    service-url: 
      defaultZone: http://localhost:7001/eureka

第三步:修改DeptProvider8001_App主启动类。

新增注解:@EnableEurekaClient //本服务启动后会自动注册进eureka服务中

修改后DeptProvider8001_App.java的全部内容是:

package com.lss.springcloud;

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

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
public class DeptProvider8001_App {

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

}

先要启动EurekaServer,即先启动7001。

再启动EurekaClient,即再启动8001。

测试:

发布了131 篇原创文章 · 获赞 1 · 访问量 6705

猜你喜欢

转载自blog.csdn.net/lbh19630726/article/details/104059321