网站的简单模拟访问流程一般是:客户端浏览器------>智能路由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是配置在路由里面的红色标注的。