如何解决通过网关无法调用本地注册微服务 您所在的位置:网站首页 网关和注册中心一样吗 如何解决通过网关无法调用本地注册微服务

如何解决通过网关无法调用本地注册微服务

2024-07-07 21:56| 来源: 网络整理| 查看: 265

相信不少人在工作中使用spring cloud微服务架构进行开发一个微服务时往往只需要关注这一个微服务的业务功能开发,但在实际情况是各个微服务相互关联,让你没办法很好的单服务模块开发,因此本文就向大家分享一个在工作中使用公司内网服务器作为eureka注册中心和zuul网关,然后在开发时将本地微服务注册到服务器注册中心,通过网关路由微服务的方案。

一、确保服务器与本地网络互通

第一步没的说,网络通畅才是保证微服务调用正常的前提,各位攻城狮若不太能搞定这点,可以让公司的运维大佬帮忙解决。

搞定一个可以在本地与服务器上互ping一下,通了就没问题了,如果本地是windows的话,需要关闭公用网络的防火墙,否则zuul无法正常需本地的微服务通讯。

二、将eureka-server与zuul部署到服务器

在本地编写一个eureka-server和zuul,如何不知道怎么编写可以查看我其他文章。

1、搭建eureka-server

要注意的是这里的eureka-server的application.properties中一个点必须注意:不能使用eureka.instance.prefer-ip-address=false,需使用以下配置,并且不能添加spring.application.name这项配置

eureka.instance.prefer-ip-address=true eureka.instance.hostname= ${spring.cloud.client.ipAddress} eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}

有些小伙伴因为使用的spring cloud版本,在运行erueka-server时可能会报下述错误

Could not resolve placeholder 'spring.cloud.client.ipAddress' in value "${spring.cloud.client.ipAddress}"

我们只需要查看org.springframework.cloud.client.HostInfoEnvironmentPostProcessor这个类里面的一个方法就知道原因了

在spring-cloud较新的版本已经将spring.cloud.client.ipAddress改为spring.cloud.client.ip-address了,因此我们配置文件中对应修改即可。

eureka.instance.prefer-ip-address=true eureka.instance.hostname= ${spring.cloud.client.ip-address} eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}

此时我们就可以成功运行eureka-server了。

2、搭建zuul

创建zuul的步骤省略,我这里只提出需要注意的地方。

在开发工作中,我们为了方便前期开发工作需要在zuul中解决跨域问题,在zuul的入口函数中添加以下配置:

package com.harris.zuulserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; /** * @author HCJ */ @SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class ZuulServerApplication { public static void main(String[] args) { SpringApplication.run(ZuulServerApplication.class, args); } @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedMethod("*"); source.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(source); } }

同时在appliction.properties中配置

zuul.sensitive-headers=Access-Control-Allow-Origin zuul.ignored-headers=Access-Control-Allow-Origin,H-APP-Id,Token,APPToken

即可一次性解决开发过程中的跨域问题,无需在前端和各个微服务中再配置,若在微服务中再配置跨域问题,将会请求时冲突报错。

接下来我们像eureka-server配置一样进行配置修改:

spring.application.name=zuul-server server.port=13000 eureka.instance.prefer-ip-address=true eureka.instance.hostname= ${spring.cloud.client.ipAddress} eureka.instance.instance-id= ${spring.cloud.client.ipAddress}:${server.port} eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

此时zuul的主要配置就完成了。若各位需要增加鉴权、回退的功能自行添加即可。

3、打包jar部署至服务器

将spring boot项目打包为jar大家十分熟练了,若大家对在服务器部署jar有疑问的话,可以查看我的这边问题,内讲述windows、linux下后台部署jar的方法:windows、linux如何后台运行jar(并且显示进程名)

三、将本地微服务注册到服务器的eureka-server

这里需要特别注意的一点时,所有的微服务中的配置都不能使用eureka.instance.prefer-ip-address=false来使用主机名进行资产,都需要使用以下配置:

eureka.instance.prefer-ip-address=true eureka.instance.hostname= ${spring.cloud.client.ipAddress} eureka.instance.instance-id= ${spring.cloud.client.ipAddress}:${server.port}

否则在本地通过服务器的网关调用本地的微服务将会报如下错误:

{ "timestamp": "2019-03-28T06:06:14.909+0000", "status": 500, "error": "Internal Server Error", "message": "GENERAL" }

关于微服务的其余地方就没有区别了。在服务器运行了eureka-server和zuul后,正常启动本地微服务即可。

四、调用测试

访问服务器的eureka,可以看到之前部署的zuul(zuul-server)和本地运行的微服务t-asset。

接下来调用本地微服务的测试一下:

可以看到本地的微服务通过服务器的网关调用成功。

 

收编不易,转载注明来源,谢谢大家!

若有疑问欢迎评论留言。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有