使用php开发微服务,注册到eureka中的解决方案:sidecar。
sidecar 介绍请见:https://blog.csdn.net/qq_32971807/article/details/53742783
POM文件
<groupId>com.ylm</groupId>
<artifactId>side_car_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>side_car_test</name>
<description></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>
启动类
@SpringBootApplication
@EnableSidecar
public class SideCarServerApplication {
public static void main(String[] args) {
SpringApplication.run(SideCarServerApplication.class, args);
}
}
YLM
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://eureka.tz.ylmo2o.com:20881/eureka/
sidecar:
port: 80
instance:
hostname: localhost
health-uri: http://${sidecar.instance.hostname}:${sidecar.port}/health.php
反馈php服务是否健康的代码
<?php
header('Content-type: application/json');
echo '{"status":"UP"}';
php rest接口
<?php
header('Content-type: application/json;charset=utf-8');
$arr = array('key'=>'啊啊');
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
java 服务消费者 接口
@FeignClient(value = "testPhp", configuration=FeignConfiguration.class)
public interface SideCarApiService {
@RequestLine("POST /hello.php")
HashMap<String,String> sayHello();
}
重头戏 PHP 调用注册中心的其他服务的接口
<?php
// Sidecar的端口号(本地调用)
$host = 'http://localhost:8203/';
// 注册到Eureka的服务名称
$javaServiceName = 'ucenter';
// 要调用服务的接口URL
$func = '/api/sysUser/getSysUserByLoginName?userLoginName=admin';
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $host.$javaServiceName.$func);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 1);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//执行命令
$data = curl_exec($curl);
var_dump($data);