配置文件 bootstrap.ymlSpringCloud_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
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



