分布式系统 eureka loadbalance 注册中心

个人总结笔记

1.什么叫做分布式系统 : 将一个系统根据不同的业务进行拆分成不同的子系统,每个系统可以独立部署,独立运行。(SOA架构:各司其职)
2.分布式结局方案:
1.dubbo+zookeeper (dubbo通讯协议: RPC协议=>远程过程调用 特点:传输速度快,效率高)
2.SpringBoot+SpringCloud (http协议 )
3.什么是微服务:粒度更小的分布式系统,可以独立部署,独立运行并互不干扰
4.什么是集群(两个节点以上):将相同业务的服务部署到不同的服务器上,完成负载均衡、高可用的作用。(滥竽充数)
5.Rest风格:(面试题!!!!!!!!)RestFul API:在相同的URI的请求下,做不同的请求方式(GET,POST,PUT,DELETE),完成不同的请求操作(查、增、改、删),具备以上特点的的请求服务,称为RestFul风格服务 不同的业务场景使用不同的协议 : 如果对效率要求更高,并且开发过程使用统一的技术栈,用RPC
6.不同的业务场景使用不同的协议
如果对效率要求更高,并且开发过程使用统一的技术栈,用RPC
如果需要更灵活,跨语言,跨平台,用http
7.什么是服务治理(dubbo的运行机制:四个词、五个角色概括)
服务注册、服务发现、服务调用、服务监控(四词)
注册中心、服务提供者、服务消费者、容器、监控中心 (五角)
8.项目搭建步骤:
一:注册中心
1.引入server端的依赖
2.修改配置文件,添加注册中心的配置
3.在主启动类上添加开启服务端的注解
二.提供者步骤:
1引入client端的依赖
2.修改配置文件,添加注册中心的地址
3.在主启动类上添加开启客户端的注解
三.调用者步骤:
1引入client端的依赖
2.修改配置文件,添加注册中心的地址
3.在主启动类上添加开启客户端的注解
4.在主启动类中添加RestTemplate类的声明
5. 在业务中添加服务发现的类,完成远程服务调用
9. controller:返回响应,接受请求
@RestController注解(是@Controller+@ResponseBody的集合)
springboot最大的特点:0配置,基本无xml文件。
eureka自动保护机制:开发环境下关闭,生产环境下默认开启
10. 集中式架构 : 缺点 :代码耦合高 ,开发维护难
无法针对不同模块进行针对性优化
单点容错率低,并发能力差
11.垂直拆分 : 优点 :系统拆分实现了流量分担,解决了并发问题
可以针对不同模块进行优化
方便水平扩展,负载均衡,容错率提高
缺点 :系统间相互独立,会有很多重复开发工作,影响开发效率
12.分布式架构 : 优点 : 将基础服务进行了抽取,系统间相互调用,提高了代码复用和开发效率
缺点 : 系统间耦合度变高,调用关系错综复杂,难以维护

Eureka简介

Eureka 是Spring Cloud Netflix 微服务套件中的一部分, 它基于Netflix Eureka 做了二次封装, 主要负
责完成微服务架构中的服务治理功能。我们只需通过简单引入依赖和注解配置就能让Spring Boot 构建
的微服务应用轻松地与Eureka 服务治理体系进行整合。
Eureka包含两个组件:Eureka Server和Eureka Client。
Eureka Server提供服务注册服务。
Eureka Client是一个java客户端,用来简化与Eureka Server的交互、客户端同时也就是一个内置的、使
用轮询(round-robin)负载算法的负载均衡器。

搭建高可用

高可用定义:相同的业务 不同的服务器 端口号不一样
服务一
启动类
添加注解:
@SpringBootApplication 主启动类
@EnableEurekaServer //开启server端的注解 只能eureka

package com.offcn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 13:49
 * @Description:
 */
@SpringBootApplication
@EnableEurekaServer  //开启server端的注解
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class);
    }
}

YML配置文件
内容:
端口号
名字
defaultZone手写

server:
  port: 8761
spring:
  application:
    name: eurekaServer

eureka:
  server:
    enable-self-preservation: false    #关闭自我保护
    eviction-interval-timer-in-ms: 5000  #监听时间
  client:
    #register-with-eureka: false      #不向其它的注册中心注册服务
    #fetch-registry: false            #不从其它的注册中心拉取服务   注意:当注册中心搭建为单节点时,以上两个配置一定要设置为false
    service-url:
      defaultZone: http://localhost:8762/eureka

POM.XML
依赖: 客户端依赖

<?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>hello_parent</artifactId>
        <groupId>com.offcn</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka_server01</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <!--eureka server  add by lhq 20220112-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

//分界线

服务二
启动类
启动类
添加注解:
@SpringBootApplication 主启动类
@EnableEurekaServer //开启server端的注解 只能eureka

package com.offcn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 15:26
 * @Description:
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer02Application {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServer02Application.class);
    }
}

YML配置文件

server:
  port: 8762
spring:
  application:
    name: eurekaServer
eureka:
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 5000
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka

YML配置文件
内容:
端口号
名字
defaultZone手写
POM.XML

<?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>hello_parent</artifactId>
        <groupId>com.offcn</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka_server02</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

结果展示 会有两个节点
在这里插入图片描述
在这里插入图片描述
接口模式///
角色:接口 提供者 消费者

接口///
接口中写一个抽象方法 获得是名字

package com.offcn.service;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 13:57
 * @Description:
 */
public interface UserService {

    public String getName();
}

提供者

实现类

## implements的是接口 实现里面的抽象方法  给一个返回值

package com.offcn.service.impl;

import com.offcn.service.UserService;
import org.springframework.stereotype.Service;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 14:07
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Override
    public String getName() {
        return "张三";
    }
}

## **controller**
调用的是接口
package com.offcn.controller;

import com.offcn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 14:10
 * @Description:
 */
@RestController   //@Controller+@ResponseBody
public class UserController {


    @Autowired
    private UserService userService;

    @GetMapping ("/getName") //@RequestMapping(value="",Method=GET)
    public String getName(){
             return userService.getName();
    }
}


yml配置文件
配置信息:端口    名字    defaultZone
```java
server:
  port: 8001

spring:
  application:
    name: helloProvider

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka

pom.xml
内容 web 客户端 接口 远程调用

<?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>hello_parent</artifactId>
        <groupId>com.offcn</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hello_provider</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <dependencies>
        <!--springboot WEB工程的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!--eureka client  add by lhq 20220112-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>com.offcn</groupId>
            <artifactId>hello_interface</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

主启动类
@SpringBootApplication 主启动类
@EnableEurekaClient//开启客户端的注解

package com.offcn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 14:05
 * @Description:
 */
@SpringBootApplication
@EnableEurekaClient//开启客户端的注解
public class HelloProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloProviderApplication.class);
    }
}

在这里插入图片描述

//消费//

实现类

内容:
		//服务查询的工具
		//获取url
package com.offcn.service.impl;

import com.offcn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 14:40
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    //服务查询的工具
    @Autowired
    private DiscoveryClient discoveryClient;


    @Autowired
    private RestTemplate restTemplate;

    //发现服务      http协议   http://localhost:8001
    private String getServerInfo(){
        List<ServiceInstance> instanceList = discoveryClient.getInstances("HELLOPROVIDER");

        //if(null!=instanceList&&instanceList.size()>0){
        if(!CollectionUtils.isEmpty(instanceList)){
            ServiceInstance serviceInstance = instanceList.get(0);
            String host = serviceInstance.getHost();
            int port = serviceInstance.getPort();
            return "http://"+host+":"+port;
        }
        return null;
    }





    @Override
    public String getName() {
        ResponseEntity<String> entity = restTemplate.getForEntity(this.getServerInfo() + "/getName", String.class);

        return entity.getBody();
    }
}

conroller
内容:注入service
返回值是我们的信息

package com.offcn.controller;

import com.offcn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 14:58
 * @Description:
 */
@RestController
public class TestController {

    @Autowired
    private UserService userService;

    @RequestMapping("/print")
    public String  print(){
        return userService.getName()+": 欢迎来到优就业学习!";
    }
}

主启动类
//注入RestTemplate 进行服务调用

package com.offcn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: lhq
 * @Date: 2022/1/12 14:51
 * @Description:
 */
@SpringBootApplication    主启动类
@EnableEurekaClient   客气服务端
public class HelloConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloConsumerApplication.class);
    }
	
    @Bean    //<bean id="" class="">
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

yml配置文件
内容:端口 名字 客户端

server:
  port: 8002

spring:
  application:
    name: helloConsumer

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka

pom.xml
web 客户端 接口 注入

<?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>hello_parent</artifactId>
        <groupId>com.offcn</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hello_consumer</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>com.offcn</groupId>
            <artifactId>hello_interface</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>
结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/27679158349f474e99d566ab4c274c90.png#pic_center)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分布式注册中心 Eureka和ZooKeeper都是常用的服务注册与发现框架,用于管理分布式架构中的服务。 区别: 1.架构设计:Eureka采用了AP(可用性和分区容错性)原则,而ZooKeeper则采用了CP(一致性和分区容忍性)原则。 2.注册与发现:Eureka注册表是基于REST接口的,可以通过HTTP方式进行注册和发现;ZooKeeper使用Znodes树形结构保存服务信息,并通过监听机制实现注册和发现。 3.可用性:在网络分区故障的情况下,Eureka的服务实例仍可实现互相注册和发现;ZooKeeper则在网络分区故障时,将导致无法写入数据和发现服务。 原理: Eureka原理:Eureka采用了Client-Server架构,服务实例通过向Eureka Server注册自身信息,Eureka Server维护着服务实例的注册表。服务消费者通过Eureka Server查找可用的服务实例并进行调用。 ZooKeeper原理:ZooKeeper通过维护一个树形目录结构来存储数据,称为Znodes。服务实例作为子节点存储在Znodes中,ZooKeeper通过监听机制实现服务注册和发现。 优缺点: Eureka优点:易于使用和部署,并提供了自我保护机制,可以在网络断裂情况下保持高可用性。 Eureka缺点:由于是基于AP原则设计,所以在网络发生故障时可能出现数据不一致的情况。 ZooKeeper优点:提供了强一致性和较高的可用性,适合于需要强一致性的分布式系统。 ZooKeeper缺点:部署和使用相对复杂,对于大规模的分布式系统来说,可能会成为性能瓶颈。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值