SpringCloud Getway

场景:微服务架构中多个服务共同完成一件事情 在部署时只有一个域名 此时需要对每个服务进行集中管理。使用新一代的路由网关(Getway)

demo 部署案例: 本列使用Consul 作为服务发现 

Service1:  

 pom.xml 核心依赖

  
            org.springframework.cloud
            spring-cloud-starter-consul-discovery
            2.1.2.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

服务注册关键代码:

 

 @Bean
    public ConsulServiceRegistry consulServiceRegistry(
            ConsulClient consulClient,
            ConsulDiscoveryProperties properties,
            TtlScheduler ttlScheduler,
            HeartbeatProperties heartbeatProperties) {
        return new ConsulServiceRegistry(
                consulClient, properties, ttlScheduler, heartbeatProperties);
    }


    @Bean
    @ConditionalOnMissingBean
    public ConsulDiscoveryProperties consulDiscoveryProperties(InetUtils inetUtils) {
        ConsulDiscoveryProperties properties = new ConsulDiscoveryProperties(inetUtils);
        //这个必须配,使用ip注册服务
        properties.setPreferIpAddress(true);
        //在consul中注册服务
        properties.setRegister(true);
        //启动服务发现
        properties.setEnabled(true);
        //禁止自动注销
        properties.setDeregister(true);
        //只需保证该字段不为空即可
        properties.setInstanceId(context.getEnvironment().getProperty("service.id"));
        //在注册中心显示的服务名称
        properties.setServiceName(context.getEnvironment().getProperty("spring.application.name"));
        //服务标记
        properties.setTags(Arrays.asList(""));
        //健康检查地址
        properties.setHealthCheckPath("/actuator/health");
        //健康检查间隔
        properties.setHealthCheckInterval("10s");
        return properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public ConsulClient getClient() {
        String consulUrl = null;
        int consulPort = 0;
        if (System.getenv("CONSUL_URL") == null || System.getenv("CONSUL_URL").equalsIgnoreCase("")) {
            consulUrl = "yourConsulHost";
            consulPort = 8500;
        } else {
            consulUrl = System.getenv("CONSUL_URL").split("//")[1];
            consulPort = 80;
        }
        return new ConsulClient(consulUrl, consulPort);
    }

    @Bean
    @ConditionalOnMissingBean
    public HeartbeatProperties heartbeatProperties() {
        HeartbeatProperties properties = new HeartbeatProperties();
        properties.setEnabled(true);
        return properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public TtlScheduler ttlScheduler(ConsulClient consulClient, HeartbeatProperties heartbeatProperties) {
        return new TtlScheduler(heartbeatProperties, consulClient);
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.context = applicationContext;
    }

开启 注册发现配置:

@EnableDiscoveryClient

Getway 关键配置 (使用StripPrefix GatewayFilter Factory 作为过滤工厂)

@Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        StripPrefixGatewayFilterFactory.Config config = new StripPrefixGatewayFilterFactory.Config();
        SetPathGatewayFilterFactory.Config c1 = new SetPathGatewayFilterFactory.Config();
        config.setParts(1);
        return builder.routes()
                .route("router1", r -> r.path("/s3").uri("http://host:8897/s3"))
                .route("router2", r -> r.path("/s2").uri("http://host:8899/s2"))
                .build();
    }

 开启getway 自动发现功能 :

spring.cloud.gateway.discovery.locator.enabled=true

测试: 在浏览器上输入 网关的 地址 :  根据地址上不同path切换到不同的服务上

如: http://localhost:8080/s2  切换到 service2 服务上

http://localhost:8080/s3 切换到 service3服务上

 

你可能感兴趣的:(Getway,springcloud)