初识spring Cloud续(config与zuul组件)

统一配置中心

为什么需要统一配置中心?

统一配置中心顾名思义,就是将配置统一管理,配置统一管理的好处是在日后大规模集群部署服务应用时相同的服务配置一致,日后再修改配置只需要统一修改全部同步,不需要一个一个服务手动维护

统一配置中心的架构图:

1.配置中心服务器的开发

1.添加依赖
      <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-config-server</artifactId>
       </dependency>
2.添加注解支持
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class EurekaApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
3.在远程的仓库创建配置文件(yml,properties,yaml)
4.配置相关的配置文件
#注册到注册中心
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
#默认端口号 8888
server.port=8888
# 实例名
spring.application.name=config
#配置git的远程仓库   https 暂时不支持ssh
spring.cloud.config.server.git.uri=https://gitee.com/my_os_do_you_know/springcloud-config.git
5.启动配置中心服务

其中 -a拼接的是不同环境

http://localhost:8888/order-a.yml

http://localhost:8888/order-a.yaml

http://localhost:8888/order-a.properties

http://localhost:8888/order-a.json

以上四种访问方式都可以

 

扫描二维码关注公众号,回复: 4862313 查看本文章
6.服务的配置的访问规则

{name}-{profiles}.yml

{name}/{profiles:.[^-].}

{name}-{profiles}.json

{label}/{name}-{profiles}.yml

{name}/{profiles}/{label:.*}

{label}/{name}-{profiles}.properties

{label}/{name}-{profiles}.json

{name}/{profile}/{label}/**

{name}/{profile}/{label}/**

说明:

label: 分支名称 默认是master分支

name:文件的服务的名称(自定义的名称)

profiles:不同环境,访问指定环境的文件会自动合并主配置文件的信息,不加Profiles则默认访问主配置文件

config server会将Git上的文件下载一份到本地,日志中会有:

2.创建配置中心客户端

1.导入相关依赖
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-client</artifactId>
</dependency>
2.添加配置
#开启配置中心
spring.cloud.config.enabled=true
#找到配置中心实例
spring.cloud.config.discovery.service-id=CONFIG
#指定名字
spring.cloud.config.name=product
#指定环境
spring.cloud.config.profile=1
#指定分支
spring.cloud.config.label=master
#指定配置中心的uri
spring.cloud.config.uri=http://localhost:9999
3.注意将配置文件名修改为 bootstrap.yml

application的优先级较低,访问端口为8080,改为bootstrap优先级较高,会先去Git拉取信息

 

路由网关(zuul)

在微服务架构中,需要多个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能 路由、配置管理等,由这个基础组件相互协作,共同组建了一个简单的微服务系统。一个简单的微服务系统如下

注意:A服务和B服务是可以相互调用的,并且配置服务也是注册到服务注册中心的。 总结:在Spring Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求先先经过负载均衡(zuul、 Ngnix),再到达服务网关(zuul集群),然后再到具体的服务。服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件由配置服务管理,配置服务的配置文件仓库,方便开发人员随时改配置。

1.Zuul 简介

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服 务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能

1.搭建Zull
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
2.编写Zuul的入口类
 @EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceZuulApplication.class, args);
}
}
3.编写application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
server:
port: 8769
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /api-a/**
serviceId: service-ribbon
api-b:
path: /api-b/**
serviceId: service-feign
#关闭安全认证
management.security.enabled=false
#设置断路时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

访问config的配置文件

http://localhost:8767/config/product-1.json

 

猜你喜欢

转载自www.cnblogs.com/lhc-hhh/p/10252506.html