eureka服務端配置 請點擊 Eureka +security 服務端 配置、調用
Eureka 服務器集群
修改服務域名
在搭建前需要修改 hosts文件,文件地址如下

hosts位置.jpg
編輯改文件 添加 127.0.0.1 peer1,peer2 如下
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 peer1 peer2
注意: #符號 后面的都是被注解掉的代碼
創(chuàng)建eureka 集群服務器
第一步 創(chuàng)建spring boot 項目,項目起名為 microservice-discovery-eureka ,修改 pom文件 添加 如下引入
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
......
其他引入包
......
<!-- eureka server 支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- spring security 支持包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<!-- Spring cloud 管理引入 -->
<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配置文件 ->原文件 后綴為 properties 請手動修改為 yml 因本例使用的是 yml
# 配置服務名稱(因為這里作為服務中心集群,所以將自己作為服務向其它服務中心注冊自己,形成互相注冊的服務注冊中心)
spring:
application:
name: microservice-discovery-eureka
security:#spring security 的登錄名和密碼設置
user:
name: user
password: 123456
# 配置端口
server:
port: 8761
eureka:
# 配置本注冊中心的hostname
instance:
hostname: peer1
# 本Eureka服務端將自己作為服務將要注冊的注冊中心的地址
client:
#表示是否將自己注冊到Eureka Server,默認為true。由于當前應用就是Eureka server,故而設置為false。
register-with-eureka: true
#表示是否從Eureka server 獲取注冊信息,默認為true。
#因為這是一個單節(jié)點的Eureka Server ,不需要同步其他的 Eureka Server 節(jié)點的數據,故而設置為false
fetch-registry: true
serviceUrl:
#將域名注冊至 集群上 三個服務或更多服務集群的時候 地址為
#http://user:123456@peer2:8762/eureka/,http://user:123456@peer3:8763/eureka/, ...
# user:123456 對應的是 peer2:8762 配置的 spring security 的 訪問時的用戶名和 密碼 使用@連接url
defaultZone: http://user:123456@peer2:8762/eureka/
第三步 創(chuàng)建 SecurityConfig 文件 代碼如下
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 SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//spring security csrf (跨域保護)關閉
http.csrf().disable();
super.configure(http);
}
}
第四步 修改 Application 文件 啟用 eureka server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //啟用 eureka server
public class MicroserviceDiscoveryEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
}
}
第五步 啟動服務器
啟動后會看到如下錯誤

控制臺錯誤.png
不要擔心 服務現在是好使的 直接訪問 ** http://peer1:8761 **可以看到 如下頁面 輸入配置文件中 的用戶名和密碼進入

spring security登錄頁面.jpg
進入后頁面如下

eureka主頁(第二服務器啟動前).jpg
以上單個eureka 服務就算完成了 現在搭建第二個 eureka服務器實現 服務器集群 不要停止服務執(zhí)行以下操作
重新創(chuàng)建一個項目起名 為 microservice-discovery-eureka-ha ,
重復上述 第一步操作
第二步操作pom文件修改為:
# 配置服務名稱(因為這里作為服務中心集群,所以將自己作為服務向其它服務中心注冊自己,形成互相注冊的服務注冊中心)
spring:
application:
name: microservice-discovery-eureka-ha
security:
user:
name: user
password: 123456
# 配置端口
server:
port: 8762
eureka:
# 配置本注冊中心的hostname
instance:
hostname: peer2
# 本Eureka服務端將自己作為服務將要注冊的注冊中心的地址
client:
register-with-eureka: true
#表示是否從Eureka server 獲取注冊信息,默認為true。因為這是一個單節(jié)點的Eureka Server ,不需要同步其他的 Eureka Server 節(jié)點的數據,故而設置為false
fetch-registry: true
serviceUrl:
defaultZone: http://user:123456@peer1:8761/eureka/
重復上述第三步與第四步操作
然后啟動 服務器 啟動后可以看到控制臺打印如下

集群后控制臺.jpg
切換至 eureka 項目控制臺可以看到類似上方圖片的信息 只是地址不同
刷新頁面可以看到 多出了兩條數據 如下圖

第二服務器啟動后.jpg