先起一個 Sidecar 服務,一個PHP服務一個應用,和PHP服務部署在同一臺機子,經過 localhost 訪問,這樣就解決了網絡開銷,至關於本地進程間調用php
Sidecar 服務比較簡單,html
一、這裏記錄下 maven 的配置java
<?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>cn.taxiong</groupId> <artifactId>tx_php_server_side_car</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>tx_php_server_side_car</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.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> <!--配置倉庫--> <repositories> <repository> <id>aliRepository</id> <name>aliRepository</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <!-- cloud --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-sidecar</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、sidecar 相關的配置,及註冊中心的配置laravel
spring: application: name: tx-php-server-sidecar #服務註冊中心端口號 server: port: 8203 #服務註冊中心實例的主機名、端口 #是否向服務註冊中心註冊本身 #是否檢索服務 #服務註冊中心的配置內容,指定服務註冊中心的位置 eureka: port: 8200 instance: hostname: localhost client: register-with-eureka: true fetch-registry: true serviceUrl: defaultZone: http://${eureka.instance.hostname}:${eureka.port}/eureka/ sidecar: port: 1215 instance: hostname: localhost health-uri: http://${sidecar.instance.hostname}:${sidecar.port}/health
三、服務啓動類 @EnableSidecarspring
四、還須要在PHP服務一個檢查服務的接口或文件, 注意響應頭類型 Content-Typeapache
以 laravel 框架爲例子,配置後的地址是 http://127.0.0.1:1215/healthjson
Route::get('health', function () { $content = '{ "status" : "UP" }'; $status = 200; $value = 'application/json; charset=utf-8'; return response($content, $status) ->header('Content-Type', $value); });
這是經過 Eureka 中心頁面,看見 Sidecar 已經註冊上去,注意若是 PHP 的 health 頁面的響應內容或者響應頭不對的話, Sidecar 服務的 UP 狀態會顯示 DOWN網絡
這裏是 PHP 服務的默認頁面app
這裏是經過 Spring Cloud gateway 轉發後的 PHP 服務頁面負載均衡
<?php /** * Created by PhpStorm. * User: liugx * Date: 2018/9/9 * Time: 下午12:57 */ namespace App\Http\Controllers; use App\Exceptions\BusinessException; use GuzzleHttp\Client; class TestController extends Controller { /** * @return \Psr\Http\Message\StreamInterface * @throws BusinessException */ public function test() { // Sidecar的端口號(本地調用) $host = 'http://localhost:8203'; // 註冊到Eureka的服務名稱 $javaServiceName = 'tx-java-server'; // 要調用服務的接口URL $func = '/'; // 經過Sidecar調用服務的URL $uri = "/{$javaServiceName}{$func}"; $base_uri = $host; $client = new Client([ // Base URI is used with relative requests 'base_uri' => $base_uri, 'timeout' => 3.0, ]); $params = []; try { $response = $client->request('GET', $uri, [ 'query' => $params ]); } catch (\GuzzleHttp\Exception\GuzzleException $ex) { throw new BusinessException("抱歉,網絡異常:{$ex->getMessage()}"); } return $response->getBody(); } }
直接用FeignClient作服務間調用