java version of the e-commerce spring cloud distributed micro social service b2b2c electricity supplier (five) routing gateway (zuul)

In the micro-service architecture, service management requires several basic components, including registration and service discovery, service consumption, load balancing, circuit breakers, intelligent routing, configuration management, mutual cooperation of these basic components, jointly set up a simple micro-service system. A micro short-answer service system below:

Azure (1) .png
Note: A and B services are services that can be invoked from each other, plotting the time forgotten. And configure the service is registered with the service registry.

In Spring Cloud micro-services system, a common load balancing methods, the client request first passes through load balancing (zuul, Ngnix), and then reach the service gateway (zuul cluster), and then to a specific service. , Unified registration services to highly available cluster service registry, all configuration files by the configuration service management services (under article describes), configure service profiles on the git repository to facilitate developers to change the configuration at any time.

A, Zuul Introduction
The main function of Zuul is routing forwarding and filters. Routing function is part of the micro-services, such as / api / user is forwarded to the service to the user, / api / shop to shop forwarded to the service. zuul default and Ribbon combination to achieve a load balancing feature.

zuul has the following features:

Authentication
Insights
Stress Testing
Canary Testing
Dynamic Routing
Service Migration
the Load Shedding
Security
Static the Response Handling
the Active / traffic the Active Management
Second, prepare for work
to continue to use on a project. In the original project, create a new project.

Third, create a service-zuul project
its pom.xml file as follows:

<?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">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.forezp</groupId>
    <artifactId>service-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>service-zuul</name>
    <description>Demo project for Spring Boot</description>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

At its inlet applicaton class annotate @EnableZuulProxy, open zuul features:

@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ServiceZuulApplication.class, args);
    }
}

Plus profiles application.yml add the following configuration code:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8769
spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: service-ribbon
    api-b:
      path: /api-b/**
      serviceId: service-feign

First registered address designated service center is http: // localhost: 8761 / eureka /, port services for 8769, the service name service-zuul; to request / api-a / the beginning are forwarded to the service-ribbon service; to / api-b / requests are forwarded to the beginning of the service-feign services;

This in turn runs five engineering; open the browser to access: HTTP: // localhost:? 8769 / API-A / hi name = forezp ; browser shows:

hi forezp,i am from port:8762

Open a browser to access: HTTP: // localhost: 8769 / API-b / hi name = forezp? ; Browser shows:

hi forezp,i am from port:8762

This shows zuul played a role in routing

Social e-commerce platform source code, please add penguin beg: 3536247259

Guess you like

Origin blog.csdn.net/qq_42748864/article/details/91411311