SpringCloud project to build (six): Service gateway routing structures

Sixth, service routing gateway 

1. Create a new module in the parent project above myzuul 
selected Spring Cloud 2. Discovery-> Eureka Discovery Client 
selected Cloud routing- the Spring> Zuul 
3.Module generally do not modify the Name, and the project name as Artifact 
4. src \ main \ resources following application.properties renamed application.yml, modify the file encoding is UTF-8, as follows 
Server: 
  Port: 1003 
Spring: 
  file application: 
    name: myzuul 
  Profiles: 
    Active: default 
  Zipkin: 
     Enabled: to true 
     base- url: HTTP: // localhost: 9411 / 
     Locator: 
       Discovery: 
         Enabled: to true 
     SENDER: 
       of the type: the WEB 
  Sleuth: 
     Web: 
       Client: 
         Enabled: to true





 
 
     Sampler:
       # The default sampling rate is 0.1, you can not see all of the requested data 
       # 1 to change the sample rate, you can see all of the requested data, but this will increase the interface call delay 
       Probability: 1.0 
Eureka: 
  instance: 
    Lease-Renewal-interval The -in-seconds: 5 # heartbeat time, a service renewal interval (default 30s) 
    Lease-expiration-DURATION-in-seconds the: 10 # no heartbeat out of time, 10 seconds, a service contract expires (the default is for 90s) 
    the prefer-ip-address: IP to true # registered to the service registry 
  Client: 
    service-url: 
      defaultzone: HTTP: // localhost: 1024 / Eureka / 
    FETCH-registry: # registered to the registry to true 
    registry -fetch-interval-seconds: 5 # service list of cache update time, default 30 seconds 
Zuul: 
  ignored-services: "*" 
  Strip-prefix: remove the forwarding path (path in value) when true # forwards the request 
  routes :
    myservice: # api to all requests beginning with / are forwarded to myservice service
      path: / API / ** 
      the serviceId: MyService 


5.pom file dependent increase, and there refresh Maven Projects 

 
    public static void main (String [] args) {< dependency > 
    < the groupId > org.springframework.cloud </ the groupId > 
    < the artifactId > Spring-Cloud-Starter-Zipkin < / the artifactId > 
</ dependency > 

6. the open src \ main \ java \ com \ li \ myzuul following MyzuulApplication.java 

added @EnableZuulProxy annotation on startup class, open zuul. 

@EnableZuulProxy 
@SpringBootApplication 
public class MyzuulApplication { 
}

        SpringApplication.run (MyzuulApplication.class, args); 
    } 


7. The opening a browser HTTP: // localhost: 1003 / API / the getTime 


8. The increase authentication security, the src \ main \ java com \ li \ myzuul below \ New Package filter, New MyFilter.java 

@Component 
public class ZuulFilter the extends the MyFilter { 

    Private static Logger LoggerFactory.getLogger = log (MyFilter.class); 
    @Override 
    public String filterType () { 
        return "pre"; 
    } 

    @Override 
    public int filterOrder () { 
        0 return; 
    } 

    @Override 
    public Boolean shouldFilter () { 
        return to true; 
    } 

    @Override 
    public RUN Object () { 
        the RequestContext RequestContext.getCurrentContext CTX = (); 
        the HttpServletRequest ctx.getRequest Request = ();
        log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("token");
        if(accessToken == null) {
            log.warn("token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e){}

            return null;
        }
        log.info("ok");
        return null;
    }
}
 
8. Open a browser to access http: // localhost:? 1003 / api / getTime token = 22

 

Guess you like

Origin www.cnblogs.com/liw66/p/12298346.html