美文网首页Spring Cloud Spring Cloud
spring cloud 构建微服务一(eureka)服务注册与

spring cloud 构建微服务一(eureka)服务注册与

作者: 阿波罗程序猿 | 来源:发表于2018-02-08 16:17 被阅读72次

    spring cloud的由来就不说了,可以查看官网。昨天刚刚上手spring cloud这里记录下我的学习过程,算是做一个知识储备,等以后业务增长为拆分微服务做准备。

    spring cloud 版本

    • 到现在spring cloud已经发展了有5个版本Angel、Brixton、Camden、Dalston、Edgware、Finchley 顺序就是 ABCDEF。其中Angel、Brixton这两个版本已经不维护了。最新GA的是Edgware SR1。所以本例就根据这个版本作为开发。
      目前官方维护的版本

    spring boot 与 spring cloud 的关系

    • 可以说spring cloud是建立在spring boot基础之上的。spring cloud脱离spring boot是玩不转的。但是spring boot脱离spring cloud依然可以玩得转。

    创建 Eureka 服务发现

    Eureka是属于Netflix OSS componentsNetflix开源软件组件中的一种,专门用来进行服务发现的组件。使用Eureka可以实现实例的注册,并且客户端可以通过spring管理的beans发现该实例。

    • 先看一下配置表,eureka-customer 不在这次范围之内。
    应用名称 eureka-server eureka-client eureka-customer
    端口 9000-9099 9100-9199 9200-9299
    说明 提供注册服务 服务本身 消费服务
    创建eureka-server 项目。使用SPRING INITIALIZR初始化项目。
    初始化 eureka-server 项目
    • @EnableEurekaServer 激活eureka服务。
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    /**
     * @author 张博
     * @since 2018/2/7 下午3:59
     */
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    
    • 配置 application.properties。 默认情况下eureka-server会将自己作为客户端注册在eureka-server上。所以我们这里禁用此默认。
    # 服务注册中心
    spring.application.name=eureka-server
    server.port=9000
    
    eureka.instance.hostname=localhost
    # 指示此实例是否向该服务注册,并被其它 eureka server 发现
    eureka.client.register-with-eureka=false
    # 指示此客户端是否从 eureka server 上获取 eureka 注册表信息
    eureka.client.fetch-registry=false
    # 设为false,关闭自我保护,默认为true
    eureka.server.enable-self-preservation=true
    # 清理间隔(单位毫秒,默认是0 不清除)
    eureka.server.eviction-interval-timer-in-ms=1000
    #eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9001/eureka/
    #EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
    
    • 启动该服务,由于还没有服务注册到上面所以看到 Instances currently registered with Eureka 这行是空的。
      启动该服务
    创建eureka-client 项目。
    • 创建 controllerDiscoveryClient 可以发现可用的服务。
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.discovery.DiscoveryClient;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author 张博
     */
    @RestController
    public class UserController {
    
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @RequestMapping(value = "/getServices/{port}", method = RequestMethod.GET)
        public String getServices(@PathVariable String port) {
            String str = "" + discoveryClient.getServices();
            return str.concat(": ").concat(port);
        }
    }
    
    • @EnableDiscoveryClient@EnableEurekaClient,它们都能启用eureka发现。@EnableDiscoveryClientspring-cloud-commons 中,@EnableEurekaClientspring-cloud-netflix 中。如果是使用 eureka的话这两个注解使用哪个都行。但spring cloud服务发现不止有eureka还有Consul、zookeeper。所以建议使用 spring-cloud-commons 中的@EnableDiscoveryClient
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class EurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    }
    
    • 配置application.propertiesspring.application.name 这个名字很重要,起相同的名字,以后配置集群时,可以把多个ip绑定到这个名字下,做负载均衡。
    # 服务提供者
    spring.application.name=eureka-client
    server.port= 9100
    
    # 把服务注册到 eureka-server 中
    eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9000/eureka/
    
    • 启动应用,查看eureka server 发现Instances currently registered with Eureka多了一项。到此服务注册与发现搞定。
      eureka server
    • 在浏览器输入http://192.168.0.109:9100/getServices/9100,得到响应结果。
    [eureka-client]: 9100
    
    复制刚才创建eureka-client项目。
    • 只修改server.port的端口号,之后启动并刷新,http://127.0.0.1:9000/
    # 服务提供者
    spring.application.name=eureka-client
    server.port=9101
    
    eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9000/eureka/
    
    可以看到,相同的应用名下,挂着2个服务

    下节我们来看看如何实现客户端负载均衡的操作。

    相关文章

      网友评论

        本文标题:spring cloud 构建微服务一(eureka)服务注册与

        本文链接:https://www.haomeiwen.com/subject/qixttftx.html