springcloud简介

Posted Me*源

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud简介相关的知识,希望对你有一定的参考价值。

Springcloud简介

简介

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

 

原有的单体项目最终会被演化成下面

 

这样的架构解决了单体项目几点问题:

1、zuul网关解决了服务调用安全性的问题

2、服务注册与发现(注册中心)eureka解决了各层服务耦合问题,它是微服务架构的核心,有它才能将单体项目拆解成微服务架构

3、Eureka集群解决了微服务中,注册中心宕机产生的问题

4、Ribbon负载均衡及Feign消费者调用服务,减小了各微服务服务器的访问压力,默认采用了经典的轮询机制

5、熔断器Hystrix解决了,微服务架构中服务器雪崩现象

6、服务监控(单机Dashboard与集群turbine),方便运维人员查看微服务架构项目运行时,各个服务器的运行状态

7、服务配置中心(springcloud config),用来通过github统一管理各个微服务的配置文件(yml文件)

 

创建父工程t226microservice

父工程是一个maven项目,一般创建方式即可,父工程的主要用途是锁定pom依赖包版本。由于springcloud2X停止更新,这里我们采用稳定的低版本,配套的springboot版本为1x版本。

Pom.xml配置如下

pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5   <modelVersion>4.0.0</modelVersion>
 6 
 7   <groupId>com.yuan</groupId>
 8   <artifactId>t226microservice</artifactId>
 9   <version>1.0-SNAPSHOT</version>
10 
11   <name>t226microservice</name>
12   <properties>
13     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14     <maven.compiler.source>1.8</maven.compiler.source>
15     <maven.compiler.target>1.8</maven.compiler.target>
16     <druid.version>1.1.10</druid.version>
17   </properties>
18 
19   <!--锁定pom依赖jar包但是并不实际引入-->
20   <dependencyManagement>
21     <dependencies>
22       <dependency>
23         <groupId>org.springframework.cloud</groupId>
24         <artifactId>spring-cloud-dependencies</artifactId>
25         <version>Edgware.SR4</version>
26         <type>pom</type>
27         <scope>import</scope>
28       </dependency>
29       <dependency>
30         <groupId>org.springframework.boot</groupId>
31         <artifactId>spring-boot-dependencies</artifactId>
32         <version>1.5.13.RELEASE</version>
33         <type>pom</type>
34         <scope>import</scope>
35       </dependency>
36       <!--  连接池  -->
37       <dependency>
38         <groupId>com.alibaba</groupId>
39         <artifactId>druid-spring-boot-starter</artifactId>
40         <version>${druid.version}</version>
41       </dependency>
42     </dependencies>
43   </dependencyManagement>
44 </project>

 

创建通用模块microservice-common

通用模块主要存放实体类、工具包等被整个微服务框架所使用的代码。创建一个简单的springboot模块即可。相关代码如下

pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.1.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.yuan</groupId>
12     <artifactId>microservice-common</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>microservice-eureka-server-2001</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter</artifactId>
25         </dependency>
26 
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-test</artifactId>
30             <scope>test</scope>
31             <exclusions>
32                 <exclusion>
33                     <groupId>org.junit.vintage</groupId>
34                     <artifactId>junit-vintage-engine</artifactId>
35                 </exclusion>
36             </exclusions>
37         </dependency>
38     </dependencies>
39 
40     <build>
41         <plugins>
42             <plugin>
43                 <groupId>org.springframework.boot</groupId>
44                 <artifactId>spring-boot-maven-plugin</artifactId>
45             </plugin>
46         </plugins>
47     </build>
48 
49 </project>

MicroserviceCommonApplication

 1 package com.yuan.microservicecommon;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 6 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 7 
 8 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
 9 public class MicroserviceCommonApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(MicroserviceCommonApplication.class, args);
13     }
14 
15 }

Student

 1 package com.yuan.microservicecommon.entity;
 2 
 3 import javax.persistence.*;
 4 import java.io.Serializable;
 5 
 6 @Entity
 7 @Table(name = "t_springcloud_student")
 8 public class Student implements Serializable {
 9 
10     private static final long serialVersionUID = 1L;
11 
12     @Id
13     @GeneratedValue
14     private Integer id;
15 
16     @Column(length = 50)
17     private String name;
18 
19     @Column(length = 50)
20     private String grade;
21 
22     public Integer getId() {
23         return id;
24     }
25 
26     public void setId(Integer id) {
27         this.id = id;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38     public String getGrade() {
39         return grade;
40     }
41 
42     public void setGrade(String grade) {
43         this.grade = grade;
44     }
45 
46 
47 }

 

创建服务提供者microservice-student-provider-1001

创建一个简单的springboot模块,这里服务提供者需要操作数据库并且被浏览器所访问,固需要添加相关配置如下

pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.yuan</groupId>
 7         <artifactId>t226microservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-student-provider-1001</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-test</artifactId>
23             <scope>test</scope>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-data-jpa</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>mysql</groupId>
31             <artifactId>mysql-connector-java</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-tomcat</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>com.alibaba</groupId>
39             <artifactId>druid-spring-boot-starter</artifactId>
40         </dependency>
41         <!--  修改后立即生效,热部署  -->
42         <dependency>
43             <groupId>org.springframework</groupId>
44             <artifactId>springloaded</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-devtools</artifactId>
49         </dependency>
50         <dependency>
51             <groupId>com.yuan</groupId>
52             <artifactId>microservice-common</artifactId>
53             <version>1.0-SNAPSHOT</version>
54             <scope>compile</scope>
55         </dependency>
56         <!--添加注册中心Eureka相关配置-->
57         <dependency>
58             <groupId>org.springframework.cloud</groupId>
59             <artifactId>spring-cloud-starter-eureka</artifactId>
60         </dependency>
61         <dependency>
62             <groupId>org.springframework.cloud</groupId>
63             <artifactId>spring-cloud-starter-config</artifactId>
64         </dependency>
65         <!-- actuator监控引入 -->
66         <dependency>
67             <groupId>org.springframework.boot</groupId>
68             <artifactId>spring-boot-starter-actuator</artifactId>
69         </dependency>
70 
71     </dependencies>
72     <build>
73         <plugins>
74             <plugin>
75                 <groupId>org.springframework.boot</groupId>
76                 <artifactId>spring-boot-maven-plugin</artifactId>
77             </plugin>
78         </plugins>
79     </build>
80 
81 </project>

 

application.yml 

 1 server:
 2   port: 1001
 3   context-path: /
 4 spring:
 5   datasource:
 6     type: com.alibaba.druid.pool.DruidDataSource
 7     driver-class-name: com.mysql.jdbc.Driver
 8     url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8
 9     username: root
10     password: 12345
11   jpa:
12     hibernate:
13       ddl-auto: update
14     show-sql: true

 

MicroserviceStudentProvider1001Application.java 

 1 package com.yuan.microservicestudentprovider1001;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.domain.EntityScan;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 
 8 @EntityScan("com.yuan.*.*")
 9 @EnableEurekaClient
10 @SpringBootApplication
11 public class MicroserviceStudentProvider1001Application {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(MicroserviceStudentProvider1001Application.class, args);
15     }
16 
17 }

 

StudentRepository.java 

1 package com.yuan.microservicestudentprovider1001.repository;
2 
3 import com.yuan.microservicecommon.entity.Student;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
6 
7 public interface StudentRepository extends JpaRepository<Student, Integer>, JpaSpecificationExecutor<Student> {
8  
9 }

 

StudentService.java 

 1 package com.yuan.microservicestudentprovider1001.service;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 
 5 import java.util.List;
 6 
 7 public interface StudentService {
 8  
 9     public void save(Student student);
10      
11     public Student findById(Integer id);
12      
13     public List<Student> list();
14      
15     public void delete(Integer id);
16      
17      
18 }

 

StudentServiceImpl.java 

 1 package com.yuan.microservicestudentprovider1001.service.impl;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import com.yuan.microservicestudentprovider1001.repository.StudentRepository;
 5 import com.yuan.microservicestudentprovider1001.service.StudentService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 
 9 import java.util.List;
10 
11 @Service
12 public class StudentServiceImpl implements StudentService {
13     @Autowired
14     private StudentRepository studentRepository;
15 
16     @Override
17     public void save(Student student) {
18         studentRepository.save(student);
19     }
20 
21     @Override
22     public Student findById(Integer id) {
23         return studentRepository.findOne(id);
24     }
25 
26     @Override
27     public List<Student> list() {
28         return studentRepository.findAll();
29     }
30 
31     @Override
32     public void delete(Integer id) {
33         studentRepository.delete(id);
34     }
35 }

 

StudentProviderController.java 

 1 package com.yuan.microservicestudentprovider1001.controller;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import com.yuan.microservicestudentprovider1001.service.StudentService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.*;
 7 
 8 import java.util.List;
 9 
10 @RestController
11 @RequestMapping("/student")
12 public class StudentProviderController {
13  
14     @Autowired
15     private StudentService studentService;
16      
17     @PostMapping(value="/save")
18     public boolean save(Student student){
19         try{
20             studentService.save(student);  
21             return true;
22         }catch(Exception e){
23             return false;
24         }
25     }
26      
27     @GetMapping(value="/list")
28     public List<Student> list(){
29         return studentService.list();
30     }
31      
32     @GetMapping(value="/get/{id}")
33     public Student get(@PathVariable("id") Integer id){
34         return studentService.findById(id);
35     }
36      
37     @GetMapping(value="/delete/{id}")
38     public boolean delete(@PathVariable("id") Integer id){
39         try{
40             studentService.delete(id);
41             return true;
42         }catch(Exception e){
43             return false;
44         }
45     }
46 }

 

创建服务消费者microservice-student-consumer-80

服务消费者主要是通过restful api来调用提供者的接口,固不需要不需要操作数据库,相关配置如下

pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.yuan</groupId>
 7         <artifactId>t226microservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-student-consumer-80</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-test</artifactId>
23             <scope>test</scope>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-data-jpa</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>mysql</groupId>
31             <artifactId>mysql-connector-java</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-tomcat</artifactId>
36         </dependency>
37         <!--  修改后立即生效,热部署  -->
38         <dependency>
39             <groupId>org.springframework</groupId>
40             <artifactId>springloaded</artifactId>
41         </dependency>
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-devtools</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>com.yuan</groupId>
48             <artifactId>microservice-common</artifactId>
49             <version>1.0-SNAPSHOT</version>
50             <scope>compile</scope>
51         </dependency>
52     </dependencies>
53     <build>
54         <plugins>
55             <plugin>
56                 <groupId>org.springframework.boot</groupId>
57                 <artifactId>spring-boot-maven-plugin</artifactId>
58             </plugin>
59         </plugins>
60     </build>
61 
62 </project>

 

application.yml 

server:
  port: 81
  context-path: /

 

MicroserviceStudentConsumer80Application.java 

 1 package com.yuan.microservicestudentconsumer80;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 6 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 7 
 8 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
 9 public class MicroserviceStudentConsumer80Application {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(MicroserviceStudentConsumer80Application.class, args);
13     }
14 
15 }

SpringCloudConfig.java 

 1 package com.yuan.microservicestudentconsumer80.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.client.RestTemplate;
 6 
 7 @Configuration
 8 public class SpringCloudConfig {
 9  
10     @Bean
11     public RestTemplate getRestTemplate(){
12         return new RestTemplate();
13     }
14 }

 

StudentConsumerController.java 

 1 package com.yuan.microservicestudentconsumer80.controller;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.web.bind.annotation.*;
 6 import org.springframework.web.client.RestTemplate;
 7 
 8 import java.util.List;
 9 
10 @RestController
11 @RequestMapping("/student")
12 public class StudentConsumerController {
13 
14     private final static String SERVER_IP_PORT = "http://localhost:1001";
15  
16      @Autowired
17      private RestTemplate restTemplate;
18       
19      @PostMapping(value="/save")
20      private boolean save(Student student){
21          return restTemplate.postForObject(SERVER_IP_PORT+"/student/save", student, Boolean.class);
22      }
23       
24     @GetMapping(value="/list"以上是关于springcloud简介的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端Eureka 服务信息Eureka 发现管理Eureka 安全配置Eureka-HA(高可用) 机制Eur(代码片段

开启springcloud全家桶1:springcloud套餐简介

Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段

SpringCloud升级之路2020.0.x版-21.Spring Cloud LoadBalancer简介

SpringCloud升级之路2020.0.x版-25.OpenFeign简介与使用

Android 逆向Linux 文件权限 ( Linux 权限简介 | 系统权限 | 用户权限 | 匿名用户权限 | 读 | 写 | 执行 | 更改组 | 更改用户 | 粘滞 )(代码片段