zoukankan      html  css  js  c++  java
  • Spring Cloud入门

    1.  什么是Spring Cloud?

    Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,主节点选举, 分布式session, 集群状态)。协调分布式环境中各个系统,为各类服务提供模板性配置。使用Spring Cloud, 开发人员可以搭建实现了这些样板的应用,并且在任何分布式环境下都能工作得非常好,小到笔记本电脑, 大到数据中心和云平台。

    Spring Cloud官网的定义比较抽象,我们可以从简单的东西开始。Spring Cloud是基于Spring Boot的, 最适合用于管理Spring Boot创建的各个微服务应用。要管理分布式环境下的各个Spring Boot微服务,必然存在服务的注册问题。所以我们先从服务的注册谈起。既然是注册,必然有个管理注册中心的服务器,各个在Spring Cloud管理下的Spring Boot应用就是需要注册的client

    Spring Cloud使用erureka server,  然后所有需要访问配置文件的应用都作为一个erureka client注册上去。eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。

    2.  创建Eureka Server

    1).创建一个Maven工程helloworld.eureka.server, pom.xml内容如下:

     pom.xml

     2). 用Spring Boot创建一个服务类EurekaServerApplication,需要一个注解@EnableEurekaServer加在springboot工程的启动类上

    复制代码
     1 package springcloud.helloworld.eureka.server;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
     6 
     7 @EnableEurekaServer
     8 @SpringBootApplication
     9 public class EurekaServerApplication {
    10 
    11     public static void main(String[] args) {
    12         SpringApplication.run(EurekaServerApplication.class, args);
    13     }
    14 }
    复制代码

    3).eureka server的配置文件application.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一个eureka server

    复制代码
     1 server:
     2    port: 8761
     3 
     4 eureka:
     5    instance:
     6        hostname: localhost
     7    client:
     8        registerWithEureka: false
     9        fetchRegistry: false
    10        serviceUrl:
    11            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    复制代码

    4) eureka server的工程结构如下

    5)启动eureka server,然后访问http://localhost:8761, 界面如下, "No instances available" 表示无client注册

    3.  创建Eureka Client

    1). 创建一个Maven工程helloworld.eureka.client, pom.xml内容如下:

     pom.xml

    2).  创建主类EurekaClientApplication

    使用@EnableEurekaClient注解表明是client

    复制代码
     1 package springcloud.helloworld.eureka.client;
     2 
     3 import org.springframework.beans.factory.annotation.Value;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RequestParam;
     9 import org.springframework.web.bind.annotation.RestController;
    10 
    11 @SpringBootApplication
    12 @EnableEurekaClient
    13 @RestController
    14 
    15 public class EurekaClientApplication {
    16 
    17     public static void main(String[] args) {
    18         SpringApplication.run(EurekaClientApplication.class, args);
    19     }
    20 
    21     @Value("${server.port}")
    22     String port;
    23     @RequestMapping("/")
    24     public String home() {
    25         return "hello world from port " + port;26     }
    27 
    28 }
    复制代码

    3) eureka client的配置文件appication.yml

    复制代码
    1 eureka:
    2     client:
    3         serviceUrl:
    4             defaultZone: http://localhost:8761/eureka/
    5 server:
    6     port: 8762
    7 spring:
    8     application:
    9         name: service-helloworld
    复制代码

    4). Client启动后, 可以访问http://localhost:8762

    5). 再次访问服务器端口, 可以看到Service Helloworld已经自动注册到之前的server中

     

  • 相关阅读:
    Java学习笔记(一)
    大端模式和小端模式
    C语言数据的表示和存储(IEEE 754标准)
    C语言的limits.h文件
    有关计算机系统的一些东西
    基于51单片机设计的简易电子琴
    11G新特性 -- ASM Fast Mirror Resync
    11G新特性 -- variable size extents
    11G新特性 -- ASM的兼容性
    log file switch (checkpoint incomplete)
  • 原文地址:https://www.cnblogs.com/Arvin66-lyh/p/10841102.html
Copyright © 2011-2022 走看看