Integration of hot-load django dev sever and hot-load reactjs dev server

Posted LightSong@计海拾贝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Integration of hot-load django dev sever and hot-load reactjs dev server相关的知识,希望对你有一定的参考价值。

background

基于django和react构建开发环境:

  1. django+restframework作为后台API提供者
  2. react+ant design作为前端UI框架

利用django和react的开发工具的热更新功能, 实现前后台代码更新都不需要手动重启server。

如下拓扑图。

首先,浏览器从 react dev server上请求前端脚本,

然后 前端脚本 访问 django dev server 的 restful和websocket接口。

 

这就牵扯到跨域访问问题, 需要在django dev server上开启对react dev server的域名许可。

 

django

pipenv run python3 manage.py runserver

react

npm start

 

Topology

 

 

 

 

域访问许可响应报文头

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

Access-Control-Allow-Origin

Access-Control-Allow-Origin 响应头指定了该响应的资源是否被允许与给定的origin共享。

语法

Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: <origin>

 

django CORS plugin

https://github.com/adamchainz/django-cors-headers

https://pypi.org/project/django-cors-headers/

A Django App that adds Cross-Origin Resource Sharing (CORS) headers to responses. This allows in-browser requests to your Django application from other origins.

About CORS

Adding CORS headers allows your resources to be accessed on other domains. It’s important you understand the implications before adding the headers, since you could be unintentionally opening up your site’s private data to others.

设置

CorsMiddleware 必须放在 CommonMiddleware 之前。

Install from pip:

python -m pip install django-cors-headers

and then add it to your installed apps:

INSTALLED_APPS = [
    ...,
    "corsheaders",
    ...,
]

Make sure you add the trailing comma or you might get a ModuleNotFoundError (see this blog post).

You will also need to add a middleware class to listen in on responses:

MIDDLEWARE = [
    ...,
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    ...,
]

CorsMiddleware should be placed as high as possible, especially before any middleware that can generate responses such as Django\'s CommonMiddleware or Whitenoise\'s WhiteNoiseMiddleware. If it is not before, it will not be able to add the CORS headers to these responses.

 

在django的setting文件中,配置允许的域名列表

CORS_ALLOWED_ORIGINS = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000",
]

 

出处:http://www.cnblogs.com/lightsong/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

原文链接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

TIPS

This article based on:a

  • Spring Boot 2.1.5
  • Spring Cloud Greenwich.SR1
  • Spring Cloud Alibaba 0.9.0
  • Nacos 1.0.0

1. What is Sentinel ?

With the popularity of microservices, the stability between services and services is becoming more and more important. Sentinel uses traffic as an entry point to protect the stability of services from multiple dimensions such as flow control, blowdown, and system load-balance protection.

In a nutshell, Sentinel is a lightweight flow control, blowdown and degraded Java library.

Sentinel has the following characteristics:

  • Rich application scenarios:Sentinel undertakes the core scene of Alibaba‘s "Double Eleven" promotion traffic for nearly 10 years. For example, spikes (that is, burst flow control can be tolerated in the system capacity), message peaking and valley filling, cluster flow control, real-time fuse downstream applications that are not available.
  • Complete real-time monitoring:Sentinel also provides real-time monitoring. You can see the single machine second-level data of the access application in the console, or even the aggregate operation of clusters of less than 500 sizes.
  • Extensive open source ecology:Sentinel Provides out-of-the-box integration modules with other open source frameworks/libraries. For example, integration with Spring Cloud, Dubbo, gRPC. You only need to introduce the appropriate dependencies and perform a simple configuration to quickly access Sentinel.
  • Complete SPI extension point:Sentinel provides an easy-to-use, comprehensive SPI expansion interface。You can quickly customize the logic by implementing an extension interface. Such as custom rule management, adapting dynamic data sources, etc.

2. Guides

  • Add dependencies:

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
      <version>0.2.1.RELEASE</version>
    </dependency>
  • Add configurations:

    server:
      port: 8010
    spring:
      application:
        # Specify the name of the service registered to the nacos server
        name: microservice-consumer-movie
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
  • Add Controller:

    @RequestMapping("/movies")
    @RestController
    public class MovieController {
      @Autowired
      private RestTemplate restTemplate;
    
      @GetMapping("/users/{id}")
      public User findById(@PathVariable Long id) {
        // Use the placeholder of the RestTemplate
        User user = this.restTemplate.getForObject(
          "http://microservice-provider-user/users/{id}",
          User.class,
          id
        );
        // ...Movie microservices business...
        return user;
      }
    }

    It can be seen from the code that this one can‘t be normal controller! Because Sentinel starter will provide a current limit for all HTTP services by default, the Controller can be protected by Sentinel (but there are no rules for configuring protection yet, so it has not been protected yet)!

3. Test

  • Access http://localhost:8010/actuator/sentinel ,the following results can be obtained:

    {
      "DegradeRules": [],
      "datasources": {},
      "ParamFlowRule": [],
      "SystemRules": [],
      "FlowRules": [],
      "properties": {
          "eager": false,
          "enabled": true,
          "datasource": {},
          "transport": {
              "port": "8719",
              "dashboard": "localhost:8080",
              "heartbeatIntervalMs": null
          },
          "metric": {
              "fileSingleSize": null,
              "fileTotalCount": null,
              "charset": "UTF-8"
          },
          "servlet": {
              "blockPage": null
          },
          "filter": {
              "order": -2147483648,
              "urlPatterns": ["/*"]
          },
          "flow": {
              "coldFactor": "3"
          },
          "log": {
              "dir": null,
              "switchPid": false
          }
      }
    }

At the moment, we don‘t know what the meaning of the result exposed by /actuator/sentinel is. It doesn‘t matter, please read on.

4. Summary

Just add the spring-cloud-starter-alibaba-sentinel dependency to your app, and all HTTP interfaces get Sentinel protection! Of course, we currently have no rules for configuring protection for Sentinel.

5. Sample Code

GitHub

Gitee

原文链接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

转载,请保留原文地址,谢谢 ~

以上是关于Integration of hot-load django dev sever and hot-load reactjs dev server的主要内容,如果未能解决你的问题,请参考以下文章

Mol Cell Proteomics. | Integration and analysis of CPTAC proteomics data in the context of cancer ge

单细胞数据整合方法 | Comprehensive Integration of Single-Cell Data

Laravel 5.5 socialite integration 显示错误 formatRedirectUrl() must be of the type array, null given

How-to centralized integration of eventbridge event notifications sent to feishu

请教ansys的一个问题:Element 54174 integration 1 point is located outside of end statios.

Continuous Integration - 持续集成