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

Gateway+Consul动态配置及自定义负载均衡规则

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

Gateway+Consul动态配置及自定义负载均衡规则

动态配置及自定义负载规则 1 gateway项目 pom.xml


    
        SpringCloud_GW_Rib_Consul
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    GatewayTest

    
        
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
        
            org.springframework.cloud
            spring-cloud-starter-consul-all
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            com.google.guava
            guava
            29.0-android
        
        
            com.netflix.netflix-commons
            netflix-commons-util
            0.3.0
        
        
            com.netflix.servo
            servo-core
            0.12.21
        
    


配置文件 bootstrap.yml
spring:
  cloud:
    consul:
      host: 10.7.29.191
      port: 8500
      #enabled将此值设置为“false”禁用Consul配置
      config:
        enabled: true   #默认是true --
        format: YAML    # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
        #data-key: configuration    #表示consul上面的KEY值(或者说文件的名字) 默认是data
        data-key: data    #表示consul上面的KEY值(或者说文件的名字) 默认是data
        prefix: config    #prefix设置配置值的基本文件夹
        #defaultContext设置所有应用程序使用的文件夹名称
        #profileSeparator设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值
        profile-separator: ':'

application-dev.yml
server:
  port: 9999
spring:
  application:
    name: gateway
  cloud:
    gateway:
      # 跨域配置
      globalcors:
        cors-configurations:
          '[
@RefreshScope
@Component
public class ServiceGroup {

    @Value("${serviceGroup}")
    private String serviceGroup;

    public String getServiceGroup() {
        return serviceGroup;
    }

    public void setServiceGroup(String serviceGroup) {
        this.serviceGroup = serviceGroup;
    }

    @Override
    public String toString() {
        return "ServiceGroup{" +
                "serviceGroup='" + serviceGroup + ''' +
                '}';
    }
}
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import com.vlife.app.config.ServiceGroup;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


public class RulesOfTheLoad extends AbstractLoadBalancerRule {

    @Autowired
    private ServiceGroup serviceGroup;

    private Server choose(ILoadBalancer loadBalancer, Object key) {
        if (loadBalancer == null) {
            return null;
        }
        System.out.println(serviceGroup.toString());
        Server server = null;
        List servers = loadBalancer.getAllServers();
        return servers.get(0);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {
    }

    @Override
    public Server choose(Object key) {
        return choose(getLoadBalancer(), key);
    }
}

Gateway项目,微服务(SpringBoot项目)项目的接口开发和正常的SpringBoot接口开发一致,不再赘述。

如有其它需求,请联系1595860703@qq.com

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

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

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