Crazy SpringCloud Notes 05 (Zuul and Config)

Keywords: Java Spring Boot Spring Cloud RESTful

9.Zull Routing Gateway





Summary

What is zuul?

Zull contains two main functions: routing the request (for jumping) and filtering:

(The routing function is responsible for forwarding external requests to specific micro-service instances, which is the basis for achieving a unified entry for external access, while the filter function is responsible for intervening in the processing of requests, which is the basis for implementing such functions as request verification, service aggregation, etc. Zuul and Eureka are integrated to register Zuul as an application under Eureka service governance, and at the same time from Eureka.Messages for other services, that is, future access to microservices, are obtained through a Zuul jump.

Note: The Zuul service will eventually be registered with Eureka

Provides: proxy + routing + filtering three major functions!

What can Zuul do?

  • Route
  • filter

Official documents:https://github.com/Netflix/zuul/





Getting Started Cases

Create a new springcloud-zuul module and import dependencies

<dependencies>
    <!--Import zuul rely on-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zuul</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <!--Hystrix rely on-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <!--dashboard rely on-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboar</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <!--Ribbon-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <!--Eureka-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <!--Entity Class+web-->
    <dependency>
        <groupId>com.haust</groupId>
        <artifactId>springcloud-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--Hot Deployment-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

application.yml

server:
  port: 9527

spring:
  application:
    name: springcloud-zuul #Microservice name

# eureka Registry Configuration
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance: #id of instance
    instance-id: zuul9527.com
    prefer-ip-address: true # Show ip

info:
  app.name: haust.springcloud # entry name
  company.name: Xiyuan Campus of Henan University of Science and Technology # Corporate name

# zull routing gateway configuration
zuul:
  # Routing-related configuration
  # Original access route eg:http://www.cspStudy.com:9527/springcloud-provider-dept/dept/get/1
  # zull route configuration access route eg:http://www.cspstudy.com:9527/haust/mydept/dept/get/1
  routes:
    mydept.serviceId: springcloud-provider-dept # Service Provider Route Name of eureka Registry
    mydept.path: /mydept/** # Change service provider routing name of eureka registry to custom routing name
  # No longer accessible using this path, *: Ignore, hide all service names~
  ignored-services: "*"
  # Set Common Prefix
  prefix: /haust

Main Startup Class

/**
 * @Auther: csp1999
 * @Date: 2020/05/20/20:53
 * @Description: Zull Routing Gateway Primary Startup Class
 */
@SpringBootApplication
@EnableZuulProxy // Turn on Zuul
public class ZuulApplication_9527 {

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

Test:

You can see that the Zull Routing Gateway is registered with the Eureka Registry!

The above figure shows the route accessed by the service interface without the Zull routing gateway configuration. You can see that it is not safe to access the microservice (service provider) name directly. You cannot expose the microservice name!

So after the Zull routing gateway is configured, the routes accessed are:

We see that the microservice name is replaced and hidden, replaced by our custom microservice name mydept, with the prefix haust, which makes it possible to encrypt routing fan access!

Refer to the Springcloud Chinese Community zuul component for details:https://www.springcloud.cc/spring-cloud-greenwich.html#_router_and_filter_zuul









10.Spring Cloud Config Distributed Configuration

Dalston.RELEASE

Spring Cloud Config provides server and client support for external configurations in distributed systems. Use ConfigServer, you can manage the external properties of applications in all environments. Conceptual mappings on the client and server are the same as the abstractions of Spring Environment and ProertySource, so they fit well with Spring applications, but can be used with any application running in any language. As applications are deployed from developers to testing and productionProcesses allow you to manage the configurations between these environments and determine that your application has everything it needs to run when migrating. The server stores the default implementation of the backend using git, so it easily supports the label version of the configuration environment and accesses the various tools used to manage content. It is easy to add alternative implementations and insert them using the Spring configuration.





Summary

Configuration file issues facing distributed systems

Micro-services mean dividing the business in a single application into individual sub-services, each of which has a relatively small granularity, so there will be a large number of services in the system. Because each service needs the necessary configuration information to run, a centralized and dynamic configuration management facility is essential.Cloud provides configServer to solve this problem. Each of our micro-services carries its own application.yml, and hundreds of configuration files have been modified, which is a headache!

What is the SpringCloud config Distributed Configuration Center?

The spring cloud config provides centralized external support for microservices in the microservice architecture, and the configuration server provides a centralized external configuration for all aspects of different microservice applications.

The spring cloud config is divided into two parts, the service side and the client side.

The server, also known as Distributed Configuration Center, is a stand-alone microservice application that connects configuration servers and provides clients with access interfaces to obtain configuration information, encrypt and decrypt information.

(Clients manage application resources and business-related configuration content through a designated configuration center, and obtain and load configuration information from the configuration center at startup. The configuration server uses git by default to store configuration information, which helps version the environment configuration. It can also be easily managed and accessed through git client tools.Content.

What can the spring cloud config Distributed Configuration Center do?

  • Centrally Manage Profiles
  • Different environments, different configurations, dynamic configuration updates, sub-environment deployments, such as/dev/test/prod/beta/release
  • Configuration is dynamically adjusted during runtime, and configuration files no longer need to be written on the machines where each service is deployed. Services pull configuration information from the configuration center.
  • When a configuration changes, the service does not need to restart to be aware of the configuration changes and apply the new configuration
  • Exposing configuration information as a REST interface

Integration of spring cloud config Distributed Configuration Center with GitHub

Since spring cloud config uses git by default to store configuration files (there are other ways, such as self-contained SVN and local files), Git is the most recommended and is accessed in the form of http / https.





Getting Started Cases





Server

New springcloud-config-server-3344 module imports pom.xml dependencies

<dependencies>
    <!--web-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--config-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
    <!--eureka-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
</dependencies>

The application.yml configuration file is created under resource, and the Spring Cloud Config server provides configuration for remote clients from the git repository (which must be provided):

server:
  port: 3344

spring:
  application:
    name: springcloud-config-server
  # Connect code cloud remote warehouse
  cloud:
    config:
      server:
        git:
          # Note that it is https, not ssh
          uri: https://gitee.com/cao_shi_peng/springcloud-config.git 
            # You can connect to git via config-server, access its resources and configure~

# Failure to do so will result in a Cannot execute request on any known server error: the connection Eureka server address is incorrect
# Or simply comment out the eureka dependency and temporarily use no eureka here
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

Main Startup Class

@EnableConfigServer // Open the spring cloud config server service
@SpringBootApplication
public class Config_server_3344 {
    public static void main(String[] args) {
        SpringApplication.run(Config_server_3344.class,args);
    }
}

Submit the new application.yml created under the springcloud-config folder of the local git warehouse to the code cloud warehouse:

The default strategy for locating resources is to clone a git repository (in spring.cloud.config.server.git.uri) and use it to initialize a mini SpringApplication. Environment s for small applications are used to enumerate property sources and publish them through JSON endpoints.

HTTP services have resources in the following formats:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

Where "Application" is injected as spring.config.name in Spring Application (i.e., usually "Application" in a regular Spring Boot application), "Configuration File" is an active configuration file (or a property of a comma-separated list), and "label" is an optional git tag (default is "master").

Test Accesshttp://localhost:3344/application-dev.yml

Test Accesshttp://localhost:3344/application/test/master

Test Accesshttp://localhost:3344/master/application-dev.yml

Configurations that do not exist for test access are not displayed such as:http://localhost:3344/master/application-aaa.yml





Client

Submit the new config-client.yml created under the springcloud-config folder of the local git warehouse to the code cloud warehouse:

Create a new springcloud-config-client-3355 module and import dependencies

<!--config-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-start -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Create application.yml and bootstrap.yml configuration files under resources

bootstrap.yml is a system level configuration

# System level configuration
spring:
  cloud:
    config:
      name: config-client # Name of resource to read from git, no suffix
      profile: dev
      label: master
      uri: http://localhost:3344

application.yml is a user-level configuration

# User level configuration
spring:
  application:
    name: springcloud-config-client

Create ConfigClientController.java under the controller package for testing

@RestController
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String applicationName; //Get the microservice name

    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer; //Get the Eureka service

    @Value("${server.port}")
    private String port; //Get the port number of the server


    @RequestMapping("/config")
    public String getConfig(){
        return "applicationName:"+applicationName +
         "eurekaServer:"+eurekaServer +
         "port:"+port;
    }
}

Main Startup Class

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

Test:

Start server Config_server_3344 and client ConfigClient

Visit:http://localhost:8201/config/

Small Case

Locally create new config-dept.yml and config-eureka.yml and submit them to the code cloud warehouse

The content of the configuration file is not enumerated here and goes directly to the code.

Create a new springcloud-config-eureka-7001 module and copy the contents of the original springcloud-eureka-7001 module.

1. Empty the application.yml configuration for the module and create a new bootstrap.yml connection remote configuration

spring:
  cloud:
    config:
      name: config-eureka # Profile name in warehouse
      label: master
      profile: dev
      uri: http://localhost:3344

2. Add a spring cloud config dependency to pom.xml

<!--config-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

3. Main Startup Class

@SpringBootApplication
@EnableEurekaServer //Startup class of the EnableEurekaServer server, which can be registered by others~
public class ConfigEurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigEurekaServer_7001.class,args);
    }
}

4. Testing

Step 1: Start Config_Server_3344 and access http://localhost:3344/master/config-eureka-dev.yml test


Part 2: Start ConfigEurekaServer_7001, access http://localhost:7001/ test


Show the above image successfully

Create a new springcloud-config-dept-8001 module and copy the contents of springcloud-provider-dept-8001

Similarly import the spring cloud config dependency, empty the application.yml, create a new bootstrap.yml configuration file, and configure

spring:
  cloud:
    config:
      name: config-dept
      label: master
      profile: dev
      uri: http://localhost:3344

Main Startup Class

@SpringBootApplication
@EnableEurekaClient //Automatically register in Eureka after service startup!
@EnableDiscoveryClient //Service Discovery~
@EnableCircuitBreaker //
public class ConfigDeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigDeptProvider_8001.class,args);
    }

    //Add a Servlet
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        return registrationBean;
    }
}

Test (omitted)

Posted by andco on Sun, 12 Sep 2021 12:23:54 -0700