spring-cloud-config 配置中心及Zuul路由

写在前面

  • Spring Cloud Config是Spring创建的一个全新的服务端/客户端项目,为应用提供分布式的配置存储,提供集中化的外部配置支持。它除了适用于Spring构建的应用程序外,也可以在其他语言运行的应用程序中使用。

  • Spring Cloud Config分为服务端和客户端两部分。其中服务端称为分布式配置中心,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息的功能;客户端则是微服务架构中的各个微服务应用,它们通过指定的配置中心来管理与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。

  • Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,但也提供了对其他存储方式的支持,比如SVN仓库、本地文件系统。在本例中构建了一个基于Git存储的分布式配置中心,并在客户端中演示了如何制定应用所属的配置中心,并能够从配置中心获取配置信息的整个过程。

搭建git服务器

测试配置中心环境

  • 依赖管理
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
  • main 方法
@SpringBootApplication
@EnableConfigServer
public class ConfigMain {
    public static void main(String[] args) {
        SpringApplication.run(ConfigMain.class,args);
    }
}
  • application.yml 文件
    这里路径指向git仓库(配置中心)
server:
 port: 8888
spring:
 cloud:
  config:
   server:
    git:
      uri: https://github.com/用户名/configcentre
  • 将uservice部署到GitHub仓库中
spring:
  application:
    name: uservice
  profiles:
    #环境切换
    active: dev
    # 配置中心 的ip和端口
    # 这个文件等价于 http://localhost:8888/uservice-dev.yml
  cloud:
    config:
      uri: http://localhost:8888
	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
@SpringBootApplication
//启用微服务客户端
@EnableEurekaClient
@RestController
public class UserServiceMain {
        public static void main(String[] args) {
            new SpringApplicationBuilder(UserServiceMain.class).web(true).run(args);
        }

}

测试

  • 注册中心→配置中心→UserServiceMain → 依次启动
    在这里插入图片描述
    在这里插入图片描述

  • 运行localhost:8888/uservice-dev.yml
    效果如下
    在这里插入图片描述

  • 测试properties后缀,后缀不一样,格式不一样。
    在这里插入图片描述

zuul

  • 依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
  • main方法
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulMain {
    public static void main(String[] args) {
        SpringApplication.run(ZuulMain.class);
        //new SpringApplicationBuilder(ZuulMain.class).web(true).run(args);
    }
}
  • application.yml文件
    将zuul部署到配置中心
eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://192.168.229.129:8761/eureka/
spring:
  application:
    name: zuul

server:
  port: 88
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值