文章目錄
環境版本
SpringCloud:Hoxton
SpringBoot:2.2.1
開發工具:idea
案例源碼:
1. Eureka簡介
Eureka是一個基於Rest服務的服務註冊與發現框架。它被集成在SpringCloud子項目spring-cloud-netflix中,以支持SpringCloud的服務發現功能。
2. Eureka原理
略
3. Eureka註冊中心
3.1 Eureka 組件
Eureka包含兩個組件:
- Eureka Server
- Eureka Client
3.2 單機版 Eureka註冊中心(Eureka Server)
示例源碼地址:https://github.com/1336037686/springcloud-demo/tree/master/Eureka/springcloud-eureka-simple
3.2.1 項目結構
3.2.2 pom.xml 依賴
引入Spring Cloud Eureka Server依賴
完整pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lgx</groupId>
<artifactId>springcloud-eureka-simple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-eureka-simple</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2.3 修改啓動類
在啓動類上添加@EnableEurekaServer
註解。開啓Eureka Server 服務
package com.lgx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudEurekaSimpleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaSimpleApplication.class, args);
}
}
3.2.4 修改配置
application.yml
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: false
service-url:
dafaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
上述配置中
配置名 | 作用 |
---|---|
spring.application.name |
設置服務名 |
server.port |
服務端口 |
eureka.instance.hostname |
設置主機名 |
eureka.client.register-with-eureka |
是否將此實例註冊到註冊中心 |
eureka.client.fetch-registry |
是否從註冊中心獲取註冊信息 |
eureka.client.service-url.dafaultZone |
設置註冊地址,多個地址用, 隔開 |
3.2.5 測試
訪問:http://localhost:8761
3.3 高可用 Eureka註冊中心集羣(Eureka Server)
註冊中心作爲微服務架構中的核心功能,其重要性不言而喻。所以單機版的Eureka Server在可靠性上並不符合現在的互聯網開發環境。集羣版的Eureka Server纔是商業開發中的選擇 [1]。
Eureka通過相互註冊的方式來使註冊中心變得更有彈性和可用性,實現集羣高可用。
示例源碼地址:https://github.com/1336037686/springcloud-demo/tree/master/Eureka/springcloud-eureka-ha
3.3.1 項目結構
父工程pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lgx</groupId>
<artifactId>springcloud-eureka-ha</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-server1</module>
<module>eureka-server2</module>
</modules>
</project>
3.3.2 eureka-server1
3.3.2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lgx</groupId>
<artifactId>springcloud-eureka-ha</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.lgx</groupId>
<artifactId>eureka-server1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.3.2.2 修改啓動類
package com.lgx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer //開啓Eureka Server服務
@SpringBootApplication
public class EurekaServer1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer1Application.class, args);
}
}
3.3.2.3 修改配置文件
具體服務設置
模塊名稱 | 服務名稱 | 主機名 | 端口 |
---|---|---|---|
eureka-server1 | eureka-server1 | localhost | 8761 |
eureka-server2 | eureka-server2 | localhost | 8762 |
application.yml
spring:
application:
name: eureka-server1
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
dafaultZone: http://localhost:8762/eureka/
eureka.client.service-url.dafaultZone
:設置註冊地址填寫其他註冊中心的路徑,多個註冊中心之間使用,
分隔
eureka.client.register-with-eureka
:設置將此實例註冊到註冊中心
eureka.client.fetch-registry
:設置從註冊中心獲取註冊信息
3.3.3 eureka-server2
3.3.3.1 pom.xml
與3.3.2.1 相同
3.3.3.2 修改啓動類
同3.3.2.2
3.3.3.3 修改配置文件
application.yml
spring:
application:
name: eureka-server2
server:
port: 8762
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
dafaultZone: http://localhost:8761/eureka/
3.3.4 測試
eureka-server1啓動時一開始會先報錯,這是因爲eureka-server2暫未啓動,eureka-server1註冊請求被拒絕。當所有的服務都啓動後:
訪問http://localhost:8761/eureka
訪問http://localhost:8762/eureka
可以看到兩個服務註冊到了
4. 微服務開發案例
在3.3 的基礎上,我們模擬一個使用Eureka作爲註冊中心,服務提供者(app-server)向服務消費者(app-client)提供服務。
4.0 Demo結構
具體功能:
- 服務提供者向服務消費者提供一個打印
Hello World
的服務hello
- 服務消費者獲取服務提供者的服務,打印
Hello World
具體服務設置
服務名 | 端口 |
---|---|
app-server | 8080 |
app-client | 8081 |
父工程pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lgx</groupId>
<artifactId>springcloud-eureka-app</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-server1</module>
<module>eureka-server2</module>
<module>app-server</module>
<module>app-client</module>
</modules>
</project>
4.1 服務提供者
服務提供者向服務消費者提供一個打印Hello World
的服務hello
4.1.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lgx</groupId>
<artifactId>springcloud-eureka-app</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.lgx</groupId>
<artifactId>app-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
不論是服務提供者還是服務消費者都需要註冊到Eureka註冊中心才能獲取到所需的服務,所以我們需要引入Eureka Client依賴:
4.1.2 修改啓動類
package com.lgx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class AppServerApplication {
public static void main(String[] args) {
SpringApplication.run(AppServerApplication.class, args);
}
}
使用@EnableDiscoveryClient
註解將一個微服務註冊到Eureka Server。
4.1.3 修改配置文件
application.yml
spring:
application:
name: app-server
server:
port: 8080
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
dafaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
4.1.4 編寫基礎業務
提供·Hello World·接口
package com.lgx.comntroller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author LGX_TvT
* @date 2019-12-06 12:53
*/
@RestController
public class HelloServiceController {
@RequestMapping("/hello")
public String doHello(){
return "Hello World";
}
}
4.2 服務消費者
服務消費者獲取服務提供者的服務,打印Hello World
4.2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lgx</groupId>
<artifactId>springcloud-eureka-app</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.lgx</groupId>
<artifactId>app-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2.2 修改啓動類
同上
4.2.3 修改配置文件
application.yml
spring:
application:
name: app-client
server:
port: 8081
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
dafaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
4.2.4 編寫業務代碼
package com.lgx.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @author LGX_TvT
* @date 2019-12-06 12:58
*/
@RestController
public class HelloController {
@Autowired
private EurekaDiscoveryClient eurekaDiscoveryClient;
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/hello")
public String doHello(){
//獲取在註冊中心中註冊的名稱爲app-server的服務實例
List<ServiceInstance> instances = eurekaDiscoveryClient.getInstances("app-server");
//獲取第1個實例
ServiceInstance serviceInstance = instances.get(0);
//拼接URL路徑
String baseURL = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hello";
//使用RestTemplate做Rest服務訪問,獲取返回結果
return restTemplate.getForObject(baseURL, String.class);
}
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
4.3 測試
啓動各個工程
訪問Eureka Server
可以看到當前兩個服務都已經註冊成功
訪問http://localhost:8081/hello
成功打印Hello World,可以看出當前app-client
成功獲取到app-server
提供的/hello
服務,到此我們就實現了一個簡單的微服務。
5. Eureka Server 安全認證
在上面幾個示例中,我們使用Eureka Server作爲Spring Cloud中的服務註冊中心。但是我們在訪問Eureka Server的時候並不需要任何的驗證信息,可以看出其安全性是很低的。所以Spring Cloud中也有爲Eureka Server提供安全認證的方式。可以使用spring-boot-starter-security組件來爲Eureka Server增加安全認證 [1]。
5.1 項目結構
父工程pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lgx</groupId>
<artifactId>springcloud-eureka-security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-eureka-security</name>
<description>Demo project for Spring Boot</description>
<modules>
<module>eureka-server1</module>
<module>eureka-server2</module>
</modules>
</project>
5.2 pom.xml
eureka-server1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lgx</groupId>
<artifactId>springcloud-eureka-security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.lgx</groupId>
<artifactId>eureka-server1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
eureka-server2的pom.xml同上
從pom.xml文件可以看出我們添加了spring-seucrity做安全認證
5.3 設置啓動類
eureka-server1,eureka-server2
package com.lgx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@EnableWebSecurity
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer1Application.class, args);
}
}
在啓動類上分別加上@EnableWebSecurity
,@EnableEurekaServer
註解開啓安全認證,和Eureka Server服務
5.4 編寫security配置類
在eureka-server1,eureka-server2分別編寫裝配security配置類。由於新版本的security默認開啓csrf了,需要將其關閉。同時開啓基於http basic的安全認證。
package com.lgx;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
/**
* 開啓基於http basic的安全認證
* @author LGX_TvT
* @date 2019-12-05 17:17
*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Configure HttpSecurity as needed (e.g. enable http basic).
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable();
//注意:爲了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,
// 如果是form方式,不能使用url格式登錄
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
5.5 修改配置類
eureka-server1 application.yml
spring:
application:
name: eureka-server1
# 設置登陸用戶名,密碼
security:
user:
name: root
password: root
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
# 使用http://${user}:${password}@${host}:${port}/eureka/進行驗證登錄
dafaultZone: http://root:root@localhost:8762/eureka/
eureka-server2 application.yml
spring:
application:
name: eureka-server2
security:
user:
name: root
password: root
server:
port: 8762
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
dafaultZone: http://root:root@localhost:8761/eureka/
5.6 修改配置文件
eureka-server1 application.yml
spring:
application:
name: eureka-server1 # 設置spring應用命名,可以自定義,非必要spring:
security:
user:
name: root # 設置安全認證用戶名
password: root # 設置安全認證密碼
server:
port: 8761 # 設置Eureka Server WEB控制檯端口,自定義
eureka:
instance:
hostname: 127.0.0.1 # 設置eureka實例名稱,建議與配置文件的變量相同,必須和Linux系統域名相同
client:
# register-with-eureka: false # 是否將自己註冊到Eureka-Server中,默認的爲true
# fetch-registry: false # 是否從Eureka-Server中獲取服務註冊信息,默認爲true
service-url: # 設置服務註冊中心地址,指向另一個註冊中心,使用域名作爲訪問路徑
defaultZone: http://root:root@${eureka.instance.hostname}:8762/eureka/
eureka-server2 application.yml
spring:
application:
name: eureka-server2 # 設置spring應用命名,可以自定義,非必要spring:
security:
user:
name: root # 設置安全認證用戶名
password: root # 設置安全認證密碼
server:
port: 8762 # 設置Eureka Server WEB控制檯端口,自定義
eureka:
instance:
hostname: 127.0.0.1 # 設置eureka實例名稱,建議與配置文件的變量相同,必須和Linux系統域名相同
client:
# register-with-eureka: false # 是否將自己註冊到Eureka-Server中,默認的爲true
# fetch-registry: false # 是否從Eureka-Server中獲取服務註冊信息,默認爲true
service-url: # 設置服務註冊中心地址,指向另一個註冊中心,使用域名作爲訪問路徑
defaultZone: http://root:root@${eureka.instance.hostname}:8761/eureka/
5.7 測試
我們啓動兩個服務進行測試,訪問http://localhost:8761
輸入之前設置的用戶名,密碼[root, root]
6. Eureka 服務保護
7. Eureka 配置
8. 參考資料
[1] 優創課堂架構課程