社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
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
<?php
echo "php:product service!";
?>
4 配置nginx,让其8500端口执行这个productservice目录。
<?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>com.clevercode.biz</groupId>
<artifactId>productsidecar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>productsidecar</name>
<description>productsidecar desc</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
<relativePath/>
</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>
<dependencies>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</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
CleverCode是一名架构师,技术交流,咨询问题,请加CleverCode创建的qq群(架构师俱乐部):517133582。加群和腾讯,阿里,百度,新浪等公司的架构师交流。【架构师俱乐部】宗旨:帮助你成长为架构师!
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!