微服务开发入门

spring-cloud-example

微服务开发
git源码地址

1.微服务配置中心(config)和注册中心(eureka)

2.登录注册接口实例

3.token生成与校验

微服务
  • 单一职责:每一个微服务应该是单一独立模块的功能,一个微服务解决一个业务问题(并非只一个接口),这就呈现了“微”的体现。
  • 面向服务:每个服务将自己业务功能进行封装并暴露对外提供服务,服务与服务之间可以相辅相成的作用。
配置中心
配置中心
微服务架构
微服务架构

微服务搭建

1. 创建Maven父项目spring-cloud-examples,用于管理项目依赖包版本。pox.xml配置

  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.4.RELEASE</version>
  </parent>
  ...
  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-dependencies</artifactId>
              <version>${spring.cloud.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>

2. 配置中心

  • 在spring-cloud-examples项目下创建子项目spring-cloud-example-config,并添加spring-cloud-config-server核心依赖包。pox.xml配置
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
  spring:
    application:
      name: spring-cloud-example-config
    profiles:
      active: native #启用本地配置文件
    cloud:
      config:
        server:
          native:
            search-locations: classpath:/configs/ #配置文件扫描目录

  server:
    port: 8000 #服务端口
  • 启动类ConfigApplication添加注解@EnableConfigServer,通过启用Config Service服务
  @SpringBootApplication
  @EnableConfigServer
  public class ConfigApplication {
    public static void main(String[] args) {
      // TODO Auto-generated method stub
      SpringApplication.run(ConfigApplication.class, args);
    }

  }

3. 注册中心

  • 在spring-cloud-examples项目下创建子项目spring-cloud-example-register,并添加spring-cloud-netflix-eureka-server和spring-cloud-starter-config核心依赖包。pox.xml配置
  • 在项目中的resources目录下添加bootstrap.yml
  spring:
    cloud:
      config:
        name: spring-cloud-example-registry #配置文件名称,多个通过逗号分隔
        uri: http://localhost:8000 #Config Server服务地址
  spring:
  application:
    name: spring-cloud-example-registry

  # Eureka相关配置
  eureka:
    client:
      register-with-eureka: false #不注册服务
      fetch-registry: false #不拉去服务清单
      serviceUrl:
        defaultZone: http://localhost:${server.port}/eureka/ #多个通过英文逗号分隔

  server:
    port: 8001
  • 启动类EurekaApplication添加注解@EnableEurekaServer通过启用Eureka Server服务。
  @SpringBootApplication
  @EnableEurekaServer
  public class EurekaApplication {
    public static void main(String[] args) {
      // TODO Auto-generated method stub
      SpringApplication.run(EurekaApplication.class, args);
    }
  }

搭建业务服务A(B、C、D...)

  • 在spring-cloud-examples项目下创建子项目spring-cloud-example-a(b、c、d...),并在相应的服务中添加相应的pox.xml,以下以服务A为案例:
  <!-- Eureka Client Starter -->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <!-- Config Client Starter -->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  ...
  spring:
    cloud:
      config:
        name: spring-cloud-example-biz-a #配置文件名称,多个通过逗号分隔
        uri: http://localhost:8000 #Config Server服务地址
  • 在spring-cloud-example-config项目中的resources添加configs目录,并且添加一个服务配置文件spring-cloud-example-a.yml
  spring:
  application:
    name: biza

  server:
    port: 8010

  # Eureka相关配置
  eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:8001/eureka/
    instance:
      lease-renewal-interval-in-seconds: 10      # 心跳时间,即服务续约间隔时间(缺省为30s)
      lease-expiration-duration-in-seconds: 60  # 发呆时间,即服务续约到期时间(缺省为90s)
      prefer-ip-address: true
      instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

搭建服务网关

  • 在spring-cloud-examples项目下创建子项目spring-cloud-example-gateway,pom.xml如下:
  <!-- zuul -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  </dependency>

  <!-- Eureka Client Starter -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

  <!-- Config Client Starter -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  spring:
  application:
    name: spring-cloud-example-gateway
  server:
    port: 8002
  # Eureka相关配置
  eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:8001/eureka/
    instance:
      lease-renewal-interval-in-seconds: 10      # 心跳时间,即服务续约间隔时间(缺省为30s)
      lease-expiration-duration-in-seconds: 60  # 发呆时间,即服务续约到期时间(缺省为90s)
      prefer-ip-address: true
      instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
  zuul:
    routes:
      custom-route-a:
        url: http://localhost:8010/ # 指定的url
        path: /biza/**  # url对应的路径
      custom-route-b:
        url: http://localhost:8011/ # 指定的url
        path: /bizb/**  # url对应的路径
    host:
      connect-timeout-millis: 15000 #HTTP连接超时要比Hystrix的大
      socket-timeout-millis: 60000   #socket超时
  ribbon:
   ReadTimeout: 10000
   ConnectTimeout: 10000
  hystrix:
   command:
     default:
       execution:
         isolation:
           thread:
             timeoutInMilliseconds: 100000
  • 启动类GatewayApplication添加注解@EnableZuulProxy通过启用网关代理服务。
  @SpringBootApplication
  @EnableZuulProxy
  public class GatewayApplication {
      public static void main(String[] args) {
      // TODO Auto-generated method stub
      SpringApplication.run(GatewayApplication.class, args);
    }
  }

到此,配置中心、注册中心以及网关配置都已经配置好了,可以按顺序启动服务了
spring-cloud-example-config>>spring-cloud-example-eureka>>spring-cloud-example-a/spring-cloud-example-b/spring-cloud-example-gateway

微服务搭建原文

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341