多个开发环境共用eureka、zuul
实现效果是:多人多台电脑运行开发环境,使用公共服务器上的eureka和zuul,zuul通过每个人的请求的IP地址,来转发到不同的运行环境中的服务。
一、启动程序,注册服务时,将服务的名称加上本地的IP地址,区分出来每个服务,避免出现同一服务两个环境启动导致变成负载的情况
修改服务模块的 application-dev.yml文件
使用idea程序参数输入dev.ip参数值为自己电脑的IP地址,也可实现成程序启动动态替换dev.ip参数为自己本地IP,将.替换成-避免后面出现的问题配置参数不支持.的情况

dev:
  ip:
spring:
  application:
    name: ${dev.ip}-服务名
启动程序注册到中显示为IP+服务名称

二、Feign配置 请求其他的服务中指定服务名时替换为IP+服务名,可以实现请求其他服务时找到的自己注册的那个服务中
@FeignClient(name = "${feign.service.serviceName}",fallbackFactory = ServiceFallbackFactory.class)
public interface XqUserInfoService {
    
}
修改服务模块的 application-dev.yml文件
feign:
    serviceName: ${dev.ip}serviceName
    serviceName2: ${dev.ip}serviceName2
    serviceName3: ${dev.ip}serviceName3
*以上只修改application-dev.yml文件,不修改prod,test等配置文件,避免出现线上注册服务导致的问题
三、在网关ZUUL中重写路由规则
public class DevServerRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {
    private static final Log log = LogFactory.getLog(DevServerRouteLocator.class);
    public DevServerRouteLocator(String servletPath, ZuulProperties properties) {
        super(servletPath, properties);
    }
    @Override
    public void refresh() {
    }
    @Override
    public Route getMatchingRoute(final String path) {
        Route route = this.getSimpleMatchingRoute(path);
        // 根据请求中的客户端IP自动去匹配服务。
        // 也可以指定,某些服务转发到公共服务中比如公共的授权服务等
        // 也可以实现配置文件自定义规则比如192.168.1.73这个IP下的转发规则限定每个服务的转发位置
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        String location = MessageFormat.format("{0}{1}", request.getRemoteHost().replace(".","-"), route.getLocation());
        route.setLocation(location);
        return route;
    }
}
@Profile({"dev"})  // 注意 只有开发环境才允许执行
@Configuration
public class UserServerBeanConfig {
    @Autowired
    ZuulProperties zuulProperties;
    @Autowired
    ServerProperties server;
    @Bean
    public DevServerRouteLocator getRouteLocator() {
        return new DevServerRouteLocator(this.server.getServlet().getContextPath(), this.zuulProperties);
    }
}
*以上只修改application-dev.yml文件,不修改prod,test等配置文件,注册Bean也只是在开发环境下才生效
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号