时间:2021-05-19
基础依赖
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: deveureka: client: service-url: defaultZone: http://本机ip地址:8761/eureka/ #注册中心地址 register-with-eureka: false #表明该实例是否应该与尤里卡服务器登记其信息被别人发现。在某些情况下,您不希望您的实例被发现而你想发现其他实例。默认为true fetch-registry: false #表明这个客户是否应该从尤里卡服务器获取尤里卡注册表信息。默认为trueserver: 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: 8761eureka: 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: eureka1eureka: client: service-url: defaultZone: http://eureka2:8761/eureka/,http://eureka3:8761/eureka/ instance: hostname: eureka1---spring: application: name: eureka2 profiles: eureka2eureka: client: service-url: defaultZone: http://eureka1:8761/eureka/,http://eureka3:8761/eureka/ instance: hostname: eureka2---spring: application: name: eureka3 profiles: eureka3eureka: client: service-url: defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/ instance: hostname: eureka3eureka.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: 8761spring: 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-jyeureka: 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-lhneureka: 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-zsmeureka: 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@Configurationpublic 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言:spring-cloud为基础的微服务架构,所有的微服务都需要注册到注册中心,如果这个注册中心阻塞或者崩了,那么整个系统都无法继续正常提供服务,所以,这里
说明:ribbon是spring-cloud中作为服务消费者的一种角色,客户端可以通过它来对服务提供者的服务进行消费,比如本例中是服务提供者注册到注册中心,服务
先简单说一下我们工程的架构:前端工程是采用react,后端工程采用spring-cloud,里面分为zuul工程和其他功能模块。zuul工程除了提供后端的路由转
1.官方文档https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.R
文档地址https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-ali