zoukankan      html  css  js  c++  java
  • SpringCloud基础学习

    详细学习:

    https://blog.csdn.net/u014494148/article/details/105741321

    1.分布式框架,是分布式服务的一种实现(需要先学习使用springboot)

    官网网站为spring.io

    EurekaService

    分为三部

    1.导包

    <dependencies>
            <!--开始Eureka的服务中心-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        </dependencies>

    2.写入yml

    我这里分为单个配置

    server:
      port: 1010  #端口
    eureka:
      instance:
        hostname: localhost #主机
      client: #客户端配置
        registerWithEureka: false  #EurekaServer自己不要注册到EurekaServer自己 ,只有EurekaClient才注册
        fetchRegistry: false  #EurekaServer不要拉取服务的通信地址列表 ,只有EurekaClient才拉取地址列表
        serviceUrl:  #注册中心的注册地址
          defaultZone: http://localhost:1010/eureka/
          #http://${eureka.instance.hostname}:${server.port}/eureka/
    View Code

    三个配置

    通过active分别启动不过有些不支持多个开启需要放开

    如果需要模拟上个配置者需要在hosts文件中添加127.0.0.1:peer1等

    如果开启会报错是没有问题的,因为他会30s发送一次连接,三次没有连接成功可能会报错,但是一开始会报错是正常的

    eureka:
      client:
        serviceUrl:
          defaultZone: http://peer1:1011/eureka/,http://peer2:1012/eureka/,http://peer3:1013/eureka/
    spring:
      profiles:
        active: peer3 #需要启用的配置
    ---
    spring:
      profiles: peer1 #配置名称
      application:
        name: eureka-service #服务名称
    eureka:
      instance:
        hostname: peer1 #主机名称
        prefer-ip-address: true
        instance-id: eureka-service
    server:
      port: 1011
    
    ---
    spring:
      profiles: peer2 #配置名称
      application:
        name: eureka-service #服务名称
    eureka:
      instance:
        hostname: peer2 #主机名称
        prefer-ip-address: true
        instance-id: eureka-service:1012
    server:
      port: 1012
    
    ---
    spring:
      profiles: peer3 #配置名称
      application:
        name: eureka-service #服务名称
    eureka:
      instance:
        hostname: peer3 #主机名称
        prefer-ip-address: true
        instance-id: eureka-service:1013
    server:
      port: 1013
    View Code

    3.写入配置文件

    /**
     * 注册中心启动类
     * @EnableEurekaServer : 开启EurekaServer服务端
     */
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication1010
    {
        public static void main( String[] args )
        {
            SpringApplication.run(EurekaServerApplication1010.class);
        }
    }

    EurekaClient

    一样的分为三部

    导包

    <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    View Code

    写入yml

    eureka:
      client:
        serviceUrl: #注册到EurekaServer
          defaultZone: http://peer1:1011/eureka/,http://peer2:1012/eureka/,http://peer3:1013/eureka/
      instance:
        prefer-ip-address: true #是否开启ip注册
        instance-id: user-server:1020 #ip名称
    spring:
      application:
        name: user-server #服务名称
    server:
      port: 1020
    View Code

    写入配置类

    /**
     * 用户的启动类
     * @EnableEurekaClient: 标记该应用是 Eureka客户端
     */
    @SpringBootApplication
    @EnableEurekaClient
    public class UserServerApplication1020
    {
    
        public static void main( String[] args )
        {
            SpringApplication.run(UserServerApplication1020.class);
        }
    }
    View Code
  • 相关阅读:
    类的设计问题
    php数组存在重复的相反元素,去重复
    常用JS验证函数总结
    python常用模块
    re 模块
    logging 模块
    configparser模块
    python 文件处理
    第15章-输入/输出 --- 理解Java的IO流
    第10章-验证框架 --- 验证器类型
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/13617108.html
Copyright © 2011-2022 走看看