php开发微服务注册到eureka中(使用sidecar)

使用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);

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值