配置中心简介
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射,所以它除了适用于Spring构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行
配置中心原理
配置中心依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- main类添加
@EnableConfigServer
- Application.yml配置
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: file:///E:/workspace3/wk/wk_config
application:
name:CONFIGSERVER
客户端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
添加
spring:
application:
name: USERSERVICE
profiles:
active: dev
cloud:
config:
uri: http://localhost:8888
Zuul简介
路由是微服务架构的不可或缺的一部分。例如:”/” 可能映射到你应用主页,/api/users映射到用户服务,/api/shop映射到购物服务。Zuul。Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器 当一个UI应用想要代理调用一个或者多个后台服务的时候,Sping cloud创建了一个嵌入的Zuul proxy很方便的开发一个简单的案例。这个功能对于代理前端需要访问的后端服务非常有用,避免了所有后端服务需要关心管理CORS和认证的问题.
spring:
application:
name: zuultest
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/访问
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
* 注解
@EnableZuulProxy
配置注册中心
spring:
application:
name: zuultest
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
访问
http://zuul服务器ip:zuul服务端口/zuul/被调用服务id/被调用服务的方法