本篇博客简单记录一下,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即为本篇博客的代码。
相关推荐
搭建SpringCloud中的Eureka的服务端和客户端,例子很简单,却很实用,导入项目之后,自动导入依赖,后,启动项目就可以用,还有详细的说明文档
1.搭建微服务 springcloud-parent 父类 ...一旦因为某些原因导致客户端没有按时发送心跳给 eureka 服务端,这时候 eureka 可能会认为你这个客户端已经挂了,它就有可能把该服务从服务列表中删除掉。
course注册到Eureka Server"Feign远程调用 理解 重点 能够测试Feign远程调用 "理解客户端负载均衡和服务端负载均衡能够进行Ribbon测试能够定义FeignClient接口能够测试Feign远程调用"课程预览技术方案 理解 理解...
SpringColud1简易分布式 ● cloud-config-server:配置服务器-(通过git获取配置) ● cloud-eureka-server:eureka注册服务器 ● cloud-simple-service:一个使用mybatis的数据库应用,服务端 ● cloud-simple-...
Eureka集群环境搭建 14 Eureka详解 17 使用Eureka闭源了闭源了怎么办? 21 使用Consul来替换Eureka 21 使用Zookeeper来替换Eureka 25 Consistency(一致性), 数据一致更新,所有数据变动都是同步的 Availability...
4.Eureka中的核心概念 5.什么是客户端负载均衡 6.Spring RestTemplate中几种常见的请求方式 7.RestTemplate的逆袭之路,从发送请求到负载均衡 8.Spring Cloud中负载均衡器概览 9.Spring Cloud中的负载均衡策略 ...
49.硅谷学习_SpringCloud_Config客户端通过Config服务端获得Github上的配置 50.硅谷学习_SpringCloud_Config配置演示与策略切换 51.硅谷学习_SpringCloud_重要必看.第一季架构技术总结和第二季展望
FCat是基于Angular4+SpringCloud的企业级基础功能框架(户权限管理、区域管理、GIS地图、......),其核心设计目标是分离前后端、开发快速、学习简单、功能强大、不重复造轮子,其目标是帮助企业搭建一套基础功能框架...
cloud-config|[配置中心(服务端、客户端)示例](https://github.com/smltq/spring-boot-demo/blob/master/cloud-config) cloud-feign|[Eureka服务注册中心、负载均衡、声明式服务调用]...