本篇博客简单记录一下,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. throws声明异常中断式处理异常

    1.throws 编译期异常,一直往上抛最后是JVM处理(打印并中断程序) 2.声明多个或者直接声明父类

  2. Tomcat配置支持war包部署

    Tomcat配置支持war包部署 #cat /data/tomcat/conf/server.xml <?xml version='1.0' encoding='utf-8'?> < ...

  3. 理解MySQL回表

    回表就是先通过数据库索引扫描出数据所在的行,再通过行主键id取出索引中未提供的数据,即基于非主键索引的查询需要多扫描一棵索引树. 因此,可以通过索引先查询出id字段,再通过主键id字段,查询行中的字段 ...

  4. Wpf读写Xaml文件

    前言 本文主要介绍Wpf读写Xaml文件. 读写实现 首先我们使用XamlWriter将Wpf的对象转换为Xaml字符串,代码如下: var btn = sender as Button; strin ...

  5. BZOJ_1008 越狱(快速幂)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1008 Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教 ...

  6. javascript,jquery在父窗口触发子窗口(iframe)某按钮的click事件

    $('iframe').contents().find(".btn").click(); 其中 contents(): 查找匹配元素内部所有的子节点(包括文本节点).如果元素是一个 ...

  7. (转载)深入理解MDL元数据锁

    作者:MySQL技术本文为作者原创,转载请注明出处:https://www.cnblogs.com/kunjian/p/11993708.html 前言: 当你在MySQL中执行一条SQL时,语句并没 ...

  8. Java面向对象系列(12)- Static关键字讲解

    场景一:静态变量 package oop.demo07; public class Student { private static int age;//静态的变量 一般多线程用的比较多 privat ...

  9. Postman 如何调试加密接口?

    大家好,我是安果! 众所周知,Postman 是一款非常流行且易用的 API 调试工具,在接口调试或测试时经常被使用针对普通 API 接口,我们可以直接在 Postman 中输入 URL.Query ...

  10. [转载]用redis实现跨服务器session

    地址:http://blog.chinaunix.net/uid-11121450-id-3284875.html 这个月我们新开发了一个项目,由于使用到了4台机器做web,使用dns做负载均衡, 上 ...