Spring Clould Sidecar整合php接口服务
因公司业务需求,需要将php接口服务注册到eureka中,通过网关统一调用,所以需要使用sidecar实现该功能。
1、新建springboot项目,导入以下包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<groupId>com.xxxx</groupId>
<artifactId>portal_news_sidecar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>portal_news_sidecar</name>
<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-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-sidecar</artifactId>
</dependency>
</dependencies>
2、applicaiton.yml
spring:
application:
name: PORTAL-NEWS-SIDECAR #服务名
server:
port: 8070 #端口
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
lease-expiration-duration-in-seconds: 3
lease-renewal-interval-in-seconds: 3
preferIpAddress: true
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://127.0.0.1:3423/eureka
sidecar:
port: 9998 #异构微服务的端口
healthUri: http://192.168.100.15:9998/document/uri #第三方程序提供的标准health接口
ip-address: 192.168.100.15 #异构微服务的IP地址
sidecar.healthUri 说明
官方译文
3、在启动类中添加如下注解
@SpringBootApplication
@EnableEurekaClient
@EnableSidecar
@EnableFeignClients(basePackages = "com.ztgm.sidecar_server")
public class SidecarServerApplication {
public static void main(String[] args) {
SpringApplication.run(SidecarServerApplication.class, args);
}
}
以上代码即可实现将php接口服务注册到eureka中。
需要调用php接口直接通过我们服务的网关/php接口地址即可实现调用。
注:sidecar心跳速度慢,启动服务后需要等待大约二三十秒。