nodejs的Eureka Client开源实现
服务发现组件具备功能:
服务注册表
服务注册与服务发现
服务检查
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
server:
port: 8761
eureka:
client:
registerWithEureka: false # 是否将自己注册到Eureka Server
fetch-registry: false # 是否从Eureka Server获取注册信息
service-url:
defaultZone: http://localhost:8761/eureka/ # 设置与Eureka Server交互的地址,多个地址用,分隔
@SpringBootApplication
@EnableEurekaServer // 声明这是一个Eureka Server
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
http://localhost:8761/
注意添加版本号,否则会下载不下来
<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 # 用于指定注册到Eureka Server上的应用名称
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true # 表示将自己的IP注册到Eureka Server
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient // 声明这是一个Eureka Client
防止因Eureka Server宕机导致微服务不可用
通过运行多个实例并相互注册的方式实现高可用部署,实例间彼此增量地同步信息,确保所有节点数据一致。
spring:
application:
name: microservice-discovery-eureka
---
spring:
config:
activate:
on-profile: peer1 # 指定profile=peer1
server:
port: 8761
eureka:
instance:
hostname: peer1 # 指定当profile=peer1时,主机名是peer1
client:
serviceUrl:
defaultZone: http://peer2:8762/eureka/ # 将自己注册到peer2这个Eureka上面去
---
spring:
config:
activate:
on-profile: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/
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
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
instance:
prefer-ip-address: true # 表示将自己的IP注册到Eureka Server
前面的示例均可以匿名访问,可以通过spring-security先登录之后在访问
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
server:
port: 8761
eureka:
client:
registerWithEureka: false # 是否将自己注册到Eureka Server
fetch-registry: false # 是否从Eureka Server获取注册信息
service-url:
defaultZone: http://user:password123@localhost:8761/eureka/
spring:
security:
user:
name: user # 配置登录的账号
password: password123 # 配置登录的密码
未设置,client注册会报Cannot execute request on any known server
/**
* 高版本的丢弃了
*
* security:
* basic:
* enabled: true
* 配置,应该使用以下方式开启
* @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();
}
}
仅需修改注册地址即可,注意和server保持一致
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka/ # 需要这种格式 http://user:password@EUREKA_HOST:EUREKA_PORT/eureka/
https://blog.csdn.net/fengzelun/article/details/117718784
https://juejin.cn/post/6995434651862958087