springCloud Euraka

由于spingcloud  版本更新比较快,此处重新整理一版:

版本:  Java 8 

   spring boot  <version>  2.1.15.RELEASE </version>

   <spring-cloud.version>Greenwich.SR6</spring-cloud.version>

1.Euraka 的使用

  1.1服务端:    概述: Eureka server  ,健康检测,  安全认证

依赖:

        <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-security</artifactId> </dependency>
     <!--健康检测 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

主类:

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServceApplication.class, args);
    }
}

配置文件: application.yml

server:
  port: 8761
#需要导入spring-boot-starter-security
#为Euraka server 添加一个认证,当访问localhost:8761会要求验证
management:
  endpoint:
    health: #健康检测 查看 http://localhost:8761/actuator/health
      show-details: always
spring:
  application:
    name: micro-discovery-eureka
  security: #安全配置
    basic:
     enabled: true
    user:
      name: root
      password: root
eureka:
  client:
    service-url:
      #erueka server的地址,/eureka
      defaultZone: http://root:[email protected]:8761/eureka
    # 是否从eureka server注册,这里我们选择false
    fetch-registry: false
    # 是否从eureka server 中拉取服务
    register-with-eureka: false

添加配置类: springcloud升级到2.x后Eureka安全配置与1.x有部分变动,新版本的security默认开启csrf了,这里我们先关掉它,否则Eureka 服务端注册不上

新建一个配置类:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭csrf
        http.csrf().disable();
        //开启认证
         http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

客户端:

依赖:

       <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-actuator</artifactId>
</dependency>

配置文件:application.yml

server:
  port: 9001
spring:
  application:
    name: micro-clent1_user
management:
  endpoint:
    health: #健康检测 查看 http://localhost:8761/actuator/health
      show-details: always
eureka:
  client:
    service-url:
      defaultZone: http://root:[email protected]:8761/eureka/
  instance:
   # 是否显示ip,如果不设置那么就会显示主机名,默认false
    prefer-ip-address: true

主类:

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServceApplication.class, args);
    }
}

登录 http://localhost:8761

猜你喜欢

转载自www.cnblogs.com/lshan/p/13163379.html