Springcloud Consolidated PHP, python examples
Code has been uploaded to: https://github.com/KeoZmy/SpringCloudDemo.git, the blog did not write to the gateway Zuul, but Git has uploaded the preface
Recently spent time studying micro services, various open source components combine a framework to the final decision to use springcloud such a mature framework. Have to say, Springcloud is indeed very strong, there are pivotal and Netfix is its strong backing and technical output. Finally, we chose the Springcloud Netfix as the core development.
The sidecar in Springcloud is inspired by the prana of Netfix, as the official reference says:
Do you have NON-JVM languages your want to take advantage of Eureka, Ribbon and Config Server? The Spring Cloud Netflix Sidecar is inspired by Netflix Prana. It includes a simple HTTP APIs to the instances (ie host and port) for a given service. You can also proxy service calls through a embedded Zuul proxy which gets its route entries from Eureka. The Spring Cloud Config Server can be accessed directly via host lookup or through the Zuul Proxy. The NON-JVM app should implement a health check so sidecar can and Eureka if the app is up or down
Do you have the ability to use Eureka, ribbon, and Config server for non-JVM language applications? Spring Cloud Netflix Sidecar is inspired by Netflix Prana. It contains a simple HTTP API to obtain all registered instance information (including host and port information). You can also invoke the proxy service via an embedded Zuul agent that relies on Eureka. The Spring Cloud Config server can be entered directly through the host lookup or Zuul agent. Non-JVM applications provide a health check implementation to allow Sidecar to Eureka synchronous applications up or down.
In short, a non-JVM program, such as: PHP, Python, and so on, want to register to Eureka, but the application is a bunch of other languages written, then I should how to achieve it. The principle of sidecar is to listen for the port that the application is running on, and then to detect the running state of the program, the official description will be more image:
Implement
This article is mainly to refer to the official given a combination of Python examples, examples of a springcloud combined with PHP, Python examples. PHP Ready
First prepare the environment for PHP, see:
Using Wamp to quickly build a PHP web development environment
PS: I have not done PHP related development, but recently participated in the restructuring of the company's projects, inside the Java code, but also has Python, PHP modules, and quickly built a PHP demo to do the test, if you are a professional PHP Developer of course you can choose your own way)
My wamp server www directory:
Health.json is a non-JVM application that provides an accessible URI that mimics the Springboot health check interface. It should return a JSON document similar to the following:
' {status ': ' Up '} '
By the way, I changed the server's port to 3000, and the default was 80.
How to modify Wamp default 80 port Python preparation (please feel free to Python great God)
This is mainly written a Python program to simulate the operation of Web applications, this application takes up a port of 5680
Import Httplib
from twisted.web Import server, resource from
twisted.internet import reactor, endpoints
Class Health (Resource. Resource):
isleaf = True
def render_get (self, request):
Request.setheader ("Content-type", "application/ JSON "") return
' {status ': ' Up '}\n '
class Fortune (resource. Resource):
isleaf = True
def render_get (self, request):
conn = Httplib. Httpconnection (' localhost ', 5678)
conn.request ("Get", "/fortunes")
res = Conn.getresponse ()
fortune = Res.read ()
request.setheader ("Content-type", "Text/plain") return
fortune
root = resource. Resource ()
root.putchild (' Health ', Health ())
root.putchild (", Fortune (
)) Endpoints.serverfromstring (reactor, "tcp:5680"). Listen (server. Site (Root))
Reactor.run ()
Eureka
Create a Springboot program that modifies the pom 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>org.test </groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> < Packaging>jar</packaging> <name>eureka</name> <description>demo Project for Spring Boot </description> <parent> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPR Ing-boot-starter-parent</artifactid> <version>1.2.5.RELEASE</version> <relativePath/> ;! --Lookup parent from repository--> </parent> <properties> <project.build.sourceencoding>utf-8& Lt;/project.build.sourceencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.bo Ot</groupid> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> & Lt;dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud- starter-eureka-server</artifactid> </dependency> <dependency> <GROUPID>ORG.SPRINGF
Ramework.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> <dependency>
;
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>release</version> </dependency> <dependency> <groupId>org.springframework< /groupid> <artifactId>spring-test</artifactId> <version>RELEASE</version> &
lt;/dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-p
Arent</artifactid> <version>Angel.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement > <build> <plugins> <plugin> <groupid>org.springframework.boot</gro upid> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugin S> </build> </project>
modifying application.properties configuration Files
server.port=8761
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
Finally, we add the annotation @enableeurekaserver to the eurekaapplication and open the Service registration center.
The project directory structure is as follows:
Configserver
Create a Springboot program that modifies the pom 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>org.test
</groupId> <artifactId>configserver</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>configserver</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifa Ctid>spring-boot-starter-parent</artifactid> <version>1.2.5.RELEASE</version> < Relativepath/> <!--lookup parent from repository--> </parent> <properties> <project.build.s Ourceencoding>utf-8</project.build.sourceencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.spring Framework.boot</groupid> <artifactId>spring-boot-starter-actuator</artifactId> </dependen cy> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>s Pring-cloud-config-server</artifactid> <exclusions> <exclusion> < Groupid>org.springframework.security</groupid> <artifactid>spring-security-rsa</artifact id> </exclusion> </exclusions> </dependency> <dependency> & Lt;groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid
> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.spring Framework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> <scope> test</scope> </dependency> <dependency> <groupid>org.springframework.boot</grou Pid> <artifactId>spring-boot-test</artifactId> <version>RELEASE</version> & lt;/dependency> <dependency> <groupId>org.springframework</groupId> <artifacti d>spring-test</artifactid> <version>RELEASE</version> </dependency> </dependenci es> <dependencyManagement> <dependencies> <dependency> <groupid>org.sp
Ringframework.cloud</groupid> <artifactId>spring-cloud-starter-parent</artifactId> <version>angel.sr3</version> <type>pom</type> <scope>import</scope> </de pendency> </dependencies> </dependencyManagement> <build> <plugins> <plugin > <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-maven-p
lugin</artifactid> </plugin> </plugins> </build> </project>
To modify the application.properties configuration file:
server.port=8888
Spring.cloud.config.server.git.uri=https://github.com/spencergibb/oscon-config-repo
There is also a configuration file bootstrap.properties:
server.port=5678
sidecar.port=3000
sidecar.health-uri=http://localhost:${sidecar.port}/phptest/ Health.json
The project directory is as follows:
Sidecar for PHP
Start writing a docking sidecar program for PHP applications
Again, this is a springboot program, we first rewrite the Pom.xml file
<?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>org.test </groupId> <artifactId>sidecar</artifactId> <version>0.0.1-SNAPSHOT</version> < Packaging>jar</packaging> <name>sidecar</name> <description>demo Project for Spring Boot </description> <parent> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPR Ing-boot-starter-parent</artifactid> <version>1.2.5.RELEASE</version> <relativePath/> ;! --Lookup parent from repository--> </parent> <properties> <project.build.sourceencoding>utf-8& Lt;/project.build.sourceencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.b
Oot</groupid> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud -starter-config</artifactid> </dependency> <dependency> <groupid>org.springframewo
Rk.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-bo ot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframewor
K.cloud</groupid> <artifactId>spring-cloud-netflix-sidecar</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</art ifactid> <scope>test</scope> </dependency> <dependency> <groupId> Org.springframework</groupid> <artifactId>spring-test</artifactId> <version>releas e</version> </dependency> <dependency> <groupid>org.springframework.boot</group id> <artifactId>spring-boot-test</artifactId> <version>RELEASE</version> &L
t;/dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-pa
Rent</artifactid> <version>Angel.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> & Lt;plugins> <plugin> <groupId>org.springframework.boot</groupId> < artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <
/project>
And then open the @enablesidecar in the program.
Finally, a critical step, we rewrite the application.properties file
server.port=5678
sidecar.port=3000
sidecar.health-uri=http://localhost:${sidecar.port}/phptest/ Health.json
server.port=5678 Specifies the port used by this sidecar runtime
SIDECAR.PORT=3000 specifies that sidecar listens on a non-JVM application's port, which is the server on which our PHP program is mounted, before I've changed the server port to 3000.
sidecar.health-uri=http://localhost:${sidecar.port}/phptest/ Health.json specified the PHP program returned to sidecar health indicator status, I am here through the return of the file, of course, you can also through the RESTAPI, if the program does not normally suspend, sidecar nature can not carry out health monitoring access Health.json (or health method), take no To up, so in the registration center will display a DOUWN state, indicating that the program is not a normal suspend, this is the idea of sidecar.
Project structure:
Sidecar for Python
Create a sidecar that is docked to Python, and the steps are the same as those created for PHP, except that the configuration files are slightly different
server.port=5688
sidecar.port=5680
sidecar.health-uri=http://localhost:${sidecar.port}/health
The first Python application we prepared uses 5680 ports, where health monitoring is done through the RESTAPI
Overall module:
Running Programs
As long as the PHP service and the Python service are turned on before two sidecar open.
The startup order for Java programs is (from left to right):
We visit http://localhost:8761/to see:
Two non-JVM language program applications have been successfully registered to Eureka and are up state
We turn off one of the Python services and we can see:
The PHP and Python services, like the Springboot program, are closed down and can be seen running in the registry. Summary
Sidecar's design is elegant, it does not invade the other application code. Like an agent, it always observes the operation of the application (its state of health), which is essentially registered to the Eureka is sidecar, but the state it displays is the application it monitors. As long as PHP, Python ... And so on to provide a health monitoring interface can be.
Code has been uploaded to: https://github.com/KeoZmy/SpringCloudDemo.git