springCloud—服务注册(eureka集群)

一、原理:

(1)、为啥要拆成微服务?

 1. 传统单体服务,业务太杂了,代码太杂了,都像霍水泥一样霍在一起。比如说,改个订单服务功能,和其它服务没有任何交集的,也要整个项目一起部署,太麻烦!
 2. 传统单体服务,有个地方保存,可能导致整个项目崩溃。
 3. 万一有个什么业务功能,需要用到新技术。单体服务的话技术更新换代都麻烦些。

(2)、关于eureka:

eureka说白了,就是个用来注册和发现服务的组件。其实我的理解:这玩意就是代替作用就是:

之前我们写单体服务的时候,要写个static类,专门记录外部应用的地址,用于HttpClicent或者webService调用。
现在我们用eureka,把这些地址直接注册到一个注册中心,并且这个注册中心是高可用的(后面的博客会说到,
其实就是死了一个,还有另外的撑着!)。然后微服务之间相互调用的时候,地址就从这个注册中心去取。

二、具体实现需要做的配置:

1、先在pom.xml引入spring-cloud-starter-eureka-server
2、启动类加注解:@EnableEurekaServer
3、yml文件设置配置以下比较注主要的信息:

	eureka.instance.hostname:这个是设置本服务注册到注册中心时,按照主机名称注册,其它服务访问的时候,也会按照主机名称+端口访问
	eureka.instance.instance-id:这个是设置本服务注册到注册中心的实例id(注册中心界面上显示的样式)
	prefer-ip-address: true :按照ip地址注册到注册中心,设置这个之后,本服务会以ip地址的形式注册到注册中心,其它项目调用的时候,也会按照ip地址来调用。
	register-with-eureka: false   不将本服务注册到注册中心,如果不需要eureka集群(死了一个注册中心,还有另外一个),这里可以设置为false,就是不将本服务注册到中心
	fetch-registry: false 不扫描注册中心的列表,不发现其它微服务.
	eureka.client.service-url.defaultZone: 设置要将本服务注册到那个注册中心。

三、实际操作:

来做一个eureka集群的例子,就是死了一个注册中心还有另外一个:

(1)、首先,新建一个项目eureka-server.
		修改host文件:添加两个主机名:
			127.0.0.1      peer1 
			127.0.0.1      peer2
	
<1>、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.base</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

说明:

引入org.springframework.cloud、org.springframework.boot、spring-cloud-starter-eureka-server包

<2>、启动类加上注解:@EnableEurekaServer

package com.base.eurekaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

<3>、yml配置:

spring:
  application:
    name: eureka-server  #应用名称
---
spring:
  profiles: peer1
server:
  port: 1001
eureka:
  instance:
    hostname: peer1  #服务端(设置当前实例的主机名称,服务默认是按照主机名称+端口号注册到中心的)
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}   #注册中心上显示的id
  client:
    service-url:
      defaultZone: http://peer1:1001/eureka/,http://peer2:1002/eureka/
    #client:
    ##    register-with-eureka: false  #不把本服务注册到注册中心
    ##    fetch-registry: false        #不检索其它服务
  server:
    enable-self-preservation: false     #关闭自我保护机制,微服务访问不了,就马上将注册信息在注册中心删除掉,不保留了

---
spring:
  profiles: peer2
server:
  port: 1002
eureka:
  instance:
    hostname: peer2  #服务端(设置当前实例的主机名称,服务默认是按照主机名称+端口号注册到中心的)
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}   #注册中心上显示的id
  client:
    service-url:
      defaultZone: http://peer1:1001/eureka/,http://peer2:1002/eureka/

说明:

这里设置两个服务实例,分别启动1001和1002端口,运行两个服务,这两个服务相互注册。
注册到中心后,每个服务会缓存一份实例注册名单到本地,所以就可以注册中心集群高可以了,
1001死了,1002还在,1002上面还有所以微服务的名单。

我们访问http://peer1:1001/eureka/,和http://peer2:1002/eureka/,看下效果:

在这里插入图片描述

在这里插入图片描述

先关注画框框的地方,这里如果是这样,就代表eureka集群成功了,你中有我,我中有你,死了一个也不怕!!!
其它的,下个博客再解释。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值