springcloud实现动态路由zuul.

本文介绍了一个基于Eureka、Feign及Zuul实现的服务发现、负载均衡与动态路由的微服务架构实例。从Eureka注册中心开始,通过两个服务提供者进行服务注册,并使用Feign结合Hystrix实现负载均衡及服务熔断。最后通过Zuul作为API网关,完成整个微服务架构的搭建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网站的简单模拟访问流程一般是:客户端浏览器------>智能路由gate way-(反向代理)------>声明式服务调用接口(feigin)----->应用集群(至少两个服务提供者)------->底层数据库

以下代码亲测通过:

一:eureka注册中心
package com.itmuch.cloud.microservicediscoveryeureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class MicroserviceDiscoveryEurekaApplication {

   public static void main(String[] args) {
      SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
   }
}

二:服务提供者,分别用8080、8081启动两个服务并注册到eureka

spring:
  application:
    name: microservice-provider-user
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    datasource: mysql
    show-sql: true
    hibernate:
      ddl-auto: update
    generate-ddl: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect

logging:
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE


eureka:
  client:
    service-url:
      defaultZone: http://discovery:8761/eureka

instance:
  preferIpAddress: true

三:Feign集成hystrix实现负载均衡和熔断保护

package com.itmuch.cloud.microserviceconsumermoviefeignwithhystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class MicroserviceConsumerMovieFeignWithHystrixApplication {

   public static void main(String[] args) {
      SpringApplication.run(MicroserviceConsumerMovieFeignWithHystrixApplication.class, args);
   }
}
server:
  port: 8015

spring:
  application:
    name: microservice-consumer-movie-ribbon-with-hystrix
eureka:
  client:
    service-url:
      defaultZone: http://discovery:8761/eureka/
  instance:
    prefer-ip-address: true
    hostname: ribbon

四:动态路由网管gateway api接口
spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050

eureka:
  instance:
    hostname: gateway

client:
  serviceUrl:
    defaultZone: http://localhost:8761/eureka/

zuul:
  routes:
    ribbon:
      url:  http://localhost:8015

#ribbon.eureka.enabled=false

顺便搞一个前置路由。也就是在路由请求之前。拦截该请求




package com.itmuch.cloud.microserviceconsumermoviezuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * : 描述信息
 * 前置过滤器,在请求前执行
 * @author liyy
 * @date 2018-07-30 11:03
 */
@Component
public class PreZuulFilter extends ZuulFilter{
    public static Logger logger  = LoggerFactory.getLogger(PreZuulFilter.class);

    /**
     * 前置过滤器
     * @return
     */
    public String filterType() {
        return "pre";
    }

    public int filterOrder() {
        return 0;
    }

    public boolean shouldFilter() {
        return true;
    }

    public Object run() {
        //打印请求路径
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        logger.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
        return null;
    }
}

最后把eureka、feign、zuul、userProvider(8080)、userProvider(8081)都启动

访问http://localhost:8050/ribbon/hystrix/1  (http://gateway ip:gateway port/service-id/...)

service-id是配置在路由里面的红色标注的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值