1. 程式人生 > >初始化Spring Cloud建立Eureka服務註冊中心

初始化Spring Cloud建立Eureka服務註冊中心

bootstrap https 控制臺 depend lee 心跳 git reg iyu

1、新建項目

https://gitee.com/frankawp/vishnu 新建一個git項目

打開git bash

git clone https://gitee.com/frankawp/vishnu

git remote add vishnu https://gitee.com/frankawp/vishnu

touch test

git add .

git commit -m initial

git push origin master

初始化git完畢

刷新https://gitee.com/frankawp/vishnu 多了一個test文件

2、新建一個spring cloud的服務註冊中心

登錄https://start.spring.io/

先建一個註冊中心

Group: cn.battlecruiser.vishnu

Artifact: vishnu-eureka

Search for dependencies :Eureka Server

Generate project

下載下來後,解開,將vishnu-eureka 放在vishnu目錄下

為加快maven下載速度,加aliyun的maven倉庫,maven settings.xml裏

<mirror>

<id>nexus-aliyun</id>

<mirrorOf>central</mirrorOf>

<name>Nexus aliyun</name>

<url>https://maven.aliyun.com/nexus/content/groups/public</url>

</mirror>

spring.io上初始化的工程用的是application.properties . 刪掉好了,用bootstrap.yml內容如下

server:

port: 1025

spring:

application:

name: vishnu-eureka-server

eureka:

client:

fetch-registry: false

register-with-eureka: false

serviceUrl:

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

instance:

hostname: localhost

server: #配置屬性,但由於 Eureka 自我保護模式以及心跳周期長的原因,經常會遇到 Eureka Server 不剔除已關停的節點的問題

enable-self-preservation: false

eviction-interval-timer-in-ms: 5000

測試一下:

cd vishnu-eureka

mvn clean package

mvn spring-boot:run

啟動成功

http://localhost:1025/actuator 可以訪問

但是登錄不了主頁 http://localhost:1025

main方法上沒有@EnableEurekaServer註解 重啟 可以了,不知道它的默認工程為什麽沒加這個。

3、加安全

pom.xml 加

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-security</artifactId>

</dependency>

啟動後控制臺會打印一個隨機密碼

Using generated security password: eaeed05e-a0ea-42aa-81d5-e27b485ba6ef

用戶名是 : user

如果要自定義

bootstrap.yml中加

security:

basic:

enabled: true # 啟用身份認證

user:

name: frank # 定義用戶名

password: frank123 # 定義密碼

4、git push

git add .

git commit -m eureka-init

git push

初始化Spring Cloud建立Eureka服務註冊中心