共计 2281 个字符,预计需要花费 6 分钟才能阅读完成。
springcloud-zuul-gateway
pom 文件新增依赖:
<!--SpringCloud 整合 config-client--><dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-config-client</artifactId> | |
</dependency> | |
<!--actuator 监控核心 --> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-actuator</artifactId> | |
</dependency> |
批改 application.yml 配置文件:
## api 网关端口号 | |
server: | |
port: 80 | |
## 服务注册名称 | |
spring: | |
application: | |
name: server-zuul | |
cloud: | |
config: | |
#### 读取版本环境 | |
profile: dev | |
discovery: | |
#### config server 服务注册别名 | |
service-id: config-server | |
#### 开启读取权限 | |
enabled: true | |
eureka: | |
client: | |
service-url: | |
## 以后服务注册到 Eureka 服务地址 | |
defaultZone: http://localhost:8100/eureka,http://localhost:9100/eureka | |
register-with-eureka: true | |
## 须要检索服务信息 | |
fetch-registry: true | |
#### 配置动静 | |
#zuul: | |
# routes: | |
# ## 示意定义转发服务规定 | |
# api-a: | |
# ### 当客户端发送申请 http://127.0.0.1:80/api-member 结尾的,都会转发到会员服务 | |
# path: /api-member/** | |
# ### 会员服务别名 zuul 默认整合 ribbon,主动实现轮询成果 | |
# serviceId: app-member | |
# api-b: | |
# ### 当客户端发送申请 http://127.0.0.1:80/api-order 结尾的,都会转发到订单服务 | |
# path: /api-order/** | |
# ### 订单服务别名 zuul 默认整合 ribbon,主动实现轮询成果 | |
# serviceId: app-order | |
#### 开启所有端点 | |
management: | |
endpoints: | |
web: | |
exposure: | |
include: "*" |
正文掉的局部,新增 yml 文件到 Getee 上
AppGateway 启动类新增 zuul 配置
package com.baba.wlb; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.cloud.context.config.annotation.RefreshScope; | |
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; | |
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; | |
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; | |
/** | |
* @Author wulongbo | |
* @Date 2021/1/28 11:52 | |
* @Version 1.0 | |
*/@SpringBootApplication | |
@EnableEurekaClient | |
@EnableZuulProxy | |
public class AppGateway { | |
//@EnableZuulProxy 开启网关代理 | |
public static void main(String[] args) {SpringApplication.run(AppGateway.class, args); | |
} | |
// zuul 配置可能应用 config 实现施行更新 | |
@RefreshScope | |
@ConfigurationProperties("zuul") | |
public ZuulProperties zuulProperties(){return new ZuulProperties(); | |
} | |
} |
启动我的项目
顺次启动 Eureka Server、Config Server、Zuul 网关 gateway, 以及 Member 或者 Order 服务
Config Server 服务
分布式配置核心 config server 验证 gitee 上的配置文件可能正确读取,拜访:http://localhost:8888/server-zuul-dev.yml
网关拜访
zuul 网关拜访:http://localhost/api-member/getMember?name=iswulongbo&userToken=cloud
验证动静读取到了 gitee 上的配置文件,要动静批改或者动静新增服务到 zuul 网关,只需在 gitee 的配置文件中增加完后手动调用接口刷新或者通过集成 bus 音讯总线就 OK!这里不再阐明。
正文完