spring cloud的使用(一)

记录下自己最近在项目中使用的spring cloud框架

spring cloud 是基于spring boot实现的微服务架构开发工具,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。

euerka

一、依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

二、application.yml配置

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false 
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka

三、通过 @EnableEurekaServer 应用程序可以作为一个独立的服务

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaApplication.class, args);
  }
}

其它需要注册进euerka的服务也需要加上@EnableEurekaServer

euerka+security
用security为eureka(注册中心)添加简单的用户认证
maven依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
</dependency>

配置文件

security:
  basic:
    enabled: true # 开启基于HTTP basic的认证,如果不设置这段内容,账号默认是user,密码就是一个随机值,该值会在启动的时候打印出来。
  user:
    name: 账号
    password: 密码
                                     #ereuka配置也需要做相应的修改
eureka:
  client:
    register-with-eureka: false      #是否向服务注册中心注册自己
    fetch-registry: false            #是否检索服务
    service-url:                     #服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone: http://账号:密码@localhost:8761/eureka #其它需要注册进注册中心的服务,也需要加上账号密码

zuul

zuul可作为服务器端的负载均衡
使用步奏:
一、依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

因为继承了父类 ,所以不需要配置jar的版本

二、application.yml配置

zuul:
  routes:
    api-a:#让zuul反向代理微服务,路径是/admin_user  api-a只要是唯一的就行,可随意写
      path: /admin_user/**
      service-id: zjlmb-user
      sensitiveHeaders:
    api-b:
      path: /merchant_user/**
      service-id: zjlmb-merchant
      ignoredHeaders:

pathservice-id表示,/admin_user/开头的url请求,将转发到zjlmb-user这个微服务上,/merchant_user/开头的url请求,将转发到zjlmb-merchant这个微服务上。
sensitiveHeaders会过滤客户端附带的headers
ignoredHeaders会过滤服务之间通信附带的headers

例子一(zuul服务的端口为8080):
除了myuser开头的url,其它url全部不通过,且myuser开头的url访问的是serviceid为zjlmb-user的服务
http://localhost:8080/zjlmb-merchant/test/hello (404)
http://localhost:8080/myuser/test/hello (200)

zuul:
  ignoredServices: '*'
  routes:
    zjlmb-user: /myuser/** #zjlmb-user必须是一个服务id

或者

zuul:
  ignoredServices: '*'
  routes:
    api: #api只要是唯一的就行,可随意写
      path: /myuser/**
      serviceId: zjlmb-user

上面俩种写法效果一样
三、通过 @EnableZuulProxy 应用程序可以作为一个独立的服务

注意:zuul不包含服务发现的客户端,所以想要基于ServiceID去路由的话,需要提供一个服务发现的客户端(discovery client),例如 eureka

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
  public static void main(String[] args) {
    SpringApplication.run(ZuulApplication.class, args);
  }
}

zuul+eureka
不需要在zuul项目启动器加上@EnableEurekaClient注解,在@EnableZuulProxy

@EnableCircuitBreaker
@EnableDiscoveryClient
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ZuulProxyConfiguration.class)
public @interface EnableZuulProxy {
}

当然也需要在配置文件中将zuul注册到eureka中

spring:
  application:
    name: zjlmb-zuul
eureka:
  client:
    serviceUrl:
      defaultZone: http://账号:密码@localhost:8761/eureka
  instance:
    prefer-ip-address: true
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值