1 java访问java:
java访问 java,是通过InstanceInfo instance = discoveryClient.getNextServerFromEureka(“ORDER-SERVICE”, false),从 注册中心直接找到对应的服务,直接访问。得到url服务:http://10.4.13.140:8003
2 java访问 php:
java访问 java,是通过InstanceInfo instance = discoveryClient.getNextServerFromEureka(“PRODUCT-SIDECAR”, false),从 注册中心直接找到对应的服务,直接访问。得到url服务:http://10.4.13.140:8500,这里注意拿到的是php微服务的端口,不是sidecar的端口2001。
3 php访问 java
浏览器中 http://10.4.13.140:2001/user-service/user/index ,(或者$ curl http://10.4.13.140:2001/user-service/user/index -H ‘Host: localhost’ )这里特别注意user-service必须全部小写,user-service是serviceId,否则会报错。同时serviceId不能是自己 ,比如http://10.4.13.140:2001/product-sidecar/index.php。访问自己也会报错。
这里其实 是把请求发给了 sidecar,sidecar根据serviceId去注册中心 找 服务,然后发起代理请求。
2 新建health.json文件
{
"status":"UP"
}
3 新 建立 一个index.php
echo "php:product service!";
?>
4 配置nginx,让其8500端口执行这个productservice目录。
<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.0modelVersion>
<groupId>com.clevercode.bizgroupId>
<artifactId>productsidecarartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>productsidecarname>
<description>productsidecar descdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.4.4.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-netflix-sidecarartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Brixton.SR4version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
2 配置application.yml
server:
port: 2001
spring:
application:
name: product-sidecar
sidecar:
port: 8500
health-uri: http://localhost:8500/health.json
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
3 ProductsidecarApplication
package com.clevercode.biz.productsidecar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.sidecar.EnableSidecar;
@EnableSidecar
@SpringBootApplication
public class ProductsidecarApplication {
public static void main(String[] args) {
SpringApplication.run(ProductsidecarApplication.class, args);
}
}
2 查看 sidecar信息
http://10.4.13.140:2001/
http://10.4.13.140:2001/hosts/product-sidecar
3 php 调用 java微服务
浏览器中 http://10.4.13.140:2001/user-service/user/index ,这里特别注意user-service必须全部小写,否则会报错。同时serviceId不能是自己 ,比如http://10.4.13.140:2001/product-sidecar/index.php。访问自己也会报错。
这里其实 是把请求发给了 sidecar,sidecar根据serviceId去注册中心 找 服务,然后发起代理请求。
3 java访问 php
InstanceInfo instance = discoveryClient.getNextServerFromEureka(“PRODUCT-SIDECAR”, false);
拿到PRODUCT-SIDECAR的服务的接口 ,其实是http://10.4.13.140:8500/,而不是2001的 sidecar端口。
http://10.4.13.140:8001/user/discoveryphp
https://github.com/cj58/springcloud/tree/master/productsidecar