idea搭建Eureka注册中心及配置密码登陆 1.创建Eureka Server 2.创建一个服务提供者 (eureka client)  3.添加Eureka密码验证

 服务的注册与发现

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

关系调用说明:

  • 服务生产者启动时,向服务注册中心注册自己提供的服务
  • 服务消费者启动时,在服务注册中心订阅自己所需要的服务
  • 注册中心返回服务提供者的地址信息个消费者
  • 消费者从提供者中调用服务

Eureka简介

Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来。
Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。
客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。

File--》new-->project-->spring initializr

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

next

 idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

next

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

next

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

finish

目录结构如下

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

在Application类上添加注解@EnableEurekaServer声明注册中心

package com.pu.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);
    }

}

在resource/application.yml(默认是.properties)配置文件添加图中内容

server:
port: 8761
eureka:
instance:
hostname: localhost
server:
#是否开启自我保护机制, 测试时关闭自我保护机制,保证不可用服务及时踢出
enable-self-preservation: false
# 剔除失效服务间隔,默认60000 ms
eviction-interval-timer-in-ms: 3000
client:
#声明自己是服务端,表示是否将自己注册到Eureka Server,默认为true。由于当前这个应用就是Eureka Server,故而设为false
register-with-eureka: false
#表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false
fetch-registry: false
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pu</groupId>
    <artifactId>eurekaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekaserver</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</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>

 启动工程

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

在浏览器中输入

http://localhost:8761/

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

这里显示No instances available是因为还没有服务注册上去

2.创建一个服务提供者 (eureka client)

File-->new-->project-->spring initializr

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

此处与上面不同

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

 finish

目录结构

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pu</groupId>
    <artifactId>eurekaclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekaclient</name>
    <description>Demo project for Spring Boot</description>

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

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

在EurekaclientApplication添加注解@EnableEurekaClient

package com.pu.eurekaclient;

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

@EnableEurekaClient
@SpringBootApplication
public class EurekaclientApplication {

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

}

配置resource/application.yml


spring:
application:
name: service-hi
server:
port: 8762
eureka:
client:
serviceUrl:
#指向注册中心
#defaultZone: http://192.168.111.133:8888/eureka/
    defaultZone: http://localhost:8761/eureka/
instance:
# 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
lease-renewal-interval-in-seconds: 1
# 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
lease-expiration-duration-in-seconds: 2

创建controller类

package com.pu.eurekaclient;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class hiController {
    @RequestMapping(value="/hi")
    public String say(){
        String str="my spring boot web project hihihi";
        System.out.println(str);
        return str;
    }
}

启动项目

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

 查看刚刚的服务端,发现服务service-hi已经注册成功

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

服务已经注册成功

但报红色字体

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

这是因为eureka server开启了SELF PRESERVATION模式,导致registry的信息不会因为过期而被剔除掉,直到退出SALF PRESERVATION模式。

针对SALF PRESEVATION的问题,在测试环境可以将自我保护模式关闭:

即在eurekasrver工程resource/application.yml添加

server:
#是否开启自我保护机制, 测试时关闭自我保护机制,保证不可用服务及时踢出
enable-self-preservation: false

重启server,client,后,正常

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

访问client服务,在浏览器中输入

http://localhost:8762/hi

idea搭建Eureka注册中心及配置密码登陆
1.创建Eureka Server
2.创建一个服务提供者 (eureka client)
 3.添加Eureka密码验证

 3.添加Eureka密码验证

pom中引入此依赖

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

yml中添加

server:
  port: 8761
spring
  security:
  user:
    name: aa
    password: 111111
eureka:
  instance:
    hostname: localhost
  server:
    #是否开启自我保护机制, 测试时关闭自我保护机制,保证不可用服务及时踢出
    enable-self-preservation: false
    # 剔除失效服务间隔,默认60000 ms
    eviction-interval-timer-in-ms: 3000
  client:
    #声明自己是服务端,表示是否将自己注册到Eureka Server,默认为true。由于当前这个应用就是Eureka Server,故而设为false
    register-with-eureka: false
    #表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false
    fetch-registry: false
    #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔
    service-url:
      defaultZone: http://aa:111111@${eureka.instance.hostname}:${server.port}/eureka/

关闭CSRF,否则导致无法注册任何服务

package com.pu.eurekaserver.config;

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;


/**
 * 配置security的csrf检验为false
 * 作用:security模块用于配置登陆Eureka界面登陆时需要输入用户名密码
 * 此模块同时会开启csrf功能导致所有服务注册不进Eureka,需在此处显示关闭
 *
 * @Author 
 * @date 20190826 15:25:49
 * @modify 20190826 15:25:49 v0.1 创建
 * @since v0.1
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}

重启服务再次进入http://localhost:8761后要求输入密码