SpringCloud2.x搭建完整Demo(五) - 集成Zuul路由
1.配置Zuul路由
2.配置过滤访问
3.配置安全访问
---------------------------------------------------------------------------------------------------------------------------
1.配置Zuul路由
1.1 新建Zuul模块【myspringcloud-zuul-gateway】,引入POM依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>myspringcloud</artifactId><groupId>com.en</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>myspringcloud-zuul-gateway</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies>
</project>
1.2 【myspringcloud-zuul-gateway】修改application.yml文件
server:port: 9501eureka:client: # 客户端进行Eureka注册的配置service-url:defaultZone: http://admin:root@eureka:7001/eureka,http://admin:root@eureka2:7002/eureka,http://admin:root@eureka3:7003/eurekaregister-with-eureka: falsespring:application:name: myspringcloud-zuul-gatewayzuul:routes:users:path: /users-proxy/**serviceId: myspringcloud-provider-users-hystrixignored-services:"*"prefix: /myspringcloud-api
1.3 创建启动类
package com.en;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@SpringBootApplication
@EnableZuulProxy
public class ZuulApp {public static void main(String[] args) {SpringApplication.run(ZuulApp.class,args);}
}
1.4 因为Zuul目前对springboot2.1.2支持不好,所以对父工程springboot版本进行降级
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.0.7.RELEASE</version><type>pom</type><scope>import</scope></dependency>
1.5 启动注册中心,生产者用户服务,正常访问和通过Zuul代理访问正常
2.配置过滤访问
2.1 【myspringcloud-zuul-gateway】 修改application.yml配置,增加产品微服务
server:port: 9501eureka:client: # 客户端进行Eureka注册的配置service-url:defaultZone: http://admin:root@eureka:7001/eureka,http://admin:root@eureka2:7002/eureka,http://admin:root@eureka3:7003/eurekaregister-with-eureka: falsespring:application:name: myspringcloud-zuul-gatewayzuul:routes:users:path: /users-proxy/**serviceId: myspringcloud-provider-users-hystrixproduct:path: /product-proxy/**serviceId: myspringcloud-provider-productignored-services:"*"prefix: /myspringcloud-api
2.2 因为产品服务配置了加密访问,所以Zuul模块也需要追加过滤处理,新建AuthorizedRequestFilter
package com.en.filter;import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;import java.nio.charset.Charset;
import java.util.Base64;public class AuthorizedRequestFilter extends ZuulFilter {@Overridepublic String filterType() {return FilterConstants.PRE_TYPE;}@Overridepublic int filterOrder() {return 0;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic Object run(){// 获取当前请求的上下文RequestContext currentContext = RequestContext.getCurrentContext() ;// 认证的原始信息String auth = "admin:root";// 进行一个加密的处理byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));String authHeader = "Basic " + new String(encodedAuth);currentContext.addZuulRequestHeader("Authorization", authHeader);return null;}
}
2.3 新建配置类,启动时加载filter
package com.en.config;import com.en.filter.AuthorizedRequestFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ZuulConfig {@Beanpublic AuthorizedRequestFilter getAuthorizedRequestFilter() {return new AuthorizedRequestFilter() ;}
}
2.4 因为降低了springboot版本,所以生产者产品服务数据库驱动类需要进行修改
driver-class-name: com.mysql.jdbc.Driver # 配置MySQL的驱动程序类
2.5 通过Zuul代理调用产品服务
3.配置安全访问
3.1 【myspringcloud-zuul-gateway】修改pom文件,增加安全访问模块
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
3.2 【myspringcloud-zuul-gateway】修改application.yml配置文件,增加用户配置
security:user:name: adminpassword: root
3.3 再次访问之前接口的时候提示输入用户名密码,输入之后接口调用成功
到这里,Springcloud常用五大组件Demo已经全部搭建完毕,剩下的Config配置中心Netflix版的感觉不是很好用,以后有时间分享一下alibaba的,还剩下SpringCloudStream和SpringCloudSleuth都需要集成消息中间件,就不放在Demo程序中了。
源码自取:
https://github.com/L1021204735/myspringcloud5