SpringCloud-2-Eureka

SpringCloud ---- Eureka

1. Eureka Server

注意, 使用SpringBoot2.2.5以上版本时, 运行Eureka要使用Hoxton.SR1以上版本的SpringCloud

1. 添加依赖

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

    <artifactId>SpringCloud-Eureka-7001</artifactId>

    <!--导包-->
    <dependencies>
        <!--Eureka server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>


</project>

注意

  • 这里是Eureka注册中心的配置, 因此导入的maven是server

2. 注册中心的配置

server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: localhost   #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己, 由于我们这里是服务端, 不需要注册自己
    fetch-registry: false   # 如果 fetch-registry 为false, 则表示自己为注册中心
    service-url:  #监控页面
	defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  #这里使用${}动态配置, hostname和端口在上面定义了

注意

  • 此处在resources下的 application.yaml 中配置

  • url使用${}动态配置

  • url最后必须是/eureka/, 千万不能写错, 否则无法注册

  • 下面的配置很重要, 是表明自己是注册中心的重要配置

    client:
        register-with-eureka: false 
        fetch-registry: false   
    

3. 配置主启动类

package com.wang.springcloud;

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

@SpringBootApplication
//表示他是一个服务端的启动类, 可以接受别人注册进来
@EnableEurekaServer
//启动之后访问 http://localhost:7001/ 就好了
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class, args);
    }
}

注意

  • 主启动类只需要添加@ EnableEurekaServer 注解, 表明自己是一个注册中心并启动EurekaServer功能

2. 将服务添加的Eureka

这里是将provider添加到Eureka中

由于consumer是通过RestTemplate访问url的, 因此不用Eureka管理

1. 添加依赖

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

注意

  • 与注册中心不同, 这里导入的Maven直接是eureka

2. Eureka的配置

在provider中的配置文件中, 添加以下配置

#Eureka配置, 配置该服务注册到哪里(与Server中的url地址一致)
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-provider-dept8001  #修改Eureka上的默认描述信息

注意

  • url与注册中心保持一致, 这样才能注册到注册中心去

  • 通过 eureka.instance.instance-id = XXX 可以修改Eureka上的默认描述信息

    image-20201010135132410

3. 配置主启动类

package com.wang.springcloud;

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

//启动类
@SpringBootApplication
//在服务启动后自动将该服务注册到Eureka中
@EnableEurekaClient
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class, args);
    }
}

注意

  • 只需要添加 @EnableEurekaClient 注解, 就可以将服务注册到Eureka中, 与注册中心的注解不同, 不要写错!

4. Eureka的保护机制

停掉8001端口的服务,过一段时间后Eureka会显示以下内容

image-20201010135436024

这是一种保护机制: 好死不如赖活着

它的架构哲学是宁可保留错误的注册服务信息, 也不盲目注销任何可能健康的服务实例

可以使用 eureka.server.enable-self-preservation = false 来禁用自我保护机制(不推荐关闭!)

3. 服务的监控

为了方便与多人协同开发, 提供一些服务的信息是十分必要的, 这里依旧是对provider进行操作

1. 添加依赖

<!--actuator 完善监控信息-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. 配置监控信息(info)

在application.yaml中, 添加如下的配置

#监控信息配置 info
info:
  app.name: wang-springcloud
  company.name: wang.study.com

3. 获取服务的信息

在controller中, 添加获取服务的内容, 这里为了方便查看, 打印到了控制台

package com.wang.springcloud.controller;

import com.wang.springcloud.pojo.Dept;
import com.wang.springcloud.service.DeptService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供RestFul服务!
@RestController
@ApiModel("Provider Controller")
public class DeptController {

    //注册DiscoveryClient, 注意此时要导入的包是SpringCloud的
    //获取一些配置的信息, 得到具体的微服务
    @Autowired
    private DiscoveryClient client;

    //注册进来的微服务, 获得一些信息(得到配置文件中的info的信息)
    @ApiOperation("微服务的信息")
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery => services: " + services);

        //得到一个具体的微服务, 通过具体的微服务ID, applicationName(即为在配置文件中配置的该SpringBoot的名字!)
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");

        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" +
                    instance.getPort() + "\t" +
                    instance.getUri() + "\t" +
                    instance.getServiceId()
            );
        }

        //返回这个client就可以了
        return this.client;
    }

}

注意

  • 要自动装配DiscoveryClient, 要导入的包是SpringCloud的

  • 返回值为当前的DiscoveryClient对象

  • 微服务的ID为在配置文件中配置的 spring.application.name 的名字的大写

  • info配置后, 点击status会跳转, 页面显示在配置文件中配置的内容

    image-20201010140253726

4. 配置主启动类

package com.wang.springcloud;

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

//启动类
@SpringBootApplication
//在服务启动后自动将该服务注册到Eureka中
@EnableEurekaClient
//服务发现, 这样就可以监控了
@EnableDiscoveryClient
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class, args);
    }
}

注意

  • 在主启动类中, 添加 @EnableDiscoveryClient 服务发现, 这样就可以监控了

4.Eureka集群

我们配置多个Eureka注册中心来注册服务

集群的好处: 如果一个注册中心崩掉, 不会影响整个服务, 只需要切换端口就可以了

1. 开启多个Eureka注册中心

这里添加了7002和7003两个端口的注册中心

image-20201010160236925

2. 修改本机的hosts文件

由于是在单机测试, 要修改hosts文件,理由如下

在单机上测试集群要注意, hostname不能使用同一个localhost, 要在hosts文件中用多个name映射127.0.0.1端口, 这是由于Eureka会把hostname相同的url移除掉

127.0.0.1       eureka7001.com
127.0.0.1       eureka7002.com
127.0.0.1       eureka7003.com

3. 修改配置文件

这里需要将三个注册中心通过url两两关联, 以7001端口为例, 关联7002和7003端口的注册中心

server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: localhost   #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己, 由于我们这里是服务端, 不需要注册自己
    fetch-registry: false   # 如果 fetch-registry 为false, 则表示自己为注册中心
    service-url:  #监控页面
      #集群(与其他的Eureka关联) 这里绑定7002和7003端口的Eureka
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

注意

  • 多个url之间用逗号分隔,千万不要打空格

4. 修改服务的配置文件

只需要修改provider发布的url即可, 将三个注册中心的url都添加到配置文件中就好了

#Eureka配置, 配置该服务注册到哪里(与Server中的url地址一致)
eureka:
  client:
    service-url:
      #向集群发布, 只需要向所有的Eureka发布url就可以了
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001  #修改Eureka上的默认描述信息

5. 测试结果

将三个注册中心以及provider服务都启动, 最终可以看到实现了集群的Eureka

image-20201010161223202

posted @ 2020-10-10 16:19  山人西来  阅读(224)  评论(0编辑  收藏  举报