SpringCloud2.0 Eureka 集群 高可用的认证服务实现与搭建


声明:本文转载自https://my.oschina.net/bianxin/blog/1819947,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

        随着近几年微服务架构和Docker容器概念的火爆,也会让Spring Cloud在未来越来越“云”化的软件开发风格中立有一席之地,尤其是在目前五花八门的分布式解决方案中提供了标准化的、全站式的技术方案,意义可能会堪比当年Servlet规范的诞生,有效推进服务端软件系统技术水平的进步。

        SpringCloud Eureka是SpringCloud Netflix服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。今天就来讲讲Eureka的高可用实现与搭建

MAVEN相关配置

<parent> 	<groupId>org.springframework.boot</groupId> 	<artifactId>spring-boot-starter-parent</artifactId> 	<version>2.0.2.RELEASE</version> 	<relativePath/> <!-- lookup parent from repository --> </parent>  <properties> 	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 	<java.version>1.8</java.version> 	<spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version> </properties>  <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> </dependencies>

application.yml 相关配置

spring:   application:     name: EUREKA --- #注意这里是三个"减号" spring:   profiles: eureka1   security:     user:       name: admin       password: 123123 server:   port: 8001 eureka:   instance:     hostname: eureka1   client:     serviceUrl:       defaultZone: http://admin:123123@eureka2:8002/eureka/,http://admin:123123@eureka3:8003/eureka/     fetch-registry: true     register-with-eureka: true --- spring:   profiles: eureka2   security:     user:       name: admin       password: 123123 server:   port: 8002 eureka:   instance:     hostname: eureka2   client:     serviceUrl:       defaultZone: http://admin:123123@eureka1:8001/eureka/,http://admin:123123@eureka3:8003/eureka/     fetch-registry: true     register-with-eureka: true --- spring:   profiles: eureka3   security:     user:       name: admin       password: 123123 server:   port: 8003 eureka:   instance:     hostname: eureka3   client:     serviceUrl:       defaultZone: http://admin:123123@eureka1:8001/eureka/,http://admin:123123@eureka2:8002/eureka/     fetch-registry: true     register-with-eureka: true

从上面的配置可以看出我们配置了3个Euerka服务,端口号分别是8001和8002与8003。
验证的用户名和密码是:admin:123123

启动类代码

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;  @SpringBootApplication @EnableEurekaServer public class EurekaApplication {  	public static void main(String[] args) { 		SpringApplication.run(EurekaApplication.class, args); 	} }

到这代码就基本完了,本地已经可以运行了。

启动前先在hosts文件添加内容如下:

127.0.0.1 eureka1
127.0.0.1 eureka2
127.0.0.1 eureka3

先本地运行一下:run configurations

分别启动3个配置eureka1,eureka2,eureka3,启动后到浏览器输入:http://eureka1:8001/ 输入你的用户名和密码。

敲黑板: 页面中Instances currently registered with Eureka下面并没得注入的别的服务,各种搜索引擎各种收,没得个所以然,去掉Spring Security后问题解决,可以知道问题是Spring Security引起的,查看源码发现CSRF保护默认是开启的,可以禁用掉即可。

老版本代码

security:      basic:     enabled: true   user:     name: admin     password: 123123

新版本解决方案

添加一个配置类禁用csrf如下:

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 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {     @Override     protected void configure(HttpSecurity http) throws Exception {         http.csrf().disable();     } }

完美的结果

再次启动三个eureka服务,如果一切都正确的话,结果入图下:

在Centos上运行的脚本

启动脚本:

#!/bin/sh #启动服务 APP_NAME=eureka-0.0.1-SNAPSHOT rm -f tpid nohup java -jar /data/apps/eureka/$APP_NAME --spring.profiles.active=eureka1> /data/apps/eureka/eureka1.log nohup java -jar /data/apps/eureka/$APP_NAME --spring.profiles.active=eureka2> /data/apps/eureka/eureka2.log nohup java -jar /data/apps/eureka/$APP_NAME --spring.profiles.active=eureka3> /data/apps/eureka/eureka3.log echo $! > tpid echo Start Success!

停止脚本:

#!/bin/sh #停止服务 APP_NAME=eureka-0.0.1-SNAPSHOT tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then     echo 'Stop Process...'     kill -15 $tpid fi sleep 5 tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then     echo 'Kill Process!'     kill -9 $tpid else     echo 'Stop Success!' fi

后面的脚本我自己没验证,我也不怎么会写脚本,如果那个大神提供更好的脚本,小编感激不尽

本文发表于2018年05月28日 16:00
(c)注:本文转载自https://my.oschina.net/bianxin/blog/1819947,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2644 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1