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

Eureka一些

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

Eureka一些

生成eurekaServer端服务注册中心

    建module改POM
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-server
            

    写YML

    server:
      port: 7001
    
    eureka:
      instance:
        hostname: localhost
      client:
    #    表示是否在注册中心注册eureka本身
        register-with-eureka: false
    #    表示是否从Eureka Server获取注册的服务信息
        fetch-registry: false
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}

    主启动

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

将EurekaClient端cloud-provider-payment8001注册进EurekaServer成为服务提供者provider

    改POM
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    

    写YML

    eureka:
      client:
        register-with-eureka: true
    #   获取已经注册的信息,默认是true,单节点的时候无所谓,但是如果是集群,必须设置true才能配合ribbon使用负载均衡
        fetch-registry: true
        service-url:
          defaultZone: http://localhost:7001/eureka/

    主启动

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

    注册成功

将EurekaClient端cloud-consumer-order80注册进EurekaServer成为消费者consumer

    改POM

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

    写YML

    eureka:
      client:
        register-with-eureka: true
    #   获取已经注册的信息,默认是true,单节点的时候无所谓,但是如果是集群,必须设置true才能配合ribbon使用负载均衡
        fetch-registry: true
        service-url:
          defaultZone: http://localhost:7001/eureka/

    主启动

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

    注册成功


 为了实现高可用,搭建注册中心集群,实现负载均衡、故障容错:各个注册中心之间,互相注册,相互守望;先建第二个注册中心:

    建module(第二个注册中心)改POM(和eureka7001相同)修改映射文件:找到C:WindowsSystem32driversetc 路径下的hosts文件;修改映射配置添加进hosts文件127.0.0.1 eureka7001.com、127.0.0.1 eureka7002.com写YML(关键)
    server:
      port: 7001
    
    eureka:
      instance:
        hostname: eureka7001
      client:
    #    表示是否在注册中心注册eureka本身
        register-with-eureka: true
    #    表示是否从Eureka Server获取注册的服务信息
        fetch-registry: true
        service-url:
          defaultZone: http://eureka7002:7002/eureka/
    
    server:
      port: 7002
    
    eureka:
      instance:
        hostname: eureka7002
      client:
        #    表示是否在注册中心注册eureka本身
        register-with-eureka: true
        #    表示是否从Eureka Server获取注册的服务信息
        fetch-registry: true
        service-url:
          defaultZone: http://eureka7001:7001/eureka/
    
    
    ####注意:如果这两个register-with-eureka 和 fetch-registry不改为true的话 
    会导致注册中心变成unavailable-replicas

    搭建成功

 

将两个微服务注册进Eureka集群

    修改YML文件即可:
    service-url:
      defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka

配置服务集群: 

    建第二个服务module修改YML文件中的端口号
    server:
      port: 8002

    如何做到轮询这两个服务呢?将消费者端的请求地址改为通过服务名称去获取:

 

 public static final String PAYMENT_SRV = "http://CLOUD-PAYMENT-SERVICE";

并且将RestTemplate的配置类的方法上加上负载均衡的注解@LoadBalanced

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

这就是Ribbon的负载均衡的功能

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

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

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