服务的注册与发现

关系调用说明:

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

Eureka简介

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

1.创建Eureka Server

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

next

next

next

finish

目录结构如下

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

启动工程

在浏览器中输入

http://localhost:8761/

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

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

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

此处与上面不同

finish

目录结构

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

启动项目

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

服务已经注册成功

但报红色字体

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,后,正常

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

http://localhost:8762/hi

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后要求输入密码

idea搭建Eureka注册中心的更多相关文章

  1. 14 微服务电商【黑马乐优商城】:day02-springcloud(搭建Eureka注册中心)

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...

  2. 搭建Eureka注册中心

    创建一个Spring Boot工程,命名为eureka-server,并在pom.xml中引入必要的依赖,代码如下. <parent> <groupId>org.springf ...

  3. spring cloud 专题一 (spring cloud 入门搭建 之 Eureka注册中心搭建)

    一.前言 本文为spring cloud 微服务框架专题的第一篇,主要讲解如何快速搭建spring cloud微服务及Eureka 注册中心 以及常用开发方式等. 本文理论不多,主要是傻瓜式的环境搭建 ...

  4. Spring Cloud第三篇 | 搭建高可用Eureka注册中心

    ​ ​本文是Spring Cloud专栏的第三篇文章,了解前两篇文章内容有助于更好的理解后面文章: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...

  5. (转)微服务_创建一个简单的Eureka注册中心

    原文地址:https://www.cnblogs.com/lplshermie/p/9105329.html 微服务和分布式已经成了一种极其普遍的技术,为了跟上时代的步伐,最近开始着手学习Spring ...

  6. 创建一个简单的Eureka注册中心

    微服务和分布式已经成了一种极其普遍的技术,为了跟上时代的步伐,最近开始着手学习SpringCloud,就从Eureka开始.他们俩就不做介绍了,网上的说明一堆,随便打开一个搜索引擎输入关键字都足够了解 ...

  7. springCloud系列教程01:Eureka 注册中心集群搭建

    springCloud系列教程包含如下内容: springCloud系列教程01:Eureka 注册中心集群搭建 springCloud系列教程02:ConfigServer 配置中心server搭建 ...

  8. 架构师入门:搭建双注册中心的高可用Eureka架构(基于项目实战)

    本文的案例是基于 架构师入门:搭建基本的Eureka架构(从项目里抽取) 改写的. 在上文里,我们演示Eureka客户端调用服务的整个流程,在这部分里我们将在架构上有所改进.大家可以想象下,在上文里案 ...

  9. Spring-cloud (一):Eureka注册中心搭建

    前提 系统安装jdk1.8及以上,配置好maven的ide(这里用idea进行演示,maven版本3.5,配置阿里云源) 项目搭建 新建一个maven项目,创建最简单的那种就好,项目名这里为Eurek ...

随机推荐

  1. 2017年3月28日15:59:16 终于明白spring这套鬼东西是怎么玩的了

    先说重点,新东家公司的项目框架没有一样是我之前用过的,首先pm和我说的是一套微服务的概念,微服务不同于传统的功能模块实现,他将服务松散化分不到各个系统之间,这样也是实现分散压力的一种. 微服务是由sp ...

  2. wc语法

    统计当前目录下的所有文件行数: wc -l * 当前目录以及子目录的所有文件行数: find  . * | xargs wc -l 可以把*改成所要匹配的文件,例如Java文件,*.java这样就只统 ...

  3. centos7.3 64位 安装git

    1.安装编译git时需要的包 # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel # yum ins ...

  4. 剑指Offer 34. 第一个只出现一次的字符 (字符串)

    题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 题目地址 https:// ...

  5. oracle 如何查询/修改dmp文件的字符集

    1.如何查询dmp文件的字符集 用oracle的exp工具导出的dmp文件也包含了字符集信息,dmp文件的第2和第3个字节记录了dmp文件的字符集.如果dmp文件不大,比如只有几M或几十M,可以用Ul ...

  6. rootfs 制作

    最小 根文件系统 (1)/dev/console(终端控制台, 提供标准输入.标准输出以及标准错误) /dev/null (表示空设备终端, 所有写到这个文件中的数据都会被丢弃掉.) (2)init进 ...

  7. Python全栈之路----函数进阶----名称空间

    又名name space,顾名思义就是存放名字的地方,存什么名字呢?举例说明,若变量x=1,1存放于内存中,那名字x存放在哪里呢?名称空间正是存放名字x与1绑定关系的=地方 名称空间共3种,分别如下 ...

  8. 编译Linux-4.15.1内核时遇到:“error : openssl/bio.h :No such file or folder”

    如题: scripts/extract-cert.c::: fatal error: openssl/bio.h: No such file or directory compilation term ...

  9. PythonStudy——赋值运算符 Assignment operator

    eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...

  10. 在centos7中安装python3并设置为默认版本

    1,查看Python默认版本 python -V (大写V) 2,看看执行Python在哪个位置 which python 3,安装gcc,用于编译Python源码 yum install gcc 4 ...