1. Initialize Spring Cloud
1.1 what is microservice
- Microservice is an architectural style that divides a single application into small service units
1.2 what is Spring Cloud
- Spring Cloud is a collection of frameworks
- Using the development convenience of Spring Boot, the development of distributed system is simplified
1.3 common modules of spring cloud
- The main components of spring cloud include:
- Eureka: Service Registry for managing services
- Ribbon: load balancing (cluster)
- Hystix: fuse, which can prevent the avalanche effect of service.
- Zuul: service gateway, providing routing and forwarding, request filtering and other functions.
- Feign: service call to simplify the call of Http interface.
2. Registration center of erueka
2.1 overview
-
When developing large-scale projects, service providers and service callers will be very large, and the cost of managing services will increase exponentially.
Eureka will be responsible for service registration, discovery and status monitoring
-
Registration: Eureka is responsible for managing and recording information of service providers
-
Discovery: Service callers don't need to find their own services, but tell Eureka their own needs,
Then Eureka will tell you the services that meet your needs
-
Monitoring: the service provider and Eureka are monitored through the "heartbeat" mechanism. When a service provider has problems,
Eureka will naturally remove it from the list of services
-
2.2 schematic diagram
-
Eureka: it is the service registry (it can be a cluster) that exposes its address externally
-
Provider: register your own information (address, what service to provide) with Eureka after startup
-
Consumer: subscribe to Eureka service, Eureka will send the address list of all providers of the corresponding service to consumers, and update it regularly
-
Heartbeat (renewal): the provider periodically refreshes its status to Eureka via http
3. Eureka introduction case
3.1 schematic diagram
3.2 configure parent project
- Modify pom.xml to determine the version of spring cloud
<!-- 1 Determine spring boot Version--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <!--2 Confirmed version--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <spring-cloud-release.version>Greenwich.RELEASE</spring-cloud-release.version> </properties> <!-- 3 locking sprig cloud Edition--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud-release.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 4 Determine spring cloud Private warehouse--> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
3.3 configuration of Registration Center
-
Step 1: modify pom.xml file and add dependency
<dependencies> <!--web Start dependence--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Eureka Server side --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
-
Step 2: modify the application.yml file configuration
# Port number server: port: 10086 # Service name spring: application: name: eureka_demo # Configuration of eureka eureka: client: service-url: defaultZone: http://localhost:${server.port}/eureka/ register-with-eureka: false #Do not register yourself with the registry fetch-registry: false #Don't pull your own registration information from the registration center
-
Step 3: write the startup class and add comments
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * Created by Che on December 5, 2019 */ @SpringBootApplication @EnableEurekaServer //Enable eureka service public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } }
3.4 configuration service provider
- Step 1: modify pom.xml file and add eureka client dependency
<dependencies> <!--web Start dependence--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Eureka Client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--spring boot Monitor--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
-
Step 2: create the yml file and determine the location of the eureka registry
#Port number server: port: 8080 #Name spring: application: name: service #eureka configuration information eureka: client: service-url: #Registry location defaultZone: http://localhost:10086/eureka/ instance: #Display effect in web page instance-id: ${spring.application.name}:${eureka.instance.prefer-ip-address}:${server.port} prefer-ip-address: true #Show IP in registry
-
Step 3: add startup class annotation
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * Created by Che on December 5, 2019 */ @SpringBootApplication @EnableEurekaClient //Start eureka client public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class,args); } }
-
Step 4: write test data (provide services)
- Create TestController
package com.czxy.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by Che on December 5, 2019 */ @RestController @RequestMapping("/test") public class TestController { @GetMapping public ResponseEntity<String> testDemo01(){ return ResponseEntity.ok("testDemo01"); } }
3.5 configure service users
- Step 1: modify pom.xml file and add eureka client dependency
<dependencies> <!--web Start dependence--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Eureka Client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--spring boot Monitor--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
-
Step 2: create the yml file and determine the location of the eureka registry
server: port: 9090 spring: application: name: client eureka: client: service-url: #Registry location defaultZone: http://localhost:10086/eureka/ instance: #web page display effect and access path instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true
-
Step 3: add a startup class annotation
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * Created by Che on December 5, 2019 */ @SpringBootApplication @EnableEurekaClient //Open eureka client public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class,args); } }
-
Step 4: write test access service provider
- Remote call using RestTemplate
- Access path: http://localhost:9090/data
package com.czxy.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * Created by Che on December 5, 2019 */ @RestController @RequestMapping("/data") public class DataController { @GetMapping public ResponseEntity<String> dataDemo01(){ RestTemplate restTemplate = new RestTemplate(); return restTemplate.getForEntity("http://localhost:8080/test", String.class); } }