动态配置及自定义负载规则

1 gateway项目

pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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:

'[/**]':

allowedOrigins: "*"

allowedMethods:

- GET

- POST

- PUT

- DELETE

- OPTIONS

# 路由负载配置

default-filters:

routes:

- id: my_route

uri: lb://service-producer-1

predicates:

- Path=/**

filters:

- StripPrefix=1

consul:

discovery:

serviceName: gateway

host: localhost

port: 8500

service-producer-1:

ribbon:

# 负载地址

# listOfServers: http://localhost:8072, http://localhost:8073

# 负载策略(自己开发的负载策略 继承 AbstractLoadBalancerRule)

NFLoadBalancerRuleClassName: com.vlife.app.rules.RulesOfTheLoad

# 健康检查

NFLoadBalancerPingClassName: com.modules.scistor.config.HealthExamination

application.yml

spring:

profiles:

active: dev

2 微服务项目

pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

SpringCloud_GW_Rib_Consul

org.example

1.0-SNAPSHOT

4.0.0

SpringBoot_1

jar

org.springframework.cloud

spring-cloud-starter-gateway

org.springframework.cloud

spring-cloud-starter-consul-discovery

org.springframework.boot

spring-boot-starter-actuator

配置文件

application.yml

server:

port: 8882

spring:

application:

name: springboot-1

cloud:

consul:

discovery:

serviceName: service-producer-1

host: 10.7.29.191

port: 8500

3 Consul 下载及安装

consul下载

下载地址:https://www.consul.io/downloads

选择windows或者linux下载

consul启动

windows

下载完成之后是EXE文件在该文件所在目录打开cmd使用:consul agent -dev 命令进行启动

linux

下载完成之后使用:./sonsul agent -dev

以上的启动consul地址只对localhost发布

如果需要远程访问需要在启动命令后追加 -client 0.0.0.0 即对所有的IP开放

consul查看

访问地址:http://127.0.0.1:8500/ui/dc1/services

consul配置

页面中key/value的配置创建及动态使用

Key/Values

创建配置

key or folder中输入 config/gateway:dev/data 其中config为gateway配置文件中的prefix这个属性的值,gateway为网关项目的项目名称,data为配置文件中的data-key这个属性的值。(注意路径和配置相匹配,不匹配则动态配置不会生效)

value则为自己要动态配置的配置值

consul的动态配置要动态生效 配置的引用类需要加注解 @RefreshScope

注:要想将动态配置的值使用到自定义的路由规则中请参考以下文件

package com.vlife.app.config;

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

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.stereotype.Component;

/**

* @author

* @description: TODO

* @date 2021/11/19 14:46

*/

@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;

/**

* @author

* @description: TODO

* @date 2021/11/15 16:21

*/

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

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: