2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
If there is only one registration center server, there will be a single point of failure and high concurrency processing will not be possible, so a cluster is required.
Prepare three EurekaServers to register with each other, that is, each EurekaServer needs to register with all EureakServers, including itself. Each EurekaServer acts as both a server and a client. Our other microservices (order, user) only need to point the registration address to all EurekaServers.
A Eureka module is used here to configure multiple yml files to achieve the cluster effect.
- 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>
In order to provide concurrency, sometimes the same service provider can deploy multiple (commodity services). When calling, the client must complete the load call according to a certain load balancing strategy.
- 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
Use RestTmplate to complete the service call with URL
Code implementation (service consumer side):
- 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 is an open source project for cloud middle-tier services released by Netflix. Its main function is to provide a client-side load balancing algorithm. The Ribbon client component provides a series of complete configuration items, such as connection timeout, retry, etc. Simply put, Ribbon is a client-side load balancer. We can list all the machines behind the load balancer in the configuration file. Ribbon will automatically help you connect to these machines based on certain rules (such as simple polling, random connection, etc.). It is also easy for us to use Ribbon to implement a custom load balancing algorithm. Ribbon is a client-side load balancer. It can complete the load balancing call of multiple service instances of a service according to certain rules. These rules also support customization.
Schematic:
OpenFeign is a declarative Web Service client, and its purpose is to make Web Service calls easier. Feign provides an interface template for HTTP requests (with access addresses marked on it). By writing simple interfaces and inserting annotations, you can define the parameters, format, address and other information of HTTP requests. OpenFeign will completely proxy (dynamic proxy) HTTP requests. We only need to call it like calling a method to complete the service request and related processing. Feign integrates Ribbon and Hystrix (we will talk about Hystrix later), so we no longer need to use these two components explicitly.
By configuring different IRule subclasses, you can select different load balancing strategies, that is, select a service from the service list with a specific strategy to complete the call. Of course, you can also customize it. So the load balancing strategy can be divided into built-in and custom.