Eureka搭建

搭建注册中心

pom

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<!--        非必须,用来加载bootstrap.yml,这部分的内容也可以直接写在applicaition.yml中       -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

application.yml

spring:
  application:
    name: eureka

bootstrap.yml

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

启动类添加@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}

查看注册中心

启动项目,访问http://127.0.0.1:8761/

业务服务注册

pom

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

bootstrap.yml

server:
  port: 8090
eureka:
  instance:
    # 以IP注册到注册中心
    preferIpAddress: ${EUREKA_INSTANCE_PREFER_IP_ADDRESS:true}
    # 续约间隔时间,默认30秒
    leaseRenewalIntervalInSeconds: ${EUREKA_INSTANCE_LEASE_RENEWAL_INTERVAL_IN_SECONDS:30}
    # 实例故障摘除时间,默认90秒,实际是90*2=180秒,配置45就是90秒
    leaseExpirationDurationInSeconds: ${EUREKA_INSTANCE_LEASE_EXPIRATION_DURATION_IN_SECONDS:45}
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka
    registryFetchIntervalSeconds: 30

启动类添加@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

    public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
    }

}

查看注册中心

启动项目,访问http://127.0.0.1:8761/,业务服务已经注册上了

Eureka添加认证

pom添加Security依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.yml中配置Security相关参数

spring:
  security:
    user:
      name: root
      password: 123456

处理CSRF

package com.learn.eureka.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author PC
 */
@Configuration
public class RegisterConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();
        return http.build();
    }
}

查看注册中心

重启项目,访问http://127.0.0.1:8761/,可以看到需要认证

登录后查看注册中心,会发现业务服务已经下线,这是因为业务服务也需要认证

业务服务添加认证

bootstrap.yml调整defaultZone

eureka:
  instance:
    # 以IP注册到注册中心
    preferIpAddress: ${EUREKA_INSTANCE_PREFER_IP_ADDRESS:true}
    # 续约间隔时间,默认30秒
    leaseRenewalIntervalInSeconds: ${EUREKA_INSTANCE_LEASE_RENEWAL_INTERVAL_IN_SECONDS:30}
    # 实例故障摘除时间,默认90秒,实际是90*2=180秒,配置45就是90秒
    leaseExpirationDurationInSeconds: ${EUREKA_INSTANCE_LEASE_EXPIRATION_DURATION_IN_SECONDS:45}
  client:
    service-url:
      defaultZone: http://root:123456@127.0.0.1:8761/eureka
    registryFetchIntervalSeconds: 30

查看注册中心

重启项目,查看注册中心

参考资料

[1].eureka项目

[2].demo项目

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值