热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

详解spring-cloud与netflixEureka整合(注册中心)

这篇文章主要介绍了详解spring-cloud与netflixEureka整合(注册中心),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

基础依赖

compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter') 

eureka(单机)

服务端:

依赖

compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') 

application.yml 配置

spring:
 application:
  name: dev
eureka:
 client:
  service-url:
   defaultZone: http://本机ip地址:8761/eureka/ #注册中心地址
  register-with-eureka: false #表明该实例是否应该与尤里卡服务器登记其信息被别人发现。在某些情况下,您不希望您的实例被发现而你想发现其他实例。默认为true
  fetch-registry: false #表明这个客户是否应该从尤里卡服务器获取尤里卡注册表信息。默认为true
server:
 port: 8761

启动类添加 @EnableEurekaServer

客户端:

主要依赖

compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') 

application.yml 配置

eureka:
 client:
  service-url:
   defaultZone: http://eureka服务地址:8761/eureka/ 

启动类添加 @EnableDiscoveryClient (注册中心通用客户端注解)或 @EnableEurekaClient (只有eureka客户端可用)

eureka(集群)

服务端

主要依赖

compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') 

application.yml 配置

server:
 port: 8761
eureka:
 client:
  register-with-eureka: false
  fetch-registry: false
 server:
  enable-self-preservation: false #使用自我保护,默认true
  peer-node-read-timeout-ms: 5000 #节点读取超时时间,默认200毫秒
---
spring:
 application:
  name: eureka1
 profiles: eureka1
eureka:
 client:
  service-url:
   defaultZone: http://eureka2:8761/eureka/,http://eureka3:8761/eureka/
 instance:
  hostname: eureka1
---
spring:
 application:
  name: eureka2
 profiles: eureka2
eureka:
 client:
  service-url:
   defaultZone: http://eureka1:8761/eureka/,http://eureka3:8761/eureka/
 instance:
  hostname: eureka2
---
spring:
 application:
  name: eureka3
 profiles: eureka3
eureka:
 client:
  service-url:
   defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/
 instance:
  hostname: eureka3

eureka.client.service-url.defaultZone 与 eureka.instance.hostsname eureka1、eureka2、eureka3  都再本机的 hosts 文件里事先写好,

例如 eureka1 服务器上的hosts文件
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   eureka1
xxx.xxx.x.xx eureka2
xx.xxx.xxx.xx eureka3

启动类同样是添加 @EnableEurekaServer 注意:如果运行 jar 包时需要程序接收命令行参数,一定要再main方法的 SpringApplication.run() 方法 中传入 args 参数

例如:

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

如果不传入的话在命令行输入 java -jar xxx.jar --参数名=参数值 参数将不起作用从而影响你无法选择写好的环境配置

客户端:

依赖跟单机的一样

application.yml 配置

eureka:
 instance:
  prefer-ip-address: true #是否显示ip
  ip-address: 本机ip #填写本机ip地址
 client:
  service-url:
   defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/,http://eureka3:8761/eureka/

启动类注解与单机版一样

客户端注册一台注册中心,同时注册到其他集群中说明成功!

eureka添加 spring-security 权限认证

依赖

compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') 

application.yml 配置

server:
 port: 8761
spring:
 application:
  name: eureka-server
 security:
  user:
   name: zyn
   password: zyn123... 
  #用户名与密码若是不配置,系统会自动生成并打印在控制台日志上
  
eureka:
 client:
  register-with-eureka: false
  fetch-registry: false
 server:
  enable-self-preservation: false
  peer-node-read-timeout-ms: 5000
  
management:
 endpoints:
  web:
   base-path: /
   exposure:
    include: '*'
 endpoint:
  health:
   show-details: always
  restart:
   enabled: true
 server:
  port: 8761
---
spring:
 profiles: eureka-jy
eureka:
 client:
  service-url:
   defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-B:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-C:8761/eureka/
 instance:
  hostname: eureka-A
---
spring:
 profiles: eureka-lhn
eureka:
 client:
  service-url:
   defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-A:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-C:8761/eureka/ 
 instance:
  hostname: eureka-B
---
spring:
 profiles: eureka-zsm
eureka:
 client:
  service-url:
   defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-A:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-B:8761/eureka/
 instance:
  hostname: eureka-C

创建一个类并继承 WebSecurityConfigurerAdapter 类,并在此类上添加 @EnableWebSecurity和@Configuration 注解,重写 configure 方法

package com.iteng.eureka.security;​
import org.springframework.context.annotation.Configuration;
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;
​
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
​
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable(); // 关闭csrf
    http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 开启认证
  }
}

启动类添加注解

@EnableEurekaServer
@EnableWebSecurity

下线服务

http://eureka地址:端口/eureka/apps/服务名/服务地址:服务端口号

实例如下:

erueka注册中心ip: 10.100.1.100

端口: 12000

服务名: CPS-RISK-SERVER

实例id: 192.168.10.54:18883

则向下面的url通过http发送delete命令,可将指定的服务实例删除:
http://10.100.1.100:12000/eureka/apps/CPS-RISK-SERVER/192.168.10.54:18883

变更服务状态

http://eureka地址:端口/eureka/apps/服务名/服务地址/status?value=${value}

其中${value}的取值为:OUT_OF_SERVICE , DOWN , UP

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 深入浅出JWT
    JWT(JSONWEBTOKEN)的组成https:jwt.ioheader(头部)承载两部分信息:声明 ... [详细]
  • 前端跨域访问后端数据的方法
    参考链接:https:mp.weixin.qq.coms4G_27oRLSMMYBFvtYZgqcg一、什么是跨域当两个域名的协议、子域名、主域名、端口号中有任意一个不 ... [详细]
  • HDIV简介一个简单又强大的安全框架
    为什么80%的码农都做不了架构师?惯例官方纯英文档:https:hdivsecurity.comtechnical-documentationdo ... [详细]
  • 在Kubernetes上部署JupyterHub的步骤和实验依赖
    本文介绍了在Kubernetes上部署JupyterHub的步骤和实验所需的依赖,包括安装Docker和K8s,使用kubeadm进行安装,以及更新下载的镜像等。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 禁止程序接收鼠标事件的工具_VNC Viewer for Mac(远程桌面工具)免费版
    VNCViewerforMac是一款运行在Mac平台上的远程桌面工具,vncviewermac版可以帮助您使用Mac的键盘和鼠标来控制远程计算机,操作简 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • CSRF校验策略及装饰器和auth认证模块
    目录csrf跨站请求伪造csrf校验策略csrf相关装饰器auth认证模块auth认证相关模块及操作扩展auth_user表csrf跨站请求伪造钓鱼网站:模仿一个正规的网站让用户在 ... [详细]
  • laravel怎么关闭csrf验证
    php框架|Laravellaravelphp框架-Laravel在线生成php源码,vscode怎么解除注释,ubuntu进入后,tomcat放在哪了,爬虫美图,php打包工具, ... [详细]
  • pikachu漏洞练习平台之暴力破解学习
    1.基于表单的暴力破解没有验证码,可以直接跑字典进行破解,burp直接抓包添加变量正确密码admin---1234562.验证码绕过(onserver ... [详细]
  • 这座城市多了十只伤心的鸽
    这个作业属于哪个课程2021春软件工程实践|W班(福州大学)这个作业要求在哪里团队第四次作业这个作业的目标设计项目原型、制作项目需求规格说明书团队名称这座城市多了十只伤心的鸽其他参 ... [详细]
  • 如何防止模拟的http的恶意请求?
    http:www.dewen.ioq5511我有一串URLwww.abc.com?paraxxx在页面中点击按钮后用ajax执行此URL后,后台会执行一些操作 ... [详细]
author-avatar
mobiledu2502856097
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有