Fork me on GitHub

Spring Cloud Eureka的学习

父项目配置

新建一个maven项目,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>

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

    <groupId>com.tiger.springcloud</groupId>
    <artifactId>springCloud-learning</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Eureka server for Spring Cloud</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>


</project>

加载完依赖以后,可以把src目录删了。

Eureka

搭建单个EurekaServer

新建一个maven模块eureka-Server-8001,并在pom.xml文件中添加eureka-server的依赖

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

在配置文件application.yml中添加Eureka注册中心的配置

server:
  port: 8001 #指定运行端口
spring:
  application:
    name: eureka-server #指定服务名称
eureka:
  instance:
    hostname: localhost #指定主机地址
  client:
    fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
    register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    
  server:
    enable-self-preservation: false #关闭保护模式

注意:在这里踩了个坑,就是把application.yml文件名改了application8001.yml,导致配置文件运行为默认配置,从而报错。约定大于配置

src目录下的java目录新建一个包并创建一个启动类

package com.tiger.springcloud;

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

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

运行启动类以后,在浏览器访问地址http://localhost:8001就看到以下Eureka注册中心的界面了

单个eurekaServer配置.png

搭建Eureka客户端

新建一个maven模块eureka-client,并在pom.xml文件中添加如下依赖:

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

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

resources目录下的配置文件application.yml中添加Eureka客户端的配置

server:
  port: 8101 #运行端口号
spring:
  application:
    name: eureka-client #服务名称
eureka:
  client:
    register-with-eureka: true #注册到Eureka的注册中心
    fetch-registry: true #获取注册实例列表
    service-url:
      defaultZone: http://localhost:8001/eureka/ #配置注册中心地址

在启动类上添加@EnableDiscoveryClient注解表明是一个Eureka客户端

package com.tiger.eurekaClient;

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

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

同时运行eurekaServerEurekaClient启动类。在浏览器访问地址http://localhost:8001,看到如下画面:

eurekaClient配置.png

上图中可看到EUREKA-CLIENT的实例。

搭建Eureka注册中心集群

由于所有服务都会注册到注册中心去,服务之间的调用都是通过从注册中心获取的服务列表来调用,注册中心一旦宕机,所有服务调用都会出现问题。所以我们需要多个注册中心组成集群来提供服务,下面将搭建一个双节点的注册中心集群。

把前面的eureka-server-8001application.yam文件做如下修改:

server:
  port: 8001
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: replica1
  client:
    serviceUrl:
      defaultZone: http://replica2:8002/eureka/ #注册到另一个Eureka注册中心
    fetch-registry: true
    register-with-eureka: true

同时新建一个maven模块的eureka-server-8002的注册中心,参考eureka-server-8001来做包括pom.xml的依赖,然后在eureka-server-8001模块下的application.yml文件修改如下:

server:
  port: 8002 #指定运行端口
eureka:
  instance:
    hostname: replica2
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://replica1:8001/eureka/ #注册到另一个Eureka注册中心

spring:
  application:
    name: eureka-server

启动类的配置如下:

package com.springcloud.eurekaserver;

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

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

这里我们通过两个注册中心互相注册,搭建了注册中心的双节点集群,由于defaultZone使用了域名,所以还需在本机的host文件中配置一下。

修改本地host文件:

127.0.0.1 replica1
127.0.0.1 replica2

运行单个eurekaServer的启动类的时候,发现代码有如下图片中的错误,该错误如由于另外一个eurekaServer还没启动,从而有这错误信息,但是不影响注册中心的正常工作。

eurekaServer启动error.png

两个eurekaServer的启动类都成功启动以后,访问http://replica1:8001/,如下图所示:

eurekaServer集群8001.png

访问http://replica2:8002/,如下图所示:

eurekaServer集群8002.png

修改Eureka-client,让其连接到eureka-sever的集群中

server:
  port: 8101 #运行端口号
spring:
  application:
    name: eureka-client #服务名称
eureka:
  client:
    register-with-eureka: true #注册到Eureka的注册中心
    fetch-registry: true #获取注册实例列表
    service-url:
      defaultZone: http://replica1:8001/eureka/ , http://replica2:8002/eureka/#配置注册中心地址

给Eureka注册中心添加认证

新建一个eureka-security-servermaven模块,在pom.xml文件中添加以下依赖:

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

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

application.yam的配置:

server:
  port: 8003
spring:
  application:
    name: eureka-security-server
    ##############################
  security: #配置SpringSecurity登录用户名和密码
    user:
      name: tiger
      password: 123456
    ##############################
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false

在启动类的同名包下新建一个conf包,并新添一个类:

package com.springcloud.security.conf;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity http) throws Exception{
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。

启动类的代码如下:

package com.springcloud.security;

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

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

运行EurekaSecurityServerApplication启动类,访问地址http://localhost:8003,会看到如下需要认证的界面

eurekaServerSecurityLogin.png

把eureka-client注册到需要登录认证的注册中心

eureka-clientapplication.yam文件做如下修改:

server:
  port: 8101 #运行端口号
spring:
  application:
    name: eureka-client #服务名称
eureka:
  client:
    register-with-eureka: true #注册到Eureka的注册中心
    fetch-registry: true #获取注册实例列表
    service-url:
      defaultZone: http://tiger:123456@localhost:8003/eureka/

运行EurekaSecurityServerApplication启动类,然后运行eureka-client的启动类,成功访问http://localhost:8003以后,会看到如下界面:

eureka-client-ca.png

Eureka的常用配置

eureka:
  client: #eureka客户端配置
    register-with-eureka: true #是否将自己注册到eureka服务端上去
    fetch-registry: true #是否获取eureka服务端上注册的服务列表
    service-url:
      defaultZone: http://localhost:8001/eureka/ # 指定注册中心地址
    enabled: true # 启用eureka客户端
    registry-fetch-interval-seconds: 30 #定义去eureka服务端获取服务列表的时间间隔
  instance: #eureka客户端实例配置
    lease-renewal-interval-in-seconds: 30 #定义服务多久去注册中心续约
    lease-expiration-duration-in-seconds: 90 #定义服务多久不去续约认为服务失效
    metadata-map:
      zone: jiangsu #所在区域
    hostname: localhost #服务主机名称
    prefer-ip-address: false #是否优先使用ip来作为主机名
  server: #eureka服务端配置
    enable-self-preservation: false #关闭eureka服务端的保护机制

注意:以上内容学习于掘金博主MacroZheng

参考链接: Spring Cloud Eureka:服务注册与发现

posted @ 2020-07-10 17:14  tiger_yam  阅读(194)  评论(0编辑  收藏  举报