Gateway is to do some 过滤或拦截
operations to make our services more secure user access to our services when we should first and then forwarded by the gateway to our services through micro gateway
1. Create a gateway service Module
2. Select springboot still works
3. The old rules a name
4. Check the registry client
5. Check the gateway module zuul
6. write configuration file
Server: # Port number Port: 8085 the Spring: the Application: # service name - using the name of communication between the service name: Service - Zuul Eureka: Client: Service - url: # Fill in the registration center server address defaultzone: HTTP: // localhost : 8081 / Eureka Zuul: routes: # set the path name just a service from the service - a: path: / service-a / ** # here to write a name registration services serviceId: service-a-objcat # set the path name of the service b easily play service-b: path: / Service-b / ** # Here to write b name registration services serviceId: service-objcat-b
7. Create a package name is com.objcat.filter,
to create a class TokenFilter继承ZuulFilter
used to implement filtering rules
8. A method of rewriting ZuulFilter, write logic filtration run () method
Import com.netflix.zuul.ZuulFilter; Import com.netflix.zuul.context.RequestContext; Import org.springframework.stereotype.Component; Import the javax.servlet.http.HttpServletRequest; public class TokenFilter the extends ZuulFilter { / ** * Filter pre represents a logical type operations before the request * / @Override public String filterType () { return "pre" ; } / ** * filter execution sequence * when a request when the presence of a plurality of filters of the same filter stage the order of execution * / @Override public int filterOrder () { return0 ; } / ** * is turned filtered * / @Override public Boolean shouldFilter () { return to true ; } / ** * filter intercepting write business logic code * / @Override public Object RUN () { the RequestContext CurrentContext = the RequestContext. getCurrentContext (); the HttpServletRequest Request = currentContext.getRequest (); String token = request.getParameter ( "token" ); IF (token == null ) { currentContext.setSendZuulResponse (false); currentContext.setResponseBody("token is null"); currentContext.setResponseStatusCode(401); } return null; } }
The logic is very simple client to check whether the request is null if the token is empty can not be returned by token is null
9. configure the gateway entry file, do not forget this place instantiate out filter
or do not take effect
import com.example.servicezuul.filter.TokenFilter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; @SpringBootApplication // 添加注解声明是注册中心客户端 @EnableEurekaClient // 开启网管 @EnableZuulProxy public class ServiceZuulApplication { public static void main(String[] args) { SpringApplication.run (ServiceZuulApplication. Class , args); } // instantiate tokenfilter, the NMS does not take effect or @Bean TokenFilter TokenFilter () { return new new TokenFilter (); } }
10. Access via a service gateway
Only need to use 网关的地址 + 网关的端口号 + 服务的别名路径(配置文件中配置) + api名称
to access the
http://localhost:8085/service-a/testA
http://localhost:8085/service-a/testA?token=123
When there is no token is returned token is null,
when the token has a value can be a normal visit
After this gateway forwards the request is called a reverse proxy You can hide the true address of your local server's
address only exposed to the outside of the gateway and then forwarded to the gateway server in order to achieve higher security