欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 03-05、SpringCloud第五章,路由网管Zuul和分布式配置中心Config

03-05、SpringCloud第五章,路由网管Zuul和分布式配置中心Config

2025/6/6 15:32:52 来源:https://blog.csdn.net/pilot_speed/article/details/144029379  浏览:    关键词:03-05、SpringCloud第五章,路由网管Zuul和分布式配置中心Config

SpringCloud从看不懂到放弃,第五章

一、zuul路由网关

1、zuul概述

	简述:外部接口的统一访问网关.Zuul包含了对请求的路由和过滤两个最主要的功能:其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。注意:Zuul服务最终还是会注册进Eureka提供=代理+路由+过滤三大功能

2、zuul基本配置

1》、新建cloud-zuul-9527 module

2》、POM

zuul服务也是要整合进eureka中的

<dependencies><!-- zuul路由网关 --><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><!-- actuator监控 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--  hystrix容错--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- 日常标配 --><dependency><groupId>com.lee</groupId><artifactId>cloud-api</artifactId><version>${project.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- 热部署插件 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies>

3》、YML新增

server:port: 9527spring:application:name: cloud-zuul-gatewayeureka:client:service-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eurekainstance:instance-id: gateway-9527.comprefer-ip-address: trueinfo:app.name: cloudauthor.name: leeapp.function: 网关build.artifactId: $project.artifactId$build.version: $project.version$

4》、HOST配置修改

127.0.0.1   zuul9527.com

5》、主启动类

package com.lee.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy
@SpringBootApplication
public class Zuul_9527_App {public static void main(String[] args) {SpringApplication.run(Zuul_9527_App.class,args);}
}

测试:

①、启动3个eureka集群
②、启动cloud-provider-dept-8001服务提供者
③、一个路由cloud-zuul-9527
④、访问:不用路由:http://localhost:8001/dept/get/1启用路由:http://zuul9527.com:9527/cloud-dept/dept/get/1注意:zuul和feign或ribbon没有多大关系。feign、ribbon http://localhost7001/consumer/xxx之类的访问和zuul.com:9521/cloud-dept/xxxx访问没有关系。在实际项目中feign\ribbon这些服务的消费方一般也是以服务的提供发而存在的。

3、zuul路由访问映射规则

3.1>、原因:

before:
http://zuul9527.com:9527/cloud-dept/dept/get/1访问路径中cloud-dept微服务名称容易暴露出来,所以我们要将其隐藏置换。

修改:YML

 zuul: routes: mydept.serviceId: microservicecloud-dept  #微服务实例名称mydept.path: /mydept/**		#映射名称

测试:

http://zuul9527.com:9527/mydept/dept/list

3.2>、原因:

before:
http://zuul9527.com:9527/cloud-dept/dept/get/1  还能看到,上面这一步只做了置换,没做隐藏

修改YML

忽略单个:
zuul: ignored-services: cloud-dept
忽略多个:
zuul: ignored-services: "*"

测试:

http://zuul9527.com:9527/cloud-dept/dept/get/1

3.3>、原因:

设置一个公共的前缀名

修改YML

zuul: prefix: /lee

测试:

http://zuul9527.com:9527/lee/mydept/dept/list如果这样访问不了,可能是yml的问题,在POM文件中引入
<dependency><groupId>org.yaml</groupId><artifactId>snakeyaml</artifactId><version>1.25</version>
</dependency>
即可

二、分布式配置中心

1、概述

原因:微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理....../(ㄒoㄒ)/~~是什么 
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。怎么玩
SpringCloud Config分为服务端和客户端两部分。服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
springcloud config 分布式配置中心能干嘛:1》、集中管理配置文件
2》、不同环境不同配置,动态化的配置更新,分环境部署:比如dev/test/prod/beta/release
3》、运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉去配置自己的信息
4》、当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
5》、将配置信息以rest接口的形式暴露

2、与Gitee码云通讯

2.1》、config server服务端配置

①、先在gitee上新建一个cloud-config的新repository
②、获得SSH协议的Git地址
③、本地硬盘目录上新建Git仓库并clonegit clone xxxxxxxxxxxxxxxxxxxxxxxxxxx.git
④、新建一个application.yml(文件内容保存的格式一定要UTF-8)spring:profiles:active:- dev---spring:profiles: dev     #开发环境application: name: cloud-config-lee-dev---spring:profiles: test   #测试环境application: name: cloud-config-lee-test#  请保存为UTF-8格式⑤、将上yml文件推送到gitee上git status #查看状态git add . git commit -m "init file"git push origin master⑥、新建module模块 cloud-config-3344配置中心模块
⑦、POM<dependencies><!-- springCloud Config --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback  --><dependency><groupId>org.eclipse.jgit</groupId><artifactId>org.eclipse.jgit</artifactId><version>4.10.0.201712302008-r</version></dependency><!-- 图形化监控 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 熔断 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- 热部署插件 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies> ⑧、yml
server: port: 3344 spring:application:name:  cloud-configcloud:config:server:git:uri: https://gitee.com/night_wish/cloud-config.git #Gitee上面的git仓库名字httpsusername: xxxxxpassword: ooooo⑨、主启动类import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication
@EnableConfigServer
public class Config_3344_App
{public static void main(String[] args){SpringApplication.run(Config_3344_App.class,args);}
}⑩、修改HOST
127.0.0.1 config3344.com
①①、启动config 测试是否可以从gitee上获取配置内容http://config3344.com:3344/application-dev.ymlhttp://config3344.com:3344/application-test.ymlhttp://config3344.com:3344/application-xxxxx.yml或http://config3344.com:3344/application/dev/masterhttp://config3344.com:3344/application/test/masterhttp://config3344.com:3344/application/xxxxx/master

2.2》、config client客户端的配置

①、在config server本地目录下创建cloud-config-client.yml
spring:profiles:active:- dev
---
server: port: 8201 
spring:profiles: devapplication: name: cloud-config-client
eureka: client: service-url: defaultZone: http://eureka-dev.com:7001/eureka/   
---
server: port: 8202 
spring:profiles: testapplication: name: cloud-config-client
eureka: client: service-url: defaultZone: http://eureka-test.com:7001/eureka/②、将cloud-config-client.yml提交到gitee中
git status
git add .
git commit -m "cloud-config-client"
git push origin master③、新建module cloud-config-client-3355
④、POM
<dependencies><!-- SpringCloud Config客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency> <dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies>⑤、bootstrap.yml
spring:cloud:config:name: cloud-config-client #需要从gitee上读取的资源名称,注意没有yml后缀名profile: dev   #本次访问的配置项label: master   uri: http://config3344.com:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取Gitee的服务地址⑥、application.yml
spring:application:name: cloud-config-client
⑦、修改hosts文件
127.0.0.1  configClient3355.com⑧、新建controller,验证client端能否通过config server从gitee上读取配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConfigClientRest {@Value("${spring.application.name}")private String applicationName;@Value("${eureka.client.service-url.defaultZone}")private String eurekaServers;@Value("${server.port}")private String port;@RequestMapping("/config")public String getConfig(){String str = "applicationName: "+applicationName+"\t eurekaServers:"+eurekaServers+"\t port: "+port;System.out.println("******str: "+ str);return "applicationName: "+applicationName+"\t eurekaServers:"+eurekaServers+"\t port: "+port;}
}⑨、主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ConfigClient_3355_App
{public static void main(String[] args){SpringApplication.run(ConfigClient_3355_App.class,args);}
}⑩、测试
启动config配置中心3344微服务  并自测http://config3344.com:3344/application-dev.yml
启动config客户端3355  并自测:如果profile:dev   http://configClient3355.com:8201/config如果profile:test  http://configClient3355.com:8202/config

BOOTSTRAP.YML是什么

applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高Spring Cloud会创建一个`Bootstrap Context`,作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootstrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。`Bootstrap`属性有高优先级,默认情况下,它们不会被本地配置覆盖。 `Bootstrap context`和`Application Context`有着不同的约定,
所以新增了一个`bootstrap.yml`文件,保证`Bootstrap Context`和`Application Context`配置的分离。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词