Spring Cloud:第五章:Zuul服务网关
快速入门
定义user,order,pay服务,定义zull服务网关服务都注册到eureka服务上,
通过一下接口访问user,order,pay的服务,
http://localhost:7070/pay/index
http://localhost:8080/user/index
http://localhost:9090/order/index
定义服务网关服务zuul,我们看看其相关配置,zuul-service加入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
配置文件:
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id:
s
p
r
i
n
g
.
a
p
p
l
i
c
a
t
i
o
n
.
n
a
m
e
:
{spring.application.name}:
spring.application.name:{spring.cloud.client.ipAddress}:KaTeX parse error: Expected '}', got 'EOF' at end of input: …on.instance_id:{server.port}}
prefer-ip-address: true
server:
port: 6069
定义启动类:
package com.zhihao.miao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
//使用@EnableZuulProxy注解开启zuul的api网关服务功能
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class,args);
}
}
然后可以通过服务网关进行访问上面的三个服务接口,
http://localhost:6069/pay-service/pay/index
http://localhost:6069/user-service/user/index
http://localhost:6069/order-service/order/index
注意:
默认的zuul结合eureka会将注册到eureka的服务名作为访问的ContextPath。
更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120382551
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?