zoukankan      html  css  js  c++  java
  • spring cloud

    1 版本说明

      Spring boot 版本: 2.1.3.RELEASE 

      Spring Cloud 版本:Greenwich.RELEASE

    2 搭建Eureka服务侧

    pom依赖如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5     <parent>
     6         <groupId>org.springframework.boot</groupId>
     7         <artifactId>spring-boot-starter-parent</artifactId>
     8         <version>2.1.3.RELEASE</version>
     9         <relativePath/> <!-- lookup parent from repository -->
    10     </parent>
    11     <groupId>com.linxi.jia</groupId>
    12     <artifactId>eureka-server</artifactId>
    13     <version>0.0.1-SNAPSHOT</version>
    14     <name>eureka-server</name>
    15     <description>eureka-server project for Spring Boot</description>
    16 
    17     <properties>
    18         <java.version>1.8</java.version>
    19         <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    20     </properties>
    21 
    22     <dependencies>
    23         <dependency>
    24             <groupId>org.springframework.cloud</groupId>
    25             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    26         </dependency>
    27 
    28         <dependency>
    29             <groupId>org.springframework.boot</groupId>
    30             <artifactId>spring-boot-starter-test</artifactId>
    31             <scope>test</scope>
    32         </dependency>
    33     </dependencies>
    34 
    35     <dependencyManagement>
    36         <dependencies>
    37             <dependency>
    38                 <groupId>org.springframework.cloud</groupId>
    39                 <artifactId>spring-cloud-dependencies</artifactId>
    40                 <version>${spring-cloud.version}</version>
    41                 <type>pom</type>
    42                 <scope>import</scope>
    43             </dependency>
    44         </dependencies>
    45     </dependencyManagement>
    46 
    47     <build>
    48         <plugins>
    49             <plugin>
    50                 <groupId>org.springframework.boot</groupId>
    51                 <artifactId>spring-boot-maven-plugin</artifactId>
    52             </plugin>
    53         </plugins>
    54     </build>
    55 
    56     <repositories>
    57         <repository>
    58             <id>spring-milestones</id>
    59             <name>Spring Milestones</name>
    60             <url>https://repo.spring.io/milestone</url>
    61         </repository>
    62     </repositories>
    63 
    64 </project>

     注意:Eureka即是服务端,也是客户端,默认配置是将自己注册到Eureka Server 上,所以在配置客户端信息的时候,也需要配置客户端注册的地址,当然也可以通过配置的形式关闭。

    配置文件如下:

      

    Eureka采用高可用部署方式,提供三个节点,三个配置文件只有 Eureka 服务侧的端口信息不一样,其余信息都一样。

    修改本地hosts文件,模拟三个不同节点:

    application.yml文件:

     1 spring:
     2   application:
     3     name: eureka-server
     4 logging:
     5   level:
     6     root: info
     7 server:
     8   port: 8761
     9 eureka:
    10   client:
    11     registerWithEureka: false # 是否自身注册到 Eureka 服务器上面 默认  true
    12     fetchRegistry: false  #是否从 Eureka Server 获取服务提供的信息,默认 true
    13     serviceUrl:
    14       defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/,http://127.0.0.1:8763/eureka/ # 当前的eureka版本,服务端同时也是户端,所以必须要将客户端注册到服务端上面去
    15   instance:
    16     hostname: node1
    17     # 为注册的 clinet 配置显示的ip值,默认显示的是主机名称
    18     #perferIpAddress: true
    19     #instance-id: 0.0.0.0:8888

    启动方式: 在应用启动类添加注解  @EnableEurekaServer

     3 以client搭建生产者

      

    配置文件:

     1 server:
     2   port: 8764
     3 spring:
     4   application:
     5     name: client-provider
     6 eureka:
     7   client:
     8     registerWithEureka: true
     9     fetchRegistry: true
    10     serviceUrl:
    11     # 需要注册到的 eureka 服务端信息
    12       defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/,http://127.0.0.1:8763/eureka/
    13   instance:
    14     hostname:  node1
    15     # 为注册的 clinet 配置显示的ip值,默认显示的是主机名称
    16     #perferIpAddress: true
    17     #instance-id: 0.0.0.0:8888
    18 logging:
    19   level:
    20     root: info

    启动:在应用启动类上面添加注解:@EnableEurekaClient

    注意:上述配置文件中包含两个配置项,spring.application.name 和 eureka.client.instance.hostname

     1. 前者代表当前应用的名称,在消费者进行消费时,是依靠该应用名进行服务消费。比如A应用以n台主机进行部署成集群的方式,如果直接调用ip进行访问,则没有办法实现负载均衡过程。

        2. 后者代表一个Eureka Client实例主机名称,比如点击查看8766实例的详细信息时,跳转的域名就是该配置项的值。

    跳转截图如下:

       

    Eureka页面

    下面截图描述的是注册到当前 Eureka Server 上面的 clinet 端。

    下面截图描述的是当前client 注册哪些 Eureka Server 上:

    完善生产者功能,向服务中心发布一个服务:

    package com.linxi.jia.com.linxi.jia;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Created by 156 on 2019/2/19.
     */
    @RestController
    @RequestMapping("/get")
    public class TController  {
    
        @Value("${server.port}")
        private String port;
    
        @GetMapping("/info")
        public Map<String,Object> getInfo(){
    
            Map<String,Object> info = new HashMap<String,Object>();
            info.put("name","zhangsan");
            info.put("age","20");
            info.put("port",port);
    
            return info;
        }
    }

    4 以client创建消费者

     Ribbon是Neflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端行为,为Ribbon配置服务提供列表后,Ribbon就可以基于某种负载均衡算法,自动的帮助服务消费者去请求。

    在生产者基础上新增依赖,并创建 RestTemplate 实例

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

    由于客户端本身已经集成,所以无需引入:

    创建 RestTemplate 实例,同时开启负载均衡。

    package com.linxi.jia.config;
    
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * Created by 156 on 2019/2/19.
     */
    
    @Configuration
    public class RibbonCfg {
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
           return new RestTemplate();
        }
    }

    创建调用实例:

    package com.linxi.jia;
    
    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.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.Map;
    
    /**
     * Created by 156 on 2019/2/19.
     */
    @RestController
    @RequestMapping("/test")
    public class TController {
    
        @Autowired
        private RestTemplate restTemplate;
    
        public Map getInfo(){
            // 通过 Eureka 服务端的 应用名称调用,无需考考虑端口和ip
            return restTemplate.getForObject("http://CLIENT-PROVIDER/get/info",Map.class);
        }
    }

    修改配置文件,添加启动注解。

    消费:http://localhost:8768/test/get

    分别消费多次,观察负载均衡情况。

    小样

  • 相关阅读:
    leetcode 77 组合
    leetcode 40组合总和 II
    leetcode 216 组合总和III
    弹性伸缩 AS(Auto Scaling)
    弹性计算服务(Elastic Compute Service) / 云服务器 ECS
    云计算概述
    Zabbix Proxy 分布式监控
    Zabbix 自动发现 & 自动注册
    LVS-DR 模式
    GoAccess 监控工具
  • 原文地址:https://www.cnblogs.com/nevegiveup/p/10399737.html
Copyright © 2011-2022 走看看