python spring框架_python 模拟Spring Cloud

一,简介

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

内部运行原理

Spring Cloud作为云应用集中了很多组件包括:路由代理(Zuul)、注册与发现(Eureka and Client)、断路器(Hystrix)、消费服务者(Ribbon  and Feign)等,每个组件在架构都为实现不同的处理功能。看如下结构图:

1、外部或者内部的非spring cloud项目都先通网关zuul然后从eureka server中获取可用列表服务。

2、从eureka server注册中心返回可用实例。

3、通过ribbon负载均衡分配可用节点。

4、ribbon选择可用的节点分配到后端具体的实例

5、hystrix断路器查看当前实例是否可用,dashboard监控实例状态信息,Turbine监控服务间的调用和熔断相关指标。

6、后端服务与服务之间都是通过feign通信,处理请求业务后返回到Eureka中心返回结果。

二、项目演示1.0

简介

Spring Cloud功能比较多,这里使用6个django项目,来模拟java spring cloud架构,采用单入口模式调用api

不涉及ribbon,hystrix,feign等功能,仅仅做演示而已,不是真正意义上的spring cloud。

项目说明

环境说明

操作系统

配置

ip

软件

centos 7.6

2核4g

192.168.31.229

pyton3.5.2,nginx1.16.1,docker 19.03.5

MySQL

由于本项目中,数据存储采用的是mysql,为了快速演示,直接使用docker启动mysql

docker run -d --restart=always --name example -e MYSQL_ROOT_PASSWORD=abcd@1234 -p 3306:3306 -v /data/mysql_3306/data:/var/lib/mysql mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

初始化数据

# docker exec -it example /bin/bash

# mysql-u root -pabcd@1234mysql>CREATE DATABASE usercenter DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

mysql>CREATE TABLE `users` (

`id` bigint(20) NOT NULL AUTO_INCREMENT,

`username` varchar(16) DEFAULT NULL COMMENT '用户名',

`password` varchar(32) DEFAULT NULL COMMENT '密码',

`create_time` datetime DEFAULT NULL COMMENT '创建时间',

`last_time` datetime DEFAULT NULL COMMENT '最后一次登录时间',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';

mysql> INSERT INTO `usercenter`.`users` (`id`, `username`, `password`, `create_time`, `last_time`) VALUES ('1', 'xiao', '81dc9bdb52d04dc20036dbd8313ed055', '2020-02-05 11:55:27', '2020-02-05 11:55:30');

mysql>exit;

# exit

url转发

项目下载地址:

运行项目

eureka

cd django-spring-cloud/1.0/eureka

pip3install -r requirements.txt

python3 manage.py runserver0.0.0.0:8001

config

cd django-spring-cloud/1.0/config

pip3install -r requirements.txt

python3 manage.py runserver0.0.0.0:8002

auth

cd django-spring-cloud/1.0/auth

pip3install -r requirements.txt

python3 manage.py runserver0.0.0.0:8003

user

cd django-spring-cloud/1.0/user

pip3install -r requirements.txt

python3 manage.py runserver0.0.0.0:8004

gateway

cd django-spring-cloud/1.0/gateway

pip3install -r requirements.txt

python3 manage.py runserver0.0.0.0:8000

前端

cd django-spring-cloud/1.0/demo_login

pip3install -r requirements.txt

python3 manage.py runserver0.0.0.0:8080

配置nginx

将nginx_conf放入 /etc/nginx/conf.d目录,并启动nginx

yum install -y nginxcp django-login-example/1.0/nginx_conf/*/etc/nginx/conf.d

# 启动nginx

nginx

配置域名解析

如果没有dns,请修改windows 10的hosts文件,添加2条记录

192.168.31.229h5.baidu.com192.168.31.229 api.baidu.com

访问页面

http://h5.baidu.com

登录信息

用户名:xiao

密码:1234

登录成功之后,效果如下:

点击auth

查看api-docs

http://api.baidu.com:8003/api-docs/

如果谷歌浏览器安装了 JSON Formatter插件,效果就是下面的样子:

如果没有安装插件,就是一行展示

访问eureka注册中心

http://api.baidu.com:8001/

效果如下:

确保状态都是UP,前端页面会每隔1秒,检测一次后端服务。不正常时,显示为DOWN

本文参考链接:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# Author: Greg Lee Turnquist # Paperback: 264 pages # Publisher: Packt Publishing; 1st New edition edition (May 21, 2010) # Language: English # ISBN-10: 1849510660 # ISBN-13: 978-1849510660 Amazon 的介紹: http://www.amazon.com/Spring-Python-1-1-Greg-Turnquist/dp/1849510660 Spring Python captures the concepts of the Spring Framework and Spring Security and brings them to the world of Python and provides many functional parts to assemble applications. Spring Python is all about using the many useful features of Spring to the fullest and making these features available when working with Python. Get to grips with all of the concepts of Spring and apply these to the language and environment of Python to develop powerful applications for your own personal requirements. The book provides an introduction to Spring Python and steadily takes you towards the advanced features that this integration has to offer. Spring uses the Java programming language. Spring Python, the first Spring extension to go live, allows developers to make maximum use of Spring features in Python. This book starts off by introducing each of the core building blocks of Spring Python using real code examples and high-level diagrams. It explores the various concepts of Spring Python with the help of examples and case studies and focuses on vital Spring Python features to make the lives of Python and Java developers simple. The early chapters cover simple applications with simple operations including data access, and then subsequent chapters scale up to multi-node, secured, transactional applications stopping short of very advanced level complexity. This book will help you to scale your applications without having to add unnecessary complexity

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值