2019独角兽企业重金招聘Python工程师标准>>>
Springboot-2.0.2.RELEASE Eureka认证后,服务注册失败问题。
随着近几年微服务架构和Docker容器概念的火爆,也会让Spring Cloud在未来越来越“云”化的软件开发风格中立有一席之地,尤其是在目前五花八门的分布式解决方案中提供了标准化的、全站式的技术方案,意义可能会堪比当年Servlet规范的诞生,有效推进服务端软件系统技术水平的进步。
SpringCloud Eureka是SpringCloud Netflix服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。今天就来讲讲Eureka的高可用实现与搭建
MAVEN相关配置
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
Finchley.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-security
如果找不到包版本配置文件中加
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
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();
}
}
完整的代码如下:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
完美的结果
再次启动三个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
后面的脚本我自己没验证,我也不怎么会写脚本,如果那个大神提供更好的脚本,小编感激不尽
源码地址:https://gitee.com/bianxin.com/earn_knife/tree/master/eureka
欢迎加入技术讨论群:340697945