微服务架构

释放双眼,带上耳机,听听看~!

1.微服务与微服务架构

微服务
强调的是服务的大小,它关注的是某一个点,是具体解决某一个问题/提供落地对应服务的一个服务应用,
狭意的看,可以看作Eclipse里面的一个个微服务工程/或者Module。

微服务架构

  微服务架构是⼀种架构模式,它提倡将单⼀应⽤程序划分成⼀组⼩的服务,服务之间互相协调、互相配合,为⽤户提供最终价值。每个服务运⾏在其独⽴的进程中,服务与服务间采⽤轻量级的通信机制互相协作(通常是基于HTTP协议的RESTful API)。每个服务都围绕着具体业务进⾏构建,并且能够被独⽴的部署到⽣产环境、类⽣产环境等。另外,应当尽量避免统⼀的、集中式的服务管理机制,对具体的⼀个服务⽽⾔,应根据业务上下⽂,选择合适的语⾔、⼯具对其进⾏构建。

 

2.使用spring cloud搭建微服务

      通过idea创建微服务。

     一个Project带着多个Module子模块

         包括:

             microservicecloud-api:封装的整体Entity/接口/公共配置等

             microservicecloud-provider-dept-8001:微服务落地的服务提供者

            microservicecloud-consumer-dept-80:微服务调用的客户端使用

  步骤:

          1.首先创建maven工程cloud-parent,Packageing是pom模式,主要是定义POM文件,将后续各个子模块公用的jar包等统一提出来,类似一个抽象父类

         pom文件:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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.el.cloud</groupId>
8  <packaging>pom</packaging>
9  <modules>
10    <module>cloud-api</module>
11  </modules>
12  <artifactId>cloud-parent</artifactId>
13  <version>1.0-SNAPSHOT</version>
14
15  <name>cloud-parent</name>
16  <!-- FIXME change it to the project's website -->
17  <url>http://www.example.com</url>
18
19  <properties>
20    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21    <maven.compiler.source>1.8</maven.compiler.source>
22    <maven.compiler.target>1.8</maven.compiler.target>
23    <junit.version>4.12</junit.version>
24    <log4j.version>1.2.17</log4j.version>
25    <lombok.version>1.16.18</lombok.version>
26  </properties>
27
28  <dependencyManagement>
29    <dependencies>
30      <dependency>
31        <groupId>org.springframework.cloud</groupId>
32        <artifactId>spring-cloud-dependencies</artifactId>
33        <version>Dalston.SR1</version>
34        <type>pom</type>
35        <scope>import</scope>
36      </dependency>
37      <dependency>
38        <groupId>org.springframework.boot</groupId>
39        <artifactId>spring-boot-dependencies</artifactId>
40        <version>1.5.9.RELEASE</version>
41        <type>pom</type>
42        <scope>import</scope>
43      </dependency>
44      <dependency>
45        <groupId>mysql</groupId>
46        <artifactId>mysql-connector-java</artifactId>
47        <version>5.0.4</version>
48      </dependency>
49      <dependency>
50        <groupId>com.alibaba</groupId>
51        <artifactId>druid</artifactId>
52        <version>1.0.31</version>
53      </dependency>
54      <dependency>
55        <groupId>org.mybatis.spring.boot</groupId>
56        <artifactId>mybatis-spring-boot-starter</artifactId>
57        <version>1.3.0</version>
58      </dependency>
59      <dependency>
60        <groupId>ch.qos.logback</groupId>
61        <artifactId>logback-core</artifactId>
62        <version>1.2.3</version>
63      </dependency>
64      <dependency>
65        <groupId>junit</groupId>
66        <artifactId>junit</artifactId>
67        <version>${junit.version}</version>
68        <scope>test</scope>
69      </dependency>
70      <dependency>
71        <groupId>log4j</groupId>
72        <artifactId>log4j</artifactId>
73        <version>${log4j.version}</version>
74      </dependency>
75    </dependencies>
76  </dependencyManagement>
77
78</project>
79
80

   2.新建cloud-api 

   pom文件

    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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    <parent>
6        <artifactId>cloud-parent</artifactId>
7        <groupId>com.el.cloud</groupId>
8        <version>1.0-SNAPSHOT</version>
9    </parent>
10    <modelVersion>4.0.0</modelVersion>
11
12    <groupId>com.el.cloud</groupId>
13    <artifactId>cloud-api</artifactId>
14
15    <name>cloud-api</name>
16    <!-- FIXME change it to the project's website -->
17    <url>http://www.example.com</url>
18
19
20    <dependencies>
21        <dependency>
22            <groupId>org.projectlombok</groupId>
23            <artifactId>lombok</artifactId>
24        </dependency>
25    </dependencies>
26
27</project>
28
29

创建完成后请回到父工程查看pom文件变化

   微服务架构

新建部门Entity且配合lombok使用

  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1package com.el.cloud.entity;
2
3import lombok.Data;
4import lombok.NoArgsConstructor;
5import lombok.experimental.Accessors;
6
7import java.io.Serializable;
8
9/**
10 * @Auther: roman.zhang
11 * @Date: 2019/3/18 22:13
12 * @Version:V1.0
13 * @Description:Dept
14 */
15@SuppressWarnings("serial")
16@NoArgsConstructor
17@Data
18@Accessors(chain=true)
19public class Dept implements Serializable {
20    private Long  deptno;   //主键
21    private String  dname;   //部门名称
22    private String  db_source;//来自那个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库
23    public Dept(String dname)
24    {
25        super();
26        this.dname = dname;
27    }
28
29}
30
31

3.新建服务提供者cloud-provider-8001

微服务架构

  pom文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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    <parent>
6        <artifactId>cloud-parent</artifactId>
7        <groupId>com.el.cloud</groupId>
8        <version>1.0-SNAPSHOT</version>
9    </parent>
10    <modelVersion>4.0.0</modelVersion>
11
12    <groupId>com.el.cloud</groupId>
13    <artifactId>cloud-provider-8001</artifactId>
14
15    <name>cloud-provider-8001</name>
16    <!-- FIXME change it to the project's website -->
17    <url>http://www.example.com</url>
18
19    <dependencies>
20        <dependency><!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
21            <groupId>com.el.cloud</groupId>
22            <artifactId>cloud-api</artifactId>
23            <version>${project.version}</version>
24        </dependency>
25        <dependency>
26            <groupId>junit</groupId>
27            <artifactId>junit</artifactId>
28        </dependency>
29        <dependency>
30            <groupId>mysql</groupId>
31            <artifactId>mysql-connector-java</artifactId>
32        </dependency>
33        <dependency>
34            <groupId>com.alibaba</groupId>
35            <artifactId>druid</artifactId>
36        </dependency>
37        <dependency>
38            <groupId>ch.qos.logback</groupId>
39            <artifactId>logback-core</artifactId>
40        </dependency>
41        <dependency>
42            <groupId>org.mybatis.spring.boot</groupId>
43            <artifactId>mybatis-spring-boot-starter</artifactId>
44        </dependency>
45        <dependency>
46            <groupId>org.springframework.boot</groupId>
47            <artifactId>spring-boot-starter-jetty</artifactId>
48        </dependency>
49        <dependency>
50            <groupId>org.springframework.boot</groupId>
51            <artifactId>spring-boot-starter-web</artifactId>
52        </dependency>
53        <dependency>
54            <groupId>org.springframework.boot</groupId>
55            <artifactId>spring-boot-starter-test</artifactId>
56        </dependency>
57        <!-- 修改后立即生效,热部署 -->
58        <dependency>
59            <groupId>org.springframework</groupId>
60            <artifactId>springloaded</artifactId>
61        </dependency>
62        <dependency>
63            <groupId>org.springframework.boot</groupId>
64            <artifactId>spring-boot-devtools</artifactId>
65        </dependency>
66    </dependencies>
67
68</project>
69
70

修改项目结构

 微服务架构

YML文件

    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
1server:
2  port: 8001
3  
4mybatis:
5  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
6  type-aliases-package: com.el.cloud.entities     # 所有Entity别名类所在包
7  mapper-locations:
8  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
9    
10spring:
11   application:
12    name: microservicecloud-dept
13   datasource:
14    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
15    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
16    url: jdbc:mysql://192.168.137.131:3306/cloudDB01              # 数据库名称
17    username: root
18    password: root
19    dbcp2:
20      min-idle: 5                                           # 数据库连接池的最小维持连接数
21      initial-size: 5                                       # 初始化连接数
22      max-total: 5                                          # 最大连接数
23      max-wait-millis: 200                                  # 等待连接获取的最大超时时间
24      
25eureka:
26      
27  instance:
28    instance-id: cloud-provider8001
29    prefer-ip-address: true     #访问路径可以显示IP地址    
30
31info:
32  app.name: cloud-provider
33  company.name: www.elitesland.com
34  build.artifactId: $project.artifactId$
35  build.version: $project.version$
36
37
38

工程src/main/resources目录下新建mybatis文件夹后新建mybatis.cfg.xml文件

微服务架构

 DeptDao部门接口

    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1package com.el.cloud.dao;
2
3import com.el.cloud.entity.Dept;
4
5import java.util.List;
6
7/**
8 * @Auther: roman.zhang
9 * @Date: 2019/3/19 10:45
10 * @Version:V1.0
11 * @Description:DeptDao
12 */
13@Mapper
14public interface DeptDao {
15    boolean addDept(Dept dept);
16
17    Dept findById(Long id);
18
19    List<Dept> findAll();
20
21}
22
23

工程src/main/resources/mybatis目录下新建mapper文件夹后再建DeptMapper.xml

 微服务架构

DeptService部门服务接口

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1package com.el.cloud.service;
2
3import com.el.cloud.entity.Dept;
4
5import java.util.List;
6
7/**
8 * @Auther: roman.zhang
9 * @Date: 2019/3/19 10:51
10 * @Version:V1.0
11 * @Description:DeptService
12 */
13public interface DeptService {
14     boolean add(Dept dept);
15     Dept    get(Long id);
16     List<Dept> list();
17
18}
19
20

DeptServiceImpl部门服务接口实现类

   


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1package com.el.cloud.service.impl;
2
3import com.el.cloud.dao.DeptDao;
4import com.el.cloud.entity.Dept;
5import com.el.cloud.service.DeptService;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Service;
8
9import java.util.List;
10
11/**
12 * @Auther: roman.zhang
13 * @Date: 2019/3/19 10:53
14 * @Version:V1.0
15 * @Description:DeptServiceImpl
16 */
17@Service
18public class DeptServiceImpl implements DeptService {
19    @Autowired
20    private DeptDao dao ;
21
22
23    @Override
24    public boolean add(Dept dept) {
25        return dao.addDept(dept);
26    }
27
28    @Override
29    public Dept get(Long id) {
30        return dao.findById(id);
31    }
32
33    @Override
34    public List<Dept> list() {
35        return dao.findAll();
36    }
37}
38
39

DeptController部门微服务提供者REST

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
1package com.el.cloud.controller;
2
3import com.el.cloud.entity.Dept;
4import com.el.cloud.service.DeptService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.stereotype.Controller;
7import org.springframework.web.bind.annotation.PathVariable;
8import org.springframework.web.bind.annotation.RequestBody;
9import org.springframework.web.bind.annotation.RequestMapping;
10import org.springframework.web.bind.annotation.RequestMethod;
11
12import java.util.List;
13
14/**
15 * @Auther: roman.zhang
16 * @Date: 2019/3/19 10:55
17 * @Version:V1.0
18 * @Description:DeptController
19 */
20@RestController
21public class DeptController {
22    @Autowired
23    private DeptService service;
24
25    @RequestMapping(value="/dept/add",method= RequestMethod.POST)
26    public boolean add(@RequestBody Dept dept)
27    {
28        return service.add(dept);
29    }
30
31    @RequestMapping(value="/dept/get/{id}",method=RequestMethod.GET)
32    public Dept get(@PathVariable("id") Long id)
33    {
34        return service.get(id);
35    }
36
37    @RequestMapping(value="/dept/list",method=RequestMethod.GET)
38    public List<Dept> list()
39    {
40        return service.list();
41    }
42
43
44
45
46}
47
48

DeptProvider8001_App主启动类DeptProvider8001_App

   


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1package com.el.cloud;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6/**
7 * Hello world!
8 *
9 */
10@SpringBootApplication
11public class DeptProvider8001_App
12{
13    public static void main( String[] args )
14    {
15        SpringApplication.run(DeptProvider8001_App.class,args);
16    }
17}
18
19

测试

  微服务架构   

微服务架构

3.创建服务消费者

   新建cloud-consumer-80

   pom文件:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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    <parent>
6        <artifactId>cloud-parent</artifactId>
7        <groupId>com.el.cloud</groupId>
8        <version>1.0-SNAPSHOT</version>
9    </parent>
10    <modelVersion>4.0.0</modelVersion>
11
12    <groupId>com.el.cloud</groupId>
13    <artifactId>cloud-consumer-80</artifactId>
14
15    <name>cloud-consumer-80</name>
16    <!-- FIXME change it to the project's website -->
17    <url>http://www.example.com</url>
18
19    <description>部门微服务消费者</description>
20
21    <dependencies>
22        <dependency><!-- 自己定义的api -->
23            <groupId>com.el.cloud</groupId>
24            <artifactId>cloud-api</artifactId>
25            <version>${project.version}</version>
26        </dependency>
27        <dependency>
28            <groupId>org.springframework.boot</groupId>
29            <artifactId>spring-boot-starter-web</artifactId>
30        </dependency>
31        <!-- 修改后立即生效,热部署 -->
32        <dependency>
33            <groupId>org.springframework</groupId>
34            <artifactId>springloaded</artifactId>
35        </dependency>
36        <dependency>
37            <groupId>org.springframework.boot</groupId>
38            <artifactId>spring-boot-devtools</artifactId>
39        </dependency>
40
41    </dependencies>
42</project>
43
44

yml文件:


1
2
3
1server:
2  port: 80
3

在com.el.cloud.configure下创建一个ConfigBean类:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1package com.el.cloud.configure;
2
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5import org.springframework.web.client.RestTemplate;
6
7/**
8 * @Auther: roman.zhang
9 * @Date: 2019/3/19 13:59
10 * @Version:V1.0
11 * @Description:ConfigBean
12 */
13@Configuration
14
15public class ConfigBean {
16
17    @Bean
18    public RestTemplate getRestTemplate(){
19        return new RestTemplate();
20    }
21
22}
23
24

RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集。

创建消费者controller

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
1package com.el.consumer.cloud.controller;
2
3import com.el.consumer.cloud.entities.Dept;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.web.bind.annotation.PathVariable;
6import org.springframework.web.bind.annotation.RequestMapping;
7import org.springframework.web.bind.annotation.RestController;
8import org.springframework.web.client.RestTemplate;
9
10import java.util.List;
11
12/**
13 * @Auther: roman.zhang
14 * @Date: 2019/3/19 14:02
15 * @Version:V1.0
16 * @Description:DeptController_Consumer
17 */
18@RestController
19public class DeptController_Consumer {
20    private static final String REST_URL_PREFIX = "http://localhost:8001";
21
22    @Autowired
23    private RestTemplate restTemplate;
24
25    @RequestMapping(value = "/consumer/dept/add")
26    public boolean add(Dept dept){
27        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,boolean.class);
28    }
29
30    @RequestMapping(value="/consumer/dept/get/{id}")
31    public Dept get(@PathVariable("id") Long id){
32        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
33    }
34
35    @RequestMapping(value="/consumer/dept/list")
36    public List<Dept> list(){
37        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
38    }
39}
40
41

测试:

   微服务架构

微服务架构

 

这样一个简单的微服务架构就是实现了。

项目源码:微服务架构源码

给TA打赏
共{{data.count}}人
人已打赏
安全网络

CDN安全市场到2022年价值76.3亿美元

2018-2-1 18:02:50

安全经验

GitLab 发布安全更新版本:10.3.4, 10.2.6 和 10.1.6

2018-1-17 11:12:22

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索