本篇博客简单记录一下,eureka 服务端和 客户端的简单搭建。

目标:

1、完成单机 eureka server 和 eureka client 的搭建。

2、完成eureka server 的添加安全认证,即不能别人知道我们的eureka server地址就可以注册上去。

3、测试环境下,关闭eureka的自我保护

一、eureka server 端的搭建

1、引入依赖

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

注意:

1、由于服务端需要保护,因此还引入了security依赖。

2、编写配置文件

spring:
application:
name: eureka-server server:
port: 8761
tomcat:
uri-encoding: utf-8
eureka:
client:
register-with-eureka: false # 由于eureka即可以作为服务端也可以作为客户端,此处是作为服务器段,因此这个参数需要设置成false: 即不作为一个客户端注册到服务注册中心
fetch-registry: false # true:表示作为一个客户端中eureka server 服务端获取服务注册信息,此处作为一个服务端因此需要设置成 false
service-url:
defaultZone : http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
instance:
hostname: localhost
server:
# 此处表示关闭 eureka 的自我保护
enable-self-preservation: false
# 清理无效节点的时间间隔,默认是60s,此处修改成10s
eviction-interval-timer-in-ms: 10000 security:
basic:
enabled: true # 开启basic认证
user:
name: root # 用户名
password: admin # 密码

注意:

1、默认情况下eureka即可以做为服务端,也可以做为客户端,此处作为服务端,因此需要将 register-with-eureka的值改成false,即不注册到eureka server上。

           2、fetch-registry: 服务端这个值 需要改成 false, 即不去检索服务。

           3、security 开头的配置是因为引入了spring security保护server端,因此 需要注意 service-url 中的 defaultZone 的值的写法 : http://用户名:密码@主机:端口/eureka/

           4、enable-self-preservation的值设置成 false 表示 关闭eureka的自我保护。

                     客户端需要修改下方2个参数的值,正式环境不建议修改。

            # 客户端与服务器断心跳的时间间隔,默认为 30秒
lease-renewal-interval-in-seconds: 3
# 租约到期时间,此值不可过小,开发环境小点没有事,默认为 90秒
lease-expiration-duration-in-seconds: 9

3、编写启动类

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

   注意:

        1、 eureka server 的服务端上需要加上  @EnableEurekaServer 注解,表示作为服务端启动。

二、eureka client 端的搭建

1、引入依赖

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

2、编写配置文件

server:
port: 8762 eureka:
client:
service-url:
defaultZone : http://${security.user.name}:${security.user.password}@localhost:8761/eureka/ #连接到服务注册中心的地址,如果服务注册中心开启了权限需要设置 http://username:password@ip:port/eureka/格式
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port} # 客户端与服务器断心跳的时间间隔,默认为 30秒
lease-renewal-interval-in-seconds: 3
# 租约到期时间,此值不可过小,开发环境小点没有事,默认为 90秒
lease-expiration-duration-in-seconds: 9 security:
user:
name: root
password: admin
spring:
application:
name: eureka-client info:
app:
name: "eureka-client"
description: "eureka-client程序"
version: "0.0.1"

注意:

    1、注意一下注册到 eureka server 上 url 的编写格式。

           2、spring.application.name 表示注册到eureka服务上的名字,建议小写。

3、编写启动类

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

    注意:

           1、eureka client需要加上 @EnableDiscoveryClient 注解,表示这个一个客户端。

三、运行界面

1、浏览器上输入 : http://localhost:8761 回车后,填写用户名(root)/密码(admin) 登录即可看到这个页面

 四、完整代码地址:

https://gitee.com/huan1993/spring-cloud-parent.git  里面 eureka-server和eureka-client即为本篇博客的代码。

eureka服务端和客户端的简单搭建的更多相关文章

  1. SpringCloud02 Eureka知识点、Eureka服务端和客户端的创建、Eureka服务端集群、Eureka客户端向集群的Eureka服务端注册

    1 Eureka知识点 按照功能划分: Eureka由Eureka服务端和Eureka客户端组成 按照角色划分: Eureka由Eureka Server.Service Provider.Servi ...

  2. thrift服务端到客户端开发简单示例

    (1)首先我们在服务器端写个helloworld.thrift文件,如下所示: service HelloWorld{ string ping(1: string name), string getp ...

  3. oauth2.0服务端与客户端搭建

    oauth2.0服务端与客户端搭建 - 推酷 今天搭建了oauth2.0服务端与客户端.把搭建的过程记录一下.具体实现的功能是:client.ruanwenwu.cn的用户能够通过 server.ru ...

  4. C# 编写WCF简单的服务端与客户端

    http://www.wxzzz.com/1860.html Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Win ...

  5. 三、eureka服务端获取服务列表

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 eureka服务端维护了一个服务信息的列表,服务端节点之间相互复制服务信息.而作为eur ...

  6. Spring Eureka的使用入门--服务端与客户端

    接上篇: Eureka作为注册中心,连接服务端与客户端: 服务端: 依赖包: apply plugin: 'org.springframework.boot' apply plugin: 'io.sp ...

  7. spring cloud eureka 服务端开启密码认证后,客户端无法接入问题

    Eureka服务端开启密码的认证比较简单 在pom文件中加入: <dependency> <groupId>org.springframework.boot</group ...

  8. SVN服务端和客户端的安装与搭建

    版权声明:本文为博主原创文章,转载请注明原文出处. https://blog.csdn.net/zzfenglin/article/details/50931462 SVN简介 SVN全名Subver ...

  9. Unity使用C#实现简单Scoket连接及服务端与客户端通讯

    简介: 网络编程是个很有意思的事情,偶然翻出来很久之前刚开始看Socket的时候写的一个实例,贴出来吧 Unity中实现简单的Socket连接,c#中提供了丰富的API,直接上代码. 服务端代码: [ ...

随机推荐

  1. 数据导出生成word附件使用POI的XWPFTemplate对象

    比较常用的实现Java导入.导出Excel的技术有两种Jakarta POI和Java Excel.Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组 ...

  2. 三剑客之sed编辑器 基操

    目录: 一.sed编辑器 二.打印内容 三.使用地址 四.删除行 五.替换 六.插入 一.sed编辑器 sed是一种流编辑器,流编辑器会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流. sed ...

  3. 部署yum仓库以及NFS共享服务

    目录: 一.YUM概述 二.准备安装源 三.访问YUM仓库 四.本地YUM仓库 五.YUM工具概述 六.软件包查询.安装.卸载 七.NFS共享 一.YUM概述 YUM(Yellow dog Updat ...

  4. 内部类访问外部类成员变量,使用外部类名.this.成员变量

    public class Outer { private int age = 12; class Inner { private int age = 13; public void print() { ...

  5. 论文解读(SimCLR)《A Simple Framework for Contrastive Learning of Visual Representations》

    1 题目 <A Simple Framework for Contrastive Learning of Visual Representations> 作者: Ting Chen, Si ...

  6. final关键字在PHP中的使用

    final关键字的使用非常简单,在PHP中的最主要作用是定义不可重写的方法.什么叫不可重写的方法呢?就是子类继承后也不能重新再定义这个同名的方法. class A { final function t ...

  7. php超时报错: Maximum execution time of 300 seconds exceeded

    php.ini里max_execution_time = 30,原因是这个脚本执行时间太小了,增加一些,或者改成0不限制 可以增加代码: set_time_limit(0);

  8. 创建一个Orchard Core CMS 应用程序

    开始使用Orchard Core作为NuGet软件包 在本文中,我们将看到使用Orchard Core提供的NuGet包创建CMS Web应用程序是多么容易. 你可以在这里找到Chris Payne写 ...

  9. U2-关系数据库

    2.1 关系数据结构及形式化定义 关系数据库系统是支持关系模型的数据库系统.(关系模型由关系数据结构.关系操作集合和关系完整性约束三部分组成) 2.1.1 关系 1-域 域是一组具有相同数据类型的值的 ...

  10. Shell系列(12)- 预定义变量(5)

    预定义变量 作用 $? 常用:最后一次执行的命令的返回状态. 如果这个变量的值为0,证明上一个命令正确执行:如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了 $$ ...