Springboot-2.0.2.RELEASE Eureka认证后,服务注册失败问题。java
随着近几年微服务架构和Docker容器概念的火爆,也会让Spring Cloud在将来愈来愈“云”化的软件开发风格中立有一席之地,尤为是在目前五花八门的分布式解决方案中提供了标准化的、全站式的技术方案,意义可能会堪比当年Servlet规范的诞生,有效推动服务端软件系统技术水平的进步。git
SpringCloud Eureka是SpringCloud Netflix服务套件中的一部分,它基于Netflix Eureka作了二次封装,主要负责完成微服务架构中的服务治理功能。今天就来说讲Eureka的高可用实现与搭建web
<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>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
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:123123spring
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 eureka3app
分别启动3个配置eureka1,eureka2,eureka3,启动后到浏览器输入:http://eureka1:8001/ 输入你的用户名和密码。分布式
敲黑板: 页面中Instances currently registered with Eureka下面并没得注入的别的服务,各类搜索引擎各类收,没得个因此然,去掉Spring Security后问题解决,能够知道问题是Spring Security引发的,查看源码发现CSRF保护默认是开启的,能够禁用掉便可。ide
security: basic: enabled: true user: name: admin password: 123123
添加一个配置类禁用csrf以下:(可是你会发现,注入服务确不须要密码了,说明失去了验证。)spring-boot
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(); } }
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic(); } }
再次启动三个eureka服务,若是一切都正确的话,结果入图下:
启动脚本:
#!/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
后面的脚本我本身没验证,我也不怎么会写脚本,若是那个大神提供更好的脚本,小编感激涕零
欢迎加入技术讨论群:340697945