This is my third article on getting Started

1.Eureka

Service registry, register the address of the producer with the service registry; And provide the corresponding address to the consumer call.

Spring.application. name=eureka-server server.port=8000 eureka-instance. hostname=localhost #fetch-registry #register-with-eureka #register-with-eureka #register-with-eureka #register-with-eureka #register-with-eureka #register-with-eureka #register-with-eureka Default is true eureka.client.register-with-eureka=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/Copy the code
In your own modules / / rely on < dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>Copy the code


* In the producer module where the service should be provided

/ / in the corresponding service producers of pom file into the client's dependence on the < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>Copy the code
/ / in the corresponding service producers in the configuration file to join the path of eureka eureka. Client. The service - url. DefaultZone = http://localhost:8000/eureka/Copy the code

2. Use Feign to implement invocation between services

Add dependencies in the appropriate business layer <! --feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>Copy the code

Add annotations to the startup class of the calling service

@EnableFeignClients
Copy the code

Creating an interface class

@FeignClient("course-list")
public interface CourseListClient {

    @GetMapping("/course")
     List<Course> courseList();
}
Copy the code

Call @autowired just like a normal service class

3. Load balancing

The customer calls to Nginx first, Nginx to achieve load balancing;

The Ribbon implements load balancing between calls between internal modules. The Ribbon does not need to go to Nginx to avoid long links.

RamdomRule Random policy: a random node

RoundRobinRule Indicates a polling policy. Nodes 1, 2, and 3 are round

ResponseTimeWeightedRule weighted: depending on the response time and the distribution and reference of the RabbitMQ work mode

4. Hystrix circuit breakers

Add dependencies at the business layer

<! --Hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>Copy the code

Add to the configuration file

feign.hystrix.enabled=true
Copy the code

Added to feign’s client interface

@FeignClient(value = "course-list",fallback = CourseListClientHystrix.class)
Copy the code

Hystrix circuit breakers are an alternate way to jump if a user fails to call a service or if no service is available for a long time. Improve user experience

5. Gateway Zuul

Users are forwarded to various service interfaces through access apis (gateways); The interface address of each service is not directly placed in the gateway, the address is registered with the service registry first, and the service registry informs the gateway, and the gateway knows the address of each service and calls it.

(1) Gateway filtering