내 연락처 정보
우편메소피아@프로톤메일.com
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
등록센터 서버가 하나만 있는 경우 단일 장애 지점이 발생하고 높은 동시성으로 처리할 수 없으므로 클러스터가 필요합니다.
서로 등록할 세 개의 EurekaServer를 준비합니다. 즉, 각 EurekaServer는 자신을 포함하여 모든 EureakServer에 등록해야 합니다. 각 EurekaServer는 서버와 클라이언트 역할을 모두 수행합니다. 다른 마이크로서비스(주문, 사용자)는 등록 주소를 모든 EurekaServer에 지정하기만 하면 됩니다.
여기서 사용되는 것은 클러스터링 효과를 달성하기 위해 여러 yml 파일을 구성하는 Eureka 모듈입니다.
- package org.example;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
-
- /**
- * @ClassName EurekaStart
- * @Author 23
- * @Date 2024/7/10 19:12
- * @Version 1.0
- * @Description TODO
- **/
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaStart {
- public static void main(String[] args) {
- SpringApplication.run(EurekaStart.class,args);
- }
- }
- spring:
- profiles:
- active: peer3
- ---
- spring:
- profiles: peer1
- application:
- name: Eureka1
- eureka:
- instance:
- hostname: localhost
- client:
- serviceUrl:
- defaultZone: http://localhost:10070/eureka/
- server:
- port: 10070
- ---
- spring:
- profiles: peer2
- application:
- name: Eureka2
- eureka:
- instance:
- hostname: localhost
- client:
- serviceUrl:
- defaultZone: http://localhost:10071/eureka/
- server:
- port: 10071
- ---
- spring:
- profiles: peer3
- application:
- name: Eureka3
- eureka:
- instance:
- hostname: localhost
- client:
- serviceUrl:
- defaultZone: http://localhost:10072/eureka/
- server:
- port: 10072
- <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>
- <parent>
- <groupId>org.example</groupId>
- <artifactId>Springcloud-Netflix</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
-
- <artifactId>Eureka-Service</artifactId>
- <packaging>jar</packaging>
-
- <name>Eureka-Service</name>
- <url>http://maven.apache.org</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </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>
- </dependency>
- <!-- Eureka服务端支持 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
- </project>
동시성을 제공하기 위해 때로는 동일한 서비스 공급자가 여러 개(상품 서비스)를 배포할 수 있습니다. 이 클라이언트는 호출 시 특정 책임 있는 밸런싱 전략에 따라 로드 호출을 완료해야 합니다.
- spring:
- profiles:
- active: order2
- ---
- server:
- port: 10010
- spring:
- profiles: order1
- application:
- name: order-service
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:10072/eureka
- instance:
- prefer-ip-address: true
- ---
- server:
- port: 10012
- spring:
- profiles: order2
- application:
- name: order-service
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:10072/eureka
- instance:
- prefer-ip-address: true
RestTmplate을 통해 URL로 서비스 호출을 완료합니다.
코드 구현(서비스 소비자 측):
- package org.example.Controller;
-
- import org.example.domain.Order;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.cloud.client.ServiceInstance;
- import org.springframework.cloud.client.discovery.DiscoveryClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- import java.util.Arrays;
- import java.util.List;
-
- /**
- * @ClassName UserController
- * @Author 23
- * @Date 2024/7/10 18:52
- * @Version 1.0
- * @Description TODO
- **/
- @RestController
- @RequestMapping("/User")
- public class UserControllerrlb {
- @Autowired
- private RestTemplate restTemplate;
- // @Autowired
- // private DiscoveryClient discoveryClient;
- @GetMapping("/getOrder/{id}")
- public List<Order> getOrderById(@PathVariable("id") Integer id) {
- // Order[] order = restTemplate.getForObject("http://localhost:10010/OrderService/user/" + id, Order[].class);
- // return Arrays.asList(order);
- // List<ServiceInstance> instances=discoveryClient.getInstances("order-service");//通过服务的名字去获取服务实例
- // ServiceInstance serviceInstance=instances.get(0);//定死只获取第一个服务实例对象
- // String ip=serviceInstance.getHost();//获取服务对象ip
- // int port=serviceInstance.getPort();//获取获取的实例对象的服务端口号
- Order[] orders=restTemplate.getForObject("http://Order-Service/OrderService/user/"+id,Order[].class);
- return Arrays.asList(orders);
-
- }
- }
리본(Ribbon)은 넷플릭스가 출시한 클라우드 미들 레이어 서비스의 오픈소스 프로젝트로 클라이언트 로드 밸런싱 알고리즘을 제공하는 것이 주요 기능이다. 리본 클라이언트 구성 요소는 연결 시간 초과, 재시도 등과 같은 일련의 전체 구성 항목을 제공합니다. 간단히 말해서 리본은 클라이언트 로드 밸런서입니다. 구성 파일에 로드 밸런서 뒤에 있는 모든 시스템을 나열할 수 있으며 리본은 특정 규칙(예: 단순 폴링, 무작위 연결 등)에 따라 자동으로 연결하도록 도와줍니다. 기계에서는 리본을 사용하여 사용자 정의 로드 밸런싱 알고리즘을 쉽게 구현할 수 있습니다. 리본은 특정 규칙에 따라 서비스의 여러 서비스 인스턴스에 대한 로드 밸런싱 호출을 완료할 수 있는 클라이언트 로드 밸런서입니다.
개략도:
OpenFeign은 웹 서비스 호출을 더 쉽게 만드는 것이 목적인 선언적 웹 서비스 클라이언트입니다. Feign은 HTTP 요청에 대한 인터페이스 템플릿을 제공합니다(액세스 주소가 표시되어 있음). 간단한 인터페이스를 작성하고 주석을 삽입하여 HTTP 요청의 매개변수, 형식, 주소 및 기타 정보를 정의할 수 있습니다. OpenFeign은 HTTP 요청을 완전히 프록시(동적 프록시)합니다. 서비스 요청 및 관련 처리를 완료하기 위한 메서드처럼 호출하기만 하면 됩니다. Feign은 Ribbon과 Hystrix(Hystrix에 대해서는 나중에 설명하겠습니다)를 통합하므로 더 이상 이 두 구성 요소를 명시적으로 사용할 필요가 없습니다.
다양한 IRule 하위 클래스를 구성하면 다양한 로드 밸런싱 전략을 선택할 수 있습니다. 즉, 특정 전략이 포함된 서비스 목록에서 서비스를 선택하여 호출을 완료할 수 있습니다. 물론 맞춤설정도 가능합니다. 따라서 로드 밸런싱 전략은 기본 제공 전략과 사용자 정의 전략으로 나눌 수 있습니다.