This is the 10th day of my participation in Gwen Challenge


theme: smartblue

Introduction to the

Spring Cloud provides developers with tools to quickly build certain common patterns in distributed systems (e.g., configuration management, service discovery, intelligent routing, micro-proxy, control bus, one-time token, global lock, distributed session, cluster state, and so on). Coordination of distributed systems leads to boilerplate styles, and developers using Spring Cloud can quickly get up and implement boilerplate services and applications. They work in any distributed environment, including the developer’s own laptop, bare-metal data centers, and hosted platforms such as Cloud Foundry.

1. Spring Cloud system

  • Spring is Bean (object) centric, providing IOC, AOP, and so on.
  • Spring Boot provides automatic configuration and monitoring functions based on Application.
  • Centering on Service, Spring Cloud provides functions such as Service registration and discovery, Service invocation and load balancing.

2. Introduction to the Spring Cloud module

The Spring Cloud module is described as follows:

  • Eureka: Service registry for service management.
  • Ribbon: Client-based load balancing component.
  • Hystrix: Fault-tolerant framework that protects against service avalanche effects.
  • Feign: Web service client that simplifies calls to HTTP interfaces.
  • Zuul: API gateway that provides functions such as route forwarding and request filtering.
  • Config: distributed configuration management.
  • Sleuth: service tracing.
  • Stream: A framework for building message-driven microservice applications.
  • Bus: The cluster message Bus of the message broker.

In addition to the above modules, there are Cli, Task, etc. Only a few commonly used modules are covered in the tutorial.

3, had been

It is a service registry; If eureka allows the member system to call the order system remotely, how do you configure it?

The first step is to create the Eureka registry. Here we recommend using the tools of IDEA to create the SpringBoot project

Pom.xml file, can be directly copied to use

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.5.0 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>spring-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging> WAR </packaging> <name> Spring-cloud </name> <description>Demo project for Spring Boot < / description > < properties > < Java version > 1.8 < / Java version > < spring - cloud version > 2020.0.3 < / spring - cloud. Version > < / properties > < dependencies > < the 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-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Then we go to the YML file and do the following configuration

Server: port: 8888 Eureka: instance: hostname: localhost Client: registerWithEureka: false fetchRegistry false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/Copy the code

Then add the eureka registry representation to the startup class

@EnableEurekaServer @SpringBootApplication public class LearningApplication { public static void main(String[] args) { SpringApplication.run(LearningApplication.class, args); }}Copy the code

The eureka registry has been successfully started using localhost:8888

OK, so how do we register in the order system and membership system? Create two SpringBoot projects in the same way as the registry. All we care about is how the YML file registers the springBoot project, so let’s see how the YML file is configured

Eureka: client: serviceUrl: # eureka defaultZone registry address: http://localhost:8888/eureka/ server: # the project port to port: Spring: Application: # register eureka name: order-serverCopy the code

Create the Controller package and start it

@RestController public class ordercontroller { @RequestMapping("orderTest") public String orderTest(){ return "this is order"; Public class DemoApplication {public static void main(String[]) {public static void main(String[]); args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

Open the registry page again, you will find that you have registered in. Repeat the above steps to register for the membership system

4. Remote call methods from PRC

There are two methods of remote invocation

1. The first method is called by REST.

First we import rest POM dependencies. We use the user call order to add the call dependency to the user. —– Ribbon is a load balancing client that is similar to an Nginx reverse proxy. Ribbon can control HTT and TCP behavior.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
Copy the code
// Add the ribbon class to spring @enableEurekaclient@springBootApplication public class DemoApplication {public static void ribbon main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @bean@loadBalanced // Enable load balancing public RestTemplate RestTemplate() {return new RestTemplate(); } } public class memService{ @Autowired private RestTemplate restTemplate; @RequestMapping("/memTest") public String memTest(){ String str = restTemplate.getForObject("http://order-server/orderTest",String.class); return str; }}Copy the code

Then we call the interface access of the member system to see if it will walk into the order system

This is the result of a Rest remote call.

2. Feigin

Feign is a declarative pseudo Http client that makes writing Http clients much easier. With Feign, you just create an interface and annotate it. It has pluggable annotations that use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign integrates the Ribbon by default, and when combined with Eureka, implements load balancing by default.

·Feign uses interface based annotations ·Feign integrates the Ribbon

The first step, again, is importing dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
Copy the code
@Service @FeignClient("order-server") public interface orderFeign { @RequestMapping("/orderTest") public String orderTest(); } @RestController public class memController { @Autowired private OrderFeign orderFeign; @RequestMapping("/memTest") public String memTest(){ String str = orderFeign.orderTest(); return str; @enableEurekaclient @enableFeignClients @SpringBootApplication Public Class DemoApplication {public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code