1. Spring Cloud

Spring Cloud is a set of tools built on the basis of Spring Boot to simplify distributed system construction, providing developers with some common patterns to quickly build distributed systems. Such as: Configuration Management, Service Discovery, Circuit Breakers, Intelligent Routing, Micro-proxy, Control Bus, one-time tokens, Global locks, Leadership elections, Distributed Sessions, Cluster State. Spring Cloud contains multiple components: for example: Spring Cloud Config, Spring Cloud Netflix Spring Cloud project home page: the projects. Spring. IO/Spring – clou… Talk is cheep, show me the code. Below we will explain the various components in the Spring Cloud in a combination of code and instructions.

2. Example Planning

Create a parent project

Before entering the topic, we first create a parent project (Spring-Cloud-MicroService-Study) that provides unified management of Maven dependencies within the project.

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "ht tp://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = aven.apache.org/xsd/maven-4.0.0.xsd "http://maven.apache.org/POM/4.0.0 http://m" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Itmuch. Cloud < / groupId > The < artifactId > spring - the cloud - microservice - study < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > pom < / packaging > <modules> <module>microservice-discovery-eureka</module> <module>microservice-provider-user</module> <module>microservice-consumer-movie-ribbon</module> <module>microservice-consumer-movie-feign</module> <module>microservice-consumer-movie-ribbon-with-hystrix</module> <module>microservice-consumer-movie-feign-with-hystrix</module> <module>microservice-hystrix-dashboard</module> <module>microservice-consumer-movie-feign-with-hystrix-stream</module> <module>microservice-hystrix-turbine</module> <module>microservice-config-server</module> <module>microservice-config-client</module> <module>microservice-config-server-eureka</module> <module>microservice-config-client-eureka</module> <module>microservice-api-gateway</module> </modules> <! <parent> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.4.0. RELEASE < / version > < / parent > < properties > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > < Java version > 1.8 < / Java version > < / properties > <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</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> 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556Copy the code

4. Service discovery

Service Discovery is one of the key principles in microservices architecture. Manually configuring each client or some form of convention is difficult and fragile. Spring Cloud provides a variety of service discovery implementations, such as Eureka, Consul, and Zookeeper. Spring Cloud supports Eureka best, Consul second, and Zookeeper last.

1.Eureka

In a production environment, we tend to configure a host for each application and use host instead of IP for queries. To get closer to the production environment and to the Docker section below, let’s first configure Host

127.0.0.1 discovery
1
Copy the code

Create a Maven project (microService-discovery-Eureka) and add the following content to pom.xml:

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "ht tp://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = aven.apache.org/xsd/maven-4.0.0.xsd "http://maven.apache.org/POM/4.0.0 http://m" > < modelVersion > 4.0.0 < / modelVersion > < artifactId > microservice - discovery - eureka < / artifactId > < packaging > jar < / packaging > <parent> <groupId>com.itmuch.cloud</groupId> <artifactId>spring-cloud-microservice-study</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / parent > < dependencies > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project> 1234567891011121314151617181920Copy the code

Declare a registry with @enableeurekaserver:

/** * Use Eureka for service discovery. * @author eacdy */ @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); }} 1234567891011Copy the code

By default, Eureka will try to register itself as a client, so in single-machine mode, we need to disable this behavior by setting the following configuration in application.yml:

Eureka: instance: hostname: discovery server: port: 8761 false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # Reference Documents: http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#_standalone_mode # reference documentation: http://my.oschina.net/buwei/blog/618756 123456789101112Copy the code

After starting the project, visit:http://discovery:8761/, as shown below. We’re going to find that we haven’t at this point

The service is registered with Eureka.

2. High availability of Eureka

Following the Eureka instructions above, we can build a simple registry. But Eureka at this point is a single point, not suitable for production environment, so how to achieve Eureka high availability? Add host name 127.0.0.1 peer1 peer2 Modify application.yml

-- Spring: profiles: peer1 # Specify profile=peer1 server: port: 8761 Eureka: instance: hostname: Peer1 # specify host name client: serviceUrl: defaultZone: http://peer2:8762/eureka/ # to register to peer2 the Eureka to above - spring: profiles: peer2 server: port: 8762 Eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8761/eureka/ 12345678910111213141516171819202122Copy the code

Start two Eureka apps respectively:

Java -jar microservice-discovery -Eureka-0.0.1 -snapshot. jar -- sprin.profiles. Active =peer1 Java -jar Microservice - discovery - eureka - 0.0.1 - the SNAPSHOT. Jar -- SPR ing. Profiles. The active = peer2 1234Copy the code

accesshttp://peer1:8761, we will find that registered-replicas already exist

There’s peer2, again, accesshttp://peer2:8762, can also be found

Registered replicas have peer1 nodes as shown below:

We try to turn off the peer2 node and visit http://peer1:8761 and see that peer2 is added to the unavaliable- Replicas column.

In this example, hostname is not required, and if it is not configured, IP will be used for lookup by default.

3. Register the service with the highly available Eureka

If the registry is highly available, then each microservice configuration simply needs to change defaultZone to the following:

eureka:
	client:
		serviceUrl:
			defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka
1234
Copy the code

Cloud-spring. IO /spring-clou…