Springboot-2.0.2.RELEASEユーレカ認定、登録サービスの失敗の後。
近年のように、マイクロサービスアーキテクチャとドッカーコンテナの人気の概念、それは特に、現在の多種多様に分布するソリューションを提供して、将来的には春の雲中立地のソフトウェア開発スタイルのより多くの「クラウド」を作りますServlet仕様を意味する標準化され、フルストップ技術的な解決策は、効果的にサーバソフトウェアシステムの技術レベルの進歩を促進、今年の誕生に匹敵するかもしれません。
SpringCloudユーレカSpringCloud Netflixのサービスは、Netflixのユーレカが第二のパッケージを作っ基づいているスイートの一部であり、サービス管理機能マイクロサービスアーキテクチャの完了の責任です。高可用性を実現し、Laijiangjiangユーレカは今日構築するには
<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>
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です。
ユーザー名とパスワードを確認します:管理者: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
ローカルそれを実行します。実行コンフィギュレーションを
分别启动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
后面的脚本我自己没验证,我也不怎么会写脚本,如果那个大神提供更好的脚本,小编感激不尽