springcloud of zuul and config-server

1.zuul related presentations

zuul springcloud one component, used as a gateway request:

All coming from the device or site requests will go through Zuul reaches the rear end of the Netflix application. As a border nature of the application, Zuul provides dynamic routing, monitoring, and security features elastic load. Zuul filter using a variety of underlying achieve the following functions:

  • Authentication and security authentication is required to identify each resource, deny the request does not meet the requirements.
  • Performance monitoring and statistics tracking service boundary data to provide an accurate view of production.
  • Dynamic routing as required dynamic routing request to the backend cluster.
  • Gradually increase the flow of stress tests on the cluster to understand their performance.
  • Offload advance for each type of capacity allocation request, automatically discarded when the request exceeds capacity.
  • Static resources to deal with some of the responses returned directly at the border.

There underlying filter zuul prefilter pre, post filter post, error filter errorFilter, the filter may also define the

1. To achieve zuul, set up a gateway

1. Import dependence of zuul

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
 </dependency>

 2. Turn on the main configuration gateway reverse proxy class above

@EnableZuulProxy

3. Configuration file yml, gateway module needs to be registered with the registry eureka

Eureka: 
Client:
serviceUrl:
defaultzone: HTTP: // localhost: 1010 / Eureka /
instance:
the prefer-ip-address: # define to true ip to the registry registration
instance-id: zuul-client: 1020 # id using the Register
server :
Port: 1020
the Spring:
the Application:
name: Zuul-client # client to the gateway server a name in erueka
Zuul: 
ignored-Services: "*"
routes:
systemmanage-Server: "/ System / **"

4. Custom gateway, to write a custom class that inherits ZuulFilter, override four methods, is a sequence, is a type , return a boolean result, a boolean value determination is based on whether the result of run () method
false, do not run method, true run method
@Component 
public class LoginCheckFilter the extends ZuulFilter {
@Override
public String filterType () {
return PRE_TYPE;
}

@Override
public int filterOrder () {
return. 3;
}

@Override
public Boolean shouldFilter () {
// Get the context object
RequestContext currentContext = RequestContext. getCurrentContext ();
// Get request object
the HttpServletRequest currentContext.getRequest request = ();
// Get the path by the resource request object
String requestURI Request.getRequestURI = ();
// if the request contains a path Login
iF (requestURI.toUpperCase () .contains ( "LOGIN")) {
to false return;
}
return to true; // true executed following return run method
}

@Override
public Object run () throws ZuulException {
// Get the context object
the RequestContext CurrentContext RequestContext.getCurrentContext = ();
// Get request object
HttpServletRequest request = currentContext .getRequest ();
    // get the response objects
the HttpServletResponse currentContext.getResponse response = ();
    // set the return type and encoding
the response.setContentType ( "file application / JSON; charset = UTF-. 8");
the Map <String, Object> jsonMap new new HashMap = <> ();
jsonMap.put ( "Success", false);
jsonMap.put ( "msg", "Login ....");
// Get request header
Request.getHeader header = String ( "X-token");
IF (StringUtils.isBlank (header)) {
//
the try {
response.getWriter () Print (JSON.toJSONString (jsonMap));. // used herein Ali Baba fastjson packet, the process returns to help us json, string object into json
} the catch (IOException E) {
e.printStackTrace ();
}
currentContext.setSendZuulResponse (to false); // if false, returns the response directly results, the following code do not execute
}
return null;
}
}

2. Use the configuration center config-server

Introduction: the role of distribution center is from a remote repository pull to point to the Configuration Center configuration files, and configuration center is pointing to a remote repository, so the micro services based on, in the name of the configuration file of remote factory library, you can configure the center of the bridge to pull their configuration.

1. To use the remote repository, you need to use a third-party warehouse as github, or cloud code, how to create a warehouse in the cloud code is no longer described above

2. In a new item inside the central module config-server configuration

3. import dependencies

<dependencies>
<!-- Config-Server 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-web</artifactId>
</dependency>
</dependencies>

4.主配置类开启配置中心
@EnableConfigServer
5. yml configuration file, point the remote repository
instance: 
the prefer-ip-address: to true # using the ip address of the registered
instance-id: config-server # specify the services the above mentioned id
Server:
Port: 5000
the Spring:
the Application:
name: config-Server
Cloud:
config:
Server:
git:
uri: https://gitee.com/huang__ming/springcloud-config.git # code points to cloud storage, this is the address of the remote repository
username: [email protected]
password: asd19930256

2. build client configuration center

1. Import dependence

<!-- Config-Client 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

2. Modify application.yml file named bootstrap.yml

3. The client configuration, the configuration code to the cloud repository (create a file zuul-client-dev.yml in the warehouse)

eureka:
client:
serviceUrl:
defaultZone: http://localhost:1010/eureka/
instance:
prefer-ip-address: true #定义ip到注册中心注册
instance-id: zuul-client:1020
server:
port: 1020
spring:
application:
name: zuul-client
zuul:
ignored-services: "*"
routes:
systemmanage-server: "/system/**"

4. After the configuration file yml modify the file name, the file path to the address configuration

spring:
cloud:
config:
uri: http://localhost:5000
name: zuul-client
profile: dev



5. Test to start the registration center services, micro-boot configuration service center to start the micro gateway services, browser and enter localhost: 5000 / zuul-client.dev.yml, if we can read to a remote repository 
content file instructions to configure the successful integration center



Guess you like

Origin www.cnblogs.com/19930909a/p/12121373.html