美文网首页
2020-02-08_Myeurekainaction单节点模式

2020-02-08_Myeurekainaction单节点模式

作者: kikop | 来源:发表于2020-07-19 18:45 被阅读0次

    Myeurekainaction单节点模式学习

    1 概述

    本文着重学习Eureka单节点模式的部署,SpringCloud基于Greenwich.RELEASE版本。

    源码地址:

    https://github.com/kikop/mysingleeurekainaction.git

    1.1工程结构

    image.png

    图 1

    1.2主maven依赖

    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.kikop.myeurekainaction</groupId>
    
        <artifactId>mysingleeurekainaction</artifactId>
    
        <version>1.0-SNAPSHOT</version>
    
        <modules>
    
            <module>eureka-server</module>
    
            <module>eureka-client</module>
    
        </modules>
    
        <packaging>pom</packaging>
    
        <parent>
    
            <groupId>org.springframework.boot</groupId>
    
            <artifactId>spring-boot-starter-parent</artifactId>
    
            <version>2.1.4.RELEASE</version>
    
            <relativePath/> <!-- lookup parent from repository -->
    
        </parent>
    
        <properties>
    
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    
            <java.version>1.8</java.version>
    
            <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    
        </properties>
    
        <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>
    
    </project>
    
    

    1.3 SpringBootApplication vs SpringCloudApplication

    image.png

    图 2

    
    @Target(ElementType.TYPE)
    
    @Retention(RetentionPolicy.RUNTIME)
    
    @Documented
    
    @Inherited
    
    @SpringBootConfiguration
    
    @EnableAutoConfiguration
    
    @ComponentScan(excludeFilters = {
    
    @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
    
    @Filter(type = FilterType.CUSTOM,
    
    classes = AutoConfigurationExcludeFilter.class) })
    
    public @interface SpringBootApplication {
    
    
    
    /**
    
     * @author Spencer Gibb
    
     */
    
    @Target(ElementType.TYPE)
    
    @Retention(RetentionPolicy.RUNTIME)
    
    @Documented
    
    @Inherited
    
    @SpringBootApplication
    
    @EnableDiscoveryClient
    
    @EnableCircuitBreaker
    
    public @interface SpringCloudApplication {
    
    }
    
    

    2 eureka单节点模式

    2.1 eurekaserver注册中心

    作为eureka的注册中心。需引用spring-cloud-starter-netflix-eureka-server。

    2.1.1 maven依赖

    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <parent>
    
            <artifactId>mysingleeurekainaction</artifactId>
    
            <groupId>com.kikop.myeurekainaction</groupId>
    
            <version>1.0-SNAPSHOT</version>
    
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>eureka-server</artifactId>
    
        <dependencies>
    
            <!--1.eureka server服务注册中心-->
    
            <dependency>
    
                <groupId>org.springframework.cloud</groupId>
    
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    
            </dependency>
    
            <!--2.actuator端点-->
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-starter-actuator</artifactId>
    
            </dependency>
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-starter-test</artifactId>
    
                <scope>test</scope>
    
            </dependency>
    
        </dependencies>
    
        <build>
    
            <plugins>
    
                <plugin>
    
                    <groupId>org.springframework.boot</groupId>
    
                    <artifactId>spring-boot-maven-plugin</artifactId>
    
                </plugin>
    
            </plugins>
    
        </build>
    
    </project>
    
    

    2.1.2 resources(application.yml)配置

    
    spring:
    
      application:
    
        name: eureka-server
    
    server:
    
      port: 8761
    
    eureka:
    
      # Eureka Server自己就是注册中心,必须禁止向自己注册,如下配置:
    
      # 必须将 eureka.client.register-with-eureka 和 eureka.client.fetch-registry 设置为false
    
      client:
    
        register-with-eureka: false # 禁止向自己注册
    
        fetch-registry: false  # 屏蔽注册信息
    
        service-url:
    
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    
      instance:
    
        prefer-ip-address: false # true:以ip地址注册到服务中心,特别是多网卡时,指定中心ip地址,集群模式相互注册时有用
    
        status-page-url-path: /actuator/info # 信息查询的url
    
        health-check-url-path: /actuator/health # 健康检查的url
    
        hostname: localhost # defaultZone用到
    
        instance-id: ${spring.application.name}:${server.port}
    
    

    2.1.3 JavaMain入口

    
    package com.kikop.eurekaserver;
    
    import org.springframework.boot.SpringApplication;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    // 开启 eureka 服务注册
    
    @EnableEurekaServer
    
    //开启组件扫描、bean nest 配置、自动配置
    
    @SpringBootApplication
    
    public class EurekaServerApplication {
    
    public static void main(String[] args) {
    
    SpringApplication.run(EurekaServerApplication.class, args);
    
    }
    
    }
    
    

    2.1.4测试

    查看服务中心

    http://localhost:8761/

    image.png

    2.2 eurekaprovider服务提供者

    作为eureka的client_P端,需引用:

    spring-cloud-starter-netflix-eureka-client。

    2.2.1 maven依赖

    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <parent>
    
            <artifactId>mysingleeurekainaction</artifactId>
    
            <groupId>com.kikop.myeurekainaction</groupId>
    
            <version>1.0-SNAPSHOT</version>
    
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>eureka-client</artifactId>
    
        <dependencies>
    
            <!--1.eureak服务提供者-->
    
            <dependency>
    
                <groupId>org.springframework.cloud</groupId>
    
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    
            </dependency>
    
            <!--2.web服务-->
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-starter-web</artifactId>
    
            </dependency>
    
            <!--3.acturator端点-->
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-starter-actuator</artifactId>
    
            </dependency>
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-starter-test</artifactId>
    
                <scope>test</scope>
    
            </dependency>
    
            <!--<dependency>-->
    
                <!--<groupId>com.kikop.boot</groupId>-->
    
                <!--<artifactId>hello-spring-boot-starter</artifactId>-->
    
                <!--<version>1.0-SNAPSHOT</version>-->
    
            <!--</dependency>-->
    
            <!--业务系统中添加-->
    
            <!--作用是编译时生成 spring-configuration-metadata.json ,-->
    
            <!--此文件主要给IDE使用。如当配置此jar相关配置属性在 application.yml ,-->
    
            <!--你可以用ctlr+鼠标左键点击属性名,IDE会跳转到你配置此属性的类中。-->
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-configuration-processor</artifactId>
    
                <optional>true</optional>
    
            </dependency>
    
        </dependencies>
    
        <build>
    
            <plugins>
    
                <plugin>
    
                    <groupId>org.springframework.boot</groupId>
    
                    <artifactId>spring-boot-maven-plugin</artifactId>
    
                </plugin>
    
            </plugins>
    
        </build>
    
    </project>
    
    

    2.2.2 resources(application.yml)配置

    
    spring:
    
      application:
    
        name: eureka-client
    
    server:
    
      port: 8762
    
    eureka:
    
      client:
    
        service-url:
    
    #    指定配置服务注册中心地址
    
          defaultZone: http://localhost:8761/eureka/
    
    com:
    
      kikop:
    
        boot:
    
          name: kikop
    
          hobby: tabletennis
    
          place: nj
    
    

    2.2.3 Java

    2.2.3.1 main入口

    
    package com.kikop.eurekaclient;
    
    import org.springframework.boot.SpringApplication;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    // 开启 EnableEurekaClient 注解,注册到EurekaServer服务端
    
    @EnableEurekaClient
    
    // springboot应用程序
    
    @SpringBootApplication
    
    public class EurekaProviderApplication {
    
    public static void main(String[] args) {
    
    SpringApplication.run(EurekaProviderApplication.class, args);
    
    }
    
    }
    
    

    2.2.3.2 controller

    
    package com.kikop.eurekaclient.controller;
    
    import com.kikop.boot.entity.HelloServiceInfo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import org.springframework.beans.factory.annotation.Value;
    
    import org.springframework.http.MediaType;
    
    import org.springframework.web.bind.annotation.GetMapping;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import org.springframework.web.bind.annotation.RequestParam;
    
    import org.springframework.web.bind.annotation.RestController;
    
    /**
    
     * @author kikop
    
     * @version 1.0
    
     * @project Name: myeurekainaction
    
     * @file Name: HiController
    
     * @desc 功能描述 服务提供者
    
     * @date 2020/2/9
    
     * @time 11:02
    
     * @by IDE: IntelliJ IDEA
    
     */
    
    @RestController
    
    @RequestMapping(value = "/provider")
    
    public class ProviderController {
    
        @Value("${server.port}")
    
        String port;
    
        @Value("${com.kikop.boot.hobby}")
    
        String hobby;
    
        @Autowired
    
        HelloServiceInfo helloServiceInfo;
    
        @RequestMapping(value = "/hello")
    
        public String hello() {
    
            return helloServiceInfo.toString();
    
        }
    
        @GetMapping("/sayHi")
    
        public String sayHi(@RequestParam String name) {
    
            return "sayHi,你好:" + name + ",i am from port:" + port;
    
        }
    
        @RequestMapping(value = "/sayHi2")
    
        public String sayHi2(String name) {
    
            return "sayHi2,你好:" + hobby + ",i am from port:" + port;
    
        }
    
        // 响应: produces
    
        @RequestMapping(value = "/sayHi3", consumes = {"text/plain", "application/*"}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    
        public String sayHi3(String name) {
    
            return "sayHi2,你好:" + hobby + ",i am from port:" + port;
    
        }
    
        /**
    
         * 暴露的远程服务接口
    
         * @return
    
         */
    
        @GetMapping("/getOuterName")
    
        public String getOuterName(@RequestParam(value = "name")  String name) {
    
            return "你好:" + name + ",this msg from port:" + port;
    
        }
    
    }
    
    

    2.2.4测试

    基本接口测试:

    http://localhost:8762/provider/getOuterName?name=kikop

    你好:kikop,this msg from port:8762

    耦合接口测试:

    http://localhost:8762/providercommon/getCurrentVersion?sysCode=110

    2.3 eureka-consumer服务消费者

    基于开源OpenFeign,而OpenFeign底层为:Feign的实现。

    作为eureka的client_C端,消费者也作为eureka的client端。需引用

    spring-cloud-starter-netflix-eureka-client。

    2.3.1 maven依赖

    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <parent>
    
            <artifactId>mysingleeurekainaction</artifactId>
    
            <groupId>com.kikop.myeurekainaction</groupId>
    
            <version>1.0-SNAPSHOT</version>
    
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>eureka-consumer</artifactId>
    
        <dependencies>
    
            <!--1.eureka client-->
    
            <dependency>
    
                <groupId>org.springframework.cloud</groupId>
    
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    
            </dependency>
    
            <!--2.http服务请求,伪http客户端(基于tcp、http)-->
    
            <dependency>
    
                <groupId>org.springframework.cloud</groupId>
    
                <artifactId>spring-cloud-starter-openfeign</artifactId>
    
            </dependency>
    
            <!--3.web服务-->
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-starter-web</artifactId>
    
            </dependency>
    
            <!--4.test-->
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-starter-test</artifactId>
    
                <scope>test</scope>
    
            </dependency>
    
            <!--5.actuator-->
    
            <dependency>
    
                <groupId>org.springframework.boot</groupId>
    
                <artifactId>spring-boot-starter-actuator</artifactId>
    
            </dependency>
    
        </dependencies>
    
    </project>
    
    

    2.3.3 resources(application.yml)配置

    
    spring:
    
      application:
    
        name: eureka-consumer
    
    server:
    
      port: 8768
    
    eureka:
    
      client:
    
        service-url:
    
    #    指定配置服务注册中心地址
    
          defaultZone: http://localhost:8761/eureka/
    
    

    2.3.3 Java

    2.3.3.1 main入口

    
    package com.kikop.eurekaconsumer;
    
    import org.springframework.boot.SpringApplication;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    // springboot应用程序
    
    @SpringBootApplication
    
    // V1:开启 EnableEurekaClient 注解,注册到EurekaServer服务端
    
    @EnableEurekaClient
    
    // V2:启用服务发现能力(todo)
    
    // @EnableDiscoveryClient
    
    // Feign客户端
    
    @EnableFeignClients
    
    public class EurekaConsumerApplication {
    
    public static void main(String[] args) {
    
    SpringApplication.run(EurekaConsumerApplication.class, args);
    
    }
    
    }
    
    

    2.3.3.2绑定****EurekaClientFeign

    
    package com.kikop.eurekaconsumer.client;
    
    import org.springframework.cloud.openfeign.FeignClient;
    
    import org.springframework.web.bind.annotation.GetMapping;
    
    import org.springframework.web.bind.annotation.RequestParam;
    
    /**
    
     * @author kikop
    
     * @version 1.0
    
     * @project Name: myeurekainaction
    
     * @file Name: HiController
    
     * @desc 功能描述 手动绑定远程服务及方法(fangzhipeng)
    
     * 在接口上加 @FeignClient 注解来声明一个Feign Client,
    
     * 其中value 为指定的远程调用其他服务的服务名,
    
     * FeignConfig.class 为 Feign Client 的配置类
    
     * @date 2020/2/9
    
     * @time 11:02
    
     * @by IDE: IntelliJ IDEA
    
     */
    
    @FeignClient(value = "eureka-provider")
    
    public interface EurekaClientFeign {
    
        /**
    
         * 设置底层服务的mapping映射
    
         * 在 EurekaClientFeign 接口内部 定义 getOuterName 方法
    
         * 该方法通过 Feign指定远程服务对象(eureka-client): Api接口 ("/getOuterName"):参数匹配来调用
    
         * @param name
    
         * @return
    
         */
    
        @GetMapping(value = "/provider/getOuterName")
    
        String getOuterName(@RequestParam(value = "name") String name);
    
    }
    
    

    2.3.3.3定义service

    
    package com.kikop.eurekaconsumer.service;
    
    import com.kikop.eurekaconsumer.client.EurekaClientFeign;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import org.springframework.stereotype.Service;
    
    /**
    
     * @author kikop
    
     * @version 1.0
    
     * @project Name: mysingleeurekainaction
    
     * @file Name: TestService
    
     * @desc 功能描述
    
     * @date 2020/7/18
    
     * @time 16:06
    
     * @by IDE: IntelliJ IDEA
    
     */
    
    @Service
    
    public class TestServiceImpl {
    
        /**
    
         * 注入远程服务的Wrap
    
         */
    
        @Autowired
    
        EurekaClientFeign eurekaClientFeign;
    
        public String getOuterName(String reqParam) {
    
            return eurekaClientFeign.getOuterName(reqParam);
    
        }
    
    }
    
    

    2.3.3.2 controller

    
    package com.kikop.eurekaconsumer.controller;
    
    import com.kikop.eurekaconsumer.service.TestServiceImpl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import org.springframework.web.bind.annotation.GetMapping;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import org.springframework.web.bind.annotation.RequestParam;
    
    import org.springframework.web.bind.annotation.RestController;
    
    /**
    
     * @author kikop
    
     * @version 1.0
    
     * @project Name: myeurekainaction
    
     * @file Name: HiController
    
     * @desc 功能描述 服务提供者
    
     * @date 2020/2/9
    
     * @time 11:02
    
     * @by IDE: IntelliJ IDEA
    
     */
    
    @RestController
    
    @RequestMapping(value = "/consumer")
    
    public class ConsumerController {
    
        @Autowired
    
        TestServiceImpl testService;
    
        @GetMapping(value = "/getOuterName")
    
        public String getOuterName(@RequestParam(value = "name",required = false,defaultValue = "默认名字") String name) {
    
            return testService.getOuterName(name);
    
        }
    
    }
    
    

    2.3.4测试

    http://localhost:8768/consumer/getOuterName

    http://localhost:8768/consumer/getOuterName?name=kikop

    你好:kikop,this msg from port:8762

    <u>http://localhost:8768/consumer/getCurrentVersion?sysCode=110</u>

    4 eureka常见参数

    4.1 eurekaserver的响应缓存

    Eurekaserver每30秒更新一次响应缓存。

    4.2 eurekclient注册延迟

    EurekaClient启动之后,不是立即向EurekaServer注册的,而是有一个延迟向服务端注册的时间,可以发现默认的延迟时间为40秒。

    4.3 eurekaclient的缓存

    Eurekaclient保留注册表信息的缓存,每30秒更新一次响应缓存,因此,刷新并发现需要30秒。

    4.4 LoadBalancer的缓存

    每30秒更新一次响应缓存。

    4.5 prefer-ip-address: true

    单节点模式下不建议,会导致自己注册为自己的Replicas。

    image.png

    图 3 prefer-ip-address设为true时

    image.png

    图 4 prefer-ip-address设为false时

    参考

    1.1 Springcloud

    https://spring.io/projects/spring-cloud

    https://start.spring.io/

    相关文章

      网友评论

          本文标题:2020-02-08_Myeurekainaction单节点模式

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