前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springcloud(四)-zuul网关与config统一配置(慕课网廖师兄SpringCloud微服务实战)

springcloud(四)-zuul网关与config统一配置(慕课网廖师兄SpringCloud微服务实战)

作者头像
Meet相识
发布2018-09-12 16:34:38
2.5K0
发布2018-09-12 16:34:38
举报
文章被收录于专栏:技术专栏技术专栏

统一配置

1.为什么需要统一配置中心

  • 不方便维护
  • 配置内容安全与权限
  • 更新项目配置需要重启

2.统一配置-服务端

  • 核心依赖
代码语言:javascript
复制
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-config-server</artifactId>
       </dependency>
  • 启动类

@EnableConfigServer 开启统一配置服务端

代码语言:javascript
复制
/**
 * Eureka 统一配置服务端启动类
 * @author gaowenfeng
 * 启用Eureka客户端
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class MicroWeatherConfigServerApplication {


    public static void main(String[] args) {
        SpringApplication.run(MicroWeatherConfigServerApplication.class, args);
    }
}
  • 配置
代码语言:javascript
复制
spring:
  application:
    name: micro-weather-config-server
  cloud:
    config:
      server:
        git:
          ## 统一配置的仓库
          uri: https://github.com/MarkGao11520/spring-cloud-repo/
          username: xxxx
          password: xxxx
          # 拉取下来的文件防止位置
          basedir: /Users/gaowenfeng/project/idea/springcloud_sell/config/basedir
          ## 仓库路径
          search-paths: config-repo

eureka:
    # 服务的url
    service-url:
      defaultZone: http://localhost:8761/eureka/

server:
  port: 8888

3.统一配置-客户端

  • 核心依赖
代码语言:javascript
复制
 <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-config-client</artifactId>
       </dependency>
  • 配置

1.这里如果配置文件名称为application.xxx的话,会搞乱顺序,先找配置中心再找注册中心,这样会找不到,会导致去加载默认的8888,

  1. eureka.service-url的配置需要在本地,不然会找不到注册中心
代码语言:javascript
复制
# 配置规则
#所以应该叫bootstrap.yml

# /{application}/{profile}[/{label}] 
# /{application}-{profile}.yml
# /{application}-{profile}.properties
# /{label}/{application}-{profile}.yml 
# /{label}/{application}-{profile}.properties 
spring:
  application:
    # 配置规则里的{application}
    name: micro-weather-config-client
  cloud:
    config:
      # 找到配置的方式一
      # uri: http://localhost:8888
      # 配置规则里的${profile}
      # profile: bus-dev
      # 配置规则里的${label},label 是分支,不是路径!
      # label: test

      # 找到配置的方式二
      discovery:
        enabled: true
        service-id: CONFIG
      profile: test
eureka:
    # 服务的url
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • 测试
代码语言:javascript
复制
@RunWith(SpringRunner.class)
@SpringBootTest
public class MicroWeatherConfigClientApplicationTests {

    @Value("${version}")
    private String version;

    @Test
    public void contextLoads() {
        Assert.assertEquals("1.0",version);
    }

}

Bus 自动更新理论

image.png

1.添加依赖
代码语言:javascript
复制
rabbitmq 的bus依赖添加
config server 和client的版本应该对应起来才可以
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
2.将bus-refresh接口暴露出去
代码语言:javascript
复制
## 变量名可能会变,利用自动提示可以找到对应的正确配置变量 (输入management.endpoints 找到默认包含一个[health,info]数组的变量,修改为"*")
management:
  endpoints:
    web:
      暴露所有接口
      expose: "*"
3.修改使用配置的客户端的地方
代码语言:javascript
复制
@RestController
@RequestMapping("/env")
@RefreshScope // 加入这个注解自动刷新配置
public class EnvController {

    @Value("${env}")
    private String env;

    @GetMapping("/print")
    public String print() {
        return env;
    }
}
}

@Data
@Component
@ConfigurationProperties("girl")
@RefreshScope // 加入这个注解自动刷新配置
public class GirlConfig {

    private String name;

    private Integer age;
}
4.使用

修改git的配置以后,POST 请求访问 "http://192.161.4:8080/actuator/bus-refresh" 即可(这个路径可能会变,具体请查阅启动日志)

5.集成WebHooks实现动态更新

在GitHub或者其他git工具上配置webhook,然后就可以实现,在git更新的时候,发送请求给我们的config服务器让其刷新配置

image.png

Zuul网关

  • 核心依赖
代码语言:javascript
复制
           <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-zuul</artifactId>
       </dependency>
  • 启动类

@EnableZuulProxy 开启网关

代码语言:javascript
复制
/**
 * Eureka 客户端启动类
 * @author gaowenfeng
 * 启用Eureka客户端
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class MicroWeatherEurekaClientApplication {


    public static void main(String[] args) {
        SpringApplication.run(MicroWeatherEurekaClientApplication.class, args);
    }
}
  • 配置
代码语言:javascript
复制
spring:
  application:
    name: micro-weather-eureka-client

eureka:
    # 服务的url
    service-url:
      defaultZone: http://localhost:8761/eureka/
zuul:
  routes:
    # 将city的请求转发到应用msa-weather-city-server
    city:
      path: /city/**
      serviceId: msa-weather-city-server
    # 将data的请求转发到应用msa-weather-data-server
    data:
      path: /data/**
      serviceId: msa-weather-data-server
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.02.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 统一配置
    • 1.为什么需要统一配置中心
      • 2.统一配置-服务端
        • 3.统一配置-客户端
          • Bus 自动更新理论
            • Zuul网关
            相关产品与服务
            微服务引擎 TSE
            微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档