1.环境准备,这里第三方程序采用nodejs为例哈:::
2.用nodejs编写两个接口:
健康心跳检测接口(这个是必须的,返回数据格式也是固定的):
假设为:http://localhost:7001/health
返回数据必须是:{“status”:“UP”}
**********************************************
第二个接口:测试用的:
http://localhost:7001/helloNode
返回数据假设直接就是:hello nodejs
然后启动nodejs程序…
3…编写基于springboot的springcloud相关模块:
编写sidecar模块(用于将nodejs的API引入springcloud生态圈)
springcloud的相关依赖:
org.springframework.cloud
spring-cloud-netflix-sidecar
2.1.2.RELEASE
org.springframework.cloud
spring-cloud-starter-eureka
1.4.7.RELEASE
******************************************************************
application.yml配置文件:
spring:
application:
name: sidecar-server #服务名
freemarker:
prefer-file-system-access: false
server:
port: 8081
eureka: #加入到eureka
client:
register-with-eureka: true #禁用客户端注册行为
fetch-registry: true #禁用客户端注册行为
service-url:
defaultZone: http://localhost:8080/eureka
instance:
prefer-ip-address: true
sidecar这个服务必须和非jvm语言的服务在同一台主机上面,
#也就是说他们之间是localhost访问的,不能是ip访问等等
sidecar:
port: 7001 #异构微服务的端口
healthUri: http://localhost:7001/health #对应第三方程序的health接口
启动类:
@SpringBootApplication
@EnableSidecar
@EnableEurekaClient
public class SidecarApplication {
public static void main(String[] args) {
SpringApplication.run(SidecarApplication.class);
}
}
然后启动sidecar模块
如果没错误,访问http://localhost:8080会出现sidecar的主页面
4.编写eureka注册中心模块:
maven依赖:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.0.2.RELEASE
application.yml文件:
server:
port : 8080
eureka:
instance:
hostname : localhost
client:
registerWithEureka : false #这个配置表示是否将其本身注册到eureka server以被其他发现,因为其自身就是server,所以无需设置为true
fetchRegistry : false #这个配置表示是否需要从eureka server中抓取eureka上的注册信息
serviceUrl:
defaultZone : http://
e
u
r
e
k
a
.
i
n
s
t
a
n
c
e
.
h
o
s
t
n
a
m
e
:
{eureka.instance.hostname}:
eureka.instance.hostname:{server.port}/eureka/
spring:
freemarker:
#template-loader-path: classpath:/templates/
prefer-file-system-access: false
application:
name: eureka-server
*******************************************************************
启动类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
然后启动它,并且访问它的主页面:http://localhost:8080
5.编写zuul模块:
maven依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-zuul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
************************************
配置文件:
server.port= 8085
spring.application.name=zuul-server
eureka.client.service-url.defaultZone: http://localhost:8080/eureka/
zuul.routes.api.path=/api/**
zuul.routes.api.serviceId=sidecar-server
启动类:
@SpringBootApplication
@EnableZuulProxy
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class);
}
}
然后启动它:
然后在sidecar主页面可以看到ping,health的链接;点击health可以看到{“status”:“UP”}
并且在eureka主页面可以看到sidecar和zuul模块注册的信息;
然后就是正文了:
1.通过zuul访问nodejs接口:
http://localhost:8085/api/hello
2.通过zuul访问Java接口:
http://localhost:8085/javaservicename/hello
注意:javaservicename是Java应用注册到eureka上面的名称;
源代码参见附件