服務(wù)發(fā)現(xiàn)(注冊(cè))機(jī)制
nodejs的Eureka Client開源實(shí)現(xiàn)
服務(wù)發(fā)現(xiàn)組件具備功能:
服務(wù)注冊(cè)表
服務(wù)注冊(cè)與服務(wù)發(fā)現(xiàn)
服務(wù)檢查
Eureka架構(gòu)圖
Eureka使用
引用
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
配置
server:
port: 8761
eureka:
client:
registerWithEureka: false # 是否將自己注冊(cè)到Eureka Server
fetch-registry: false # 是否從Eureka Server獲取注冊(cè)信息
service-url:
defaultZone: http://localhost:8761/eureka/ # 設(shè)置與Eureka Server交互的地址,多個(gè)地址用,分隔
啟動(dòng)類標(biāo)記
@SpringBootApplication
@EnableEurekaServer // 聲明這是一個(gè)Eureka Server
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
訪問(wèn)
http://localhost:8761/
微服務(wù)注冊(cè)
微服務(wù)工程添加引用
注意添加版本號(hào),否則會(huì)下載不下來(lái)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
增加配置
spring:
application:
name: microservice-provider-user # 用于指定注冊(cè)到Eureka Server上的應(yīng)用名稱
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true # 表示將自己的IP注冊(cè)到Eureka Server
啟動(dòng)類增加注解
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient // 聲明這是一個(gè)Eureka Client
啟動(dòng)服務(wù)注冊(cè)
Eureka Server集群部署
防止因Eureka Server宕機(jī)導(dǎo)致微服務(wù)不可用
通過(guò)運(yùn)行多個(gè)實(shí)例并相互注冊(cè)的方式實(shí)現(xiàn)高可用部署,實(shí)例間彼此增量地同步信息,確保所有節(jié)點(diǎn)數(shù)據(jù)一致。
修改配置文件
spring:
application:
name: microservice-discovery-eureka
---
spring:
config:
activate:
on-profile: peer1 # 指定profile=peer1
server:
port: 8761
eureka:
instance:
hostname: peer1 # 指定當(dāng)profile=peer1時(shí),主機(jī)名是peer1
client:
serviceUrl:
defaultZone: http://peer2:8762/eureka/ # 將自己注冊(cè)到peer2這個(gè)Eureka上面去
---
spring:
config:
activate:
on-profile: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/
啟動(dòng)多個(gè)eureka實(shí)例
java -jar microservice-discovery-eureka-3.0.2.jar --spring.profiles.active=peer1
java -jar microservice-discovery-eureka-3.0.2.jar --spring.profiles.active=peer2
微服務(wù)注冊(cè)到多個(gè)eureka實(shí)例
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
instance:
prefer-ip-address: true # 表示將自己的IP注冊(cè)到Eureka Server
為Eureka Server添加用戶認(rèn)證
前面的示例均可以匿名訪問(wèn),可以通過(guò)spring-security先登錄之后在訪問(wèn)
引入spring-security
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
配置
server:
port: 8761
eureka:
client:
registerWithEureka: false # 是否將自己注冊(cè)到Eureka Server
fetch-registry: false # 是否從Eureka Server獲取注冊(cè)信息
service-url:
defaultZone: http://user:password123@localhost:8761/eureka/
spring:
security:
user:
name: user # 配置登錄的賬號(hào)
password: password123 # 配置登錄的密碼
關(guān)閉security的csrf,否則client無(wú)法注冊(cè)
未設(shè)置,client注冊(cè)會(huì)報(bào)Cannot execute request on any known server
/**
* 高版本的丟棄了
*
* security:
* basic:
* enabled: true
* 配置,應(yīng)該使用以下方式開啟
* @param http
* @throws Exception
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable();
//注意:為了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,
// 如果是form方式,不能使用url格式登錄
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
return http.build();
}
}
client注冊(cè)
僅需修改注冊(cè)地址即可,注意和server保持一致
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka/ # 需要這種格式 http://user:password@EUREKA_HOST:EUREKA_PORT/eureka/
Eureka自我保護(hù)模式
https://blog.csdn.net/fengzelun/article/details/117718784文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-767479.html
常見問(wèn)題
1、Cannot execute request on any known server
https://juejin.cn/post/6995434651862958087文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-767479.html
到了這里,關(guān)于微服務(wù)注冊(cè)與發(fā)現(xiàn)——Eureka的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!