Eureka注册中心安全认证

pom文件

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

EurekaApplication.java

@EnableEurekaServer     //启用服务器的配置中心
@SpringBootApplication
public class EurekaApplication {
    
    
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
    
}

在全局配置文件中,开启基于http basic的安全认证。
application.yml

spring:
  application: 
    name: eureka-server
  profiles:   ## application-{}.yml
    active: $spring.profiles.active$   ## linux 
     active:dev   ## window

application-dev.yml

server:
  port: 8001
    
eureka: 
  instance:
    hostname: eureka1
    lease-renewal-interval-in-seconds: 10   ##客户端向服务器(注册中心发送心跳的时间间隔)
    lease-expiration-duration-in-seconds: 120 ##服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
  client:
    service-url:
 ##defaultZone: http://eureka2:8002/eureka/  ##互相注册到对方--高可用服务发现
       defaultZone: http://test:123456@eureka2:8002/eureka/
security:  ## 开启基于http basic的安全认证
  basic:
    enabled: true
  user:
    name: test
    password: 123456 

springcloud-eureka-b
pom.文件

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

EurekaApplication.java

@EnableEurekaServer     //启用服务器的配置中心
@SpringBootApplication
public class EurekaApplication {
    
    
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
    
}

application.yml

spring:
  application: 
    name: eureka-server
  profiles:   ## application-{}.yml
    active: $spring.profiles.active$   ## linux 
    active:dev   ## window

application-dev.yml

server:
  port: 8002
    
eureka: 
  instance:
    hostname: eureka2
    lease-renewal-interval-in-seconds: 10   ##客户端向服务器(注册中心发送心跳的时间间隔)
    lease-expiration-duration-in-seconds: 120 ##服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
  client:
    service-url:
          ## defaultZone: http://eureka1:8001/eureka/
      defaultZone: http://test:123456@eureka1:8001/eureka/
security:  ## 开启基于http basic的安全认证
  basic:
    enabled: true
  user:
    name: test
    password: 123456

window 启动 略。。。
linux 启动
可以在Linux终端中,通过java命令来启动Eureka Server。在启动的时候,可以通过启动参数来设置有效的配置环境。具体命令如下:
java -jar -Dspring.profiles.active=eurekaserver1 spring-cloud-eureka-server-cluster-1.0.jar
其中-Dspring.profiles.active启动参数,用于定义本次启动的Eureka Server应用的有效全局配置命名,也就是全局配置文件的后缀。SpringBOOT在启动的时候,会根据启动参数来决定读取的有效全局配置文件是哪一个。

效果图:


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容