zoukankan      html  css  js  c++  java
  • Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门

    服务治理:Spring Cloud Eureka

    一、服务治理

    服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现。

    1.服务注册

    在服务治理框架中,通常会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号、

    版本号、通信协议等一些附件信息告知注册中心,注册中心按服务名称分类组织服务清单,例如:

    2.服务发现:

    调用方需要向服务注册中心咨询服务,并获取所有服务的实例清单,以实现对具体服务实例的访问。

    二、Netflexi Euraka

    Spring Cloud Euraka使用Netflexi Euraka来实现服务的注册与发现,它既包含了

    服务端的组件也包含了客户端的组件。

    Euraka 服务端:即服务注册中心。

    Euraka 客户端:主要处理服务的注册与发现。Euraka客户端向注册中心注册自身提供的服务并

    周期性地发送心跳来更新它的服务租约。同时,他能从服务端查询当前注册的服务信息并把它们

    缓存到本地并周期性地更新服务状态。

    三、搭建服务注册中心

    pom.xml中引入必要依赖:

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Brixton.SR5</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

    启用服务注册中心:

    //启动一个服务注册中心,提供给其他应用进行对话。
    //在默认设置下,该服务注册中心也会将自己作为客户端尝试注册它自己
    @EnableEurekaServer
    @SpringBootApplication
    public class TangcloudApplication {
        public static void main(String[] args) {
            SpringApplication.run(TangcloudApplication.class, args);
        }
    }

    禁用默认的客户端注册行为:

    #设置服务注册中心端口
    server.port=1111
    eureka.instance.hostname=localhost
    #不向注册中心注册自己
    eureka.client.register-with-eureka=false
    #注册中心不需要去检索服务
    eureka.client.fetch-registry=false
    eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eurka/

    四、注册服务提供者

    pom.xml:

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Brixton.SR5</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

    controller:

    @RestController
    public class HelloController {
        @Autowired
        private DiscoveryClient client;
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String index() {
            ServiceInstance serviceInstance = client.getLocalServiceInstance();
            System.out.println("/hello, host: " + serviceInstance.getHost() + ", service_id: " +
            serviceInstance.getServiceId());
            return "Hello World!";
        }
    }
    @EnableDiscoveryClient //激活DiscoveryClient实现
    @SpringBootApplication
    public class Service1Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Service1Application.class, args);
        }
    
    }

    application.properties文件:

    server.port=9669
    spring.application.name=hello-service
    eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

    五、高可用注册中心

    Euraka高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就可以

    形成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用效果。

    构建一个服务注册中心集群

    1)创建application-peer1.properties,作为peer1服务中心的配置:

    pring.application.name=eureka-server
    server.port=1111
    
    eureka.instance.hostname=peer1
    eureka.client.register-with-eureka=true
    eureka.client.fetch-registry=true
    eureka.client.service-url.defaultZone=http://peer2:1112/eureka/

    2)建application-peer1.properties,作为peer1服务中心的配置:

    spring.application.name=eureka-server
    server.port=1112
    
    eureka.instance.hostname=peer2
    eureka.client.register-with-eureka=true
    eureka.client.fetch-registry=true
    eureka.client.service-url.defaultZone=http://peer1:1111/eureka/

    3)服务提供方的配置:

    eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/

    六、服务的发现与消费

     下面构建一个服务消费者,它可以发现服务及消费服务。其中服务的发现任务由Eureka的客户端完成。

    Ribbon是一个基于HTTP和TCP的客户端负载均衡器,它在Eureka发现服务的基础上,实现了一套对服务

    的选择策略。

    首先,创建一个Spring Boot工程,并在之前hello-service的pom.xml基础上新增:

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

    启动类:

    @SpringBootApplication
    @EnableDiscoveryClient //让该应用注册为Eureka客户端,获得服务发现的能力
    public class ConsumerApplication {
        @Bean
        @LoadBalanced  //开启客户端负载均衡
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    }

    application.properties

    server.port=9671
    spring.application.name=ribbon-consumer
    eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

    controller:

    @RestController
    public class ConsumerController {
        @Autowired
        RestTemplate restTemplate; //通过它来调用service-hello服务的/hello接口
        @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
        public String helloConsumer() {
            return restTemplate.getForEntity("http://hello-service/hello",
                    String.class).getBody();
        }
    }

     

  • 相关阅读:
    数据库语句中(+)是什么意思
    MySQL的存储引擎(二)解决Warning Code : 3719 'utf8' is currently an alias for the character set UTF8MB3,...
    MSQL存储引擎(一)
    fastjson的使用,在redis里面存list
    js的发展历史,笔记
    spring的断言工具类Assert的基本使用
    httpclient的使用
    nginx的反向代理
    使用 Redis 连接池的原因
    springboot的yml自定义值的笔记
  • 原文地址:https://www.cnblogs.com/Shadowplay/p/10471848.html
Copyright © 2011-2022 走看看