標籤:.com response pac localhost aliyun version instance prope 還需
先起一個 Sidecar 服務,一個PHP服務一個應用,和PHP服務部署在同一台機子,通過 localhost 訪問,這樣就解決了網路開銷,相當於本地進程間調用
Sidecar 服務比較簡單,
1、這裡記錄下 maven 的配置
<?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>
View Code
2、sidecar 相關的配置,及註冊中心的配置
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
3、服務啟動類 @EnableSidecar
4、還需要在PHP服務一個檢查服務的介面或檔案, 注意回應標頭類型 Content-Type
以 laravel 架構為例子,配置後的地址是 http://127.0.0.1:1215/health
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 服務的預設頁面
這裡是通過 Spring Cloud gateway 轉寄後的 PHP 服務頁面
SpringCloud初體驗:五、Sidecar 將 PHP 這類非 Java 生態語言的服務接入 Spring Cloud