API Gateway (a)

API Gateway is a population of external services, it hides the implementation of the internal architecture, micro-architecture services essential component. API gateway can manage a large number of our API interface, you can also interface the customer, adaptation protocol, secure authentication, forward routing, traffic restrictions, monitoring logs, to prevent the reptiles, gray publishing.

As your business grows, more and more services, how to call micro-service front-end user becomes a problem. For example, a user assessment district to assess community needs to show details after the completion of price trends, transaction data, listing data, this information is in different services, the front-end system wants to implement such a feature would require a large number of services and interact, call interface they provide, so performance is certainly low. And the logical front-end system is more complex, it needs to know all the micro-services information provided. This time API gateway acts manifested by polymerization internal service API provides a unified API interface to external front-end system, shielding the internal implementation details.

1.Zuul Profile

Zuul is Netflix OSS in one, is a JVM-based routing and server load balancer. Services Framework provides routing, monitoring, resiliency, security and other aspects. Zuul works with Eureka, Ribbon, Hystrix other components.

Zuul's core is a filter through which we can filter out a lot of extended features, such as:

Dynamic Routing: dynamically routing client requests to different backend service logic to do some processing, such as a plurality of aggregated data and services return.

• Request monitoring: You can monitor the entire system requests, write a detailed response to the request log, real-time statistics of the current traffic system status and monitoring.

· Certification authorization: do certification for each access request, reject illegal request, to protect the back-end services.

Pressure test: The pressure test is a very important job, like some of the electricity supplier companies need to simulate more realistic user concurrency to ensure the stability of the system during major events. Can be forwarded by Zuul dynamically request to cluster the back-end services, it can also identify the real test traffic and traffic to do some special treatment.

· Gray Published: gray release can ensure the stability of the overall system, can be found in the initial gray when the adjustment problems, in order to ensure that the degree of influence.

2. Use the Zuul building micro gateway service

2.1 simple to use

Create a maven project fangjia-fsh-api increased reliance Spring Cloud projects in pom.xml and then rely Zuul's:

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

Profile Information:

= Fangjia-FSH-spring.application.name API 
server.port = 2103 
# configured by zuul.routes routing forwarding, cxytiandi is the name of the custom 
# when access cxytiandi / ** time will jump to the beginning of the address http: //baidu.com/ the 
zuul.routes.cxytiandi.path = / cxytiandi / ** 
zuul.routes.cxytiandi.url = http://baidu.com/

Next, create a startup class, open proxy routing function by @EnableZuulProxy, code is as follows:

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class, args);
    }
}

2.2 Integration Eureka

Through the study of the previous section, we already can simply use Zuul forwarding route, and in actual use, we usually use Zuul to forward the request to the internal proxy service up, the same as an external service.

The number of internal services will be a lot, but can be extended at any time, we can not add a service routing configuration is changed once, so they have to achieve dynamic routing forwarding through a combination of Eureka. First I want to add Eureka dependency:

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

Start class does not need to be modified because @EnableZuulProxy already comes with @EnableDiscoveryClient, simply add the address to Eureka in the configuration file:

eureka.client.serverUrl.defaultZone=http://yinjihuan:123456@master:8761/eureka/

Restart the service, we can access the service by default in Eureka forwarding rules. Hello interfaces such as access to our previous definition of equivalent through http: // localhost: 2013 / fsh -house / house / hello to access hello interfaces fsh-house service. Access rule is "service name API gateway address + + interface to access the URI".


11

Guess you like

Origin www.cnblogs.com/xc-xinxue/p/12459955.html