栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微服务常用注解与配置文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微服务常用注解与配置文件

 一、注解 1、@EnableEurekaServer

写在spring启动类上启动Eureka服务

@EnableEurekaServer

二、application.yml配置文件 1、服务端
server:
  port: ${port:8761}
eureka:
  client:
    #Eureka客户端向Eureka服务端进行注册的地址。
    service-url:
      defaultZone: ${defaultZone:http://localhost:8762/eureka}
    #向注册中心注册自己
    register-with-eureka: true
    #从注册中心拉取服务
    fetch-registry: true
  server:
    # 检查失效服务的间隔时间.每隔60秒检测一次,把服务列表中超过90秒没有发送心跳续约服务进行剔除
    eviction-interval-timer-in-ms: 60000
    # 每分钟需要收到的续约次数的阈值,默认值就是:0.85
    renewal-percent-threshold: 0.85
    # 开启自我保护
    enable-self-preservation: true
    # 在获取不到服务时,需要等待的时间,默认5分钟,单位为毫秒,
    wait-time-in-ms-when-sync-empty: 300000

spring:
  application:
    # 向注册中心进行注册的服务名称,不要使用下划线,不然启动会失败
    name: eureka-server
2、客户端 
server:
  port: 9001
spring:
  application:
    name: cinema-portal-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
      # 服务续约时间,每30秒发送一次心跳检测,证明自己还活着
    lease-renewal-interval-in-seconds: 30
      # 服务失效时间,默认90秒
    lease-expiration-duration-in-seconds: 90


三、pom.xml依赖 1、eureka依赖 (1)父项目

    org.springframework.cloud
    spring-cloud-dependencies
    2020.0.4
    pom
    import
(2)子项目

    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

四、安全认证 1、pom.xml依赖

   org.springframework.boot
   spring-boot-starter-security
2、配置类
    @EnableWebSecurity
        public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().ignoringAntMatchers("/eureka/**");
                super.configure(http);
            }
        }
3、application.yml配置文件 (1)服务端
server:
  port: ${port:8761}
eureka:
  client:
    #Eureka客户端向Eureka服务端进行注册的地址。
    service-url:
      defaultZone: ${defaultZone:http://nie:123@localhost:8762/eureka}
    #不向注册中心注册自己
    register-with-eureka: true
    #不从注册中心拉取服务
    fetch-registry: true
  server:
    # 检查失效服务的间隔时间.每隔60秒检测一次,把服务列表中超过90秒没有发送心跳续约服务进行剔除
    eviction-interval-timer-in-ms: 60000
    # 每分钟需要收到的续约次数的阈值,默认值就是:0.85
    renewal-percent-threshold: 0.85
    # 开启自我保护
    enable-self-preservation: true
    # 在获取不到服务时,需要等待的时间,默认5分钟,单位为毫秒,
    wait-time-in-ms-when-sync-empty: 300000

spring:
  application:
    # 向注册中心进行注册的服务名称,不要使用下划线,不然启动会失败
    name: eureka-server
  security:
    user:
      name: nie
      password: 123
(2)客户端
server:
  port: 9001
spring:
  application:
    name: cinema-portal-service
eureka:
  client:
    service-url:
      defaultZone: http://nie:123@localhost:8761/eureka,http://nie:123@localhost:8762/eureka
      # 服务续约时间,每30秒发送一次心跳检测,证明自己还活着
    lease-renewal-interval-in-seconds: 30
      # 服务失效时间,默认90秒
    lease-expiration-duration-in-seconds: 90

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996142.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号