1、微服务与微服务架构。微服务概念如下所示:
答:微服务强调的是服务的大小,它关注的是某一个点,是具体解决某一个问题、提供落地对应服务的一个服务应用,狭意的看,可以看作Eclipse里面的一个个微服务工程/或者Module模块。
2、微服务架构是一种架构模式,微服务架构概念如下所示:
答:微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制互相协作(通常是基于HTTP协议的RESTful API,区别于dubbo的RPC远程调用)。每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外,应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语言、工具对其进行构建。
微服务和微服务架构,从技术维度理解,微服务化的核心就是将传统的一站式应用,根据业务拆分成一个一个的服务,彻底地去耦合,每一个微服务提供单个业务功能的服务,一个服务做一件事,从技术角度看就是一种小而独立的处理过程,类似进行概念,能够自行单独启动或者销毁,拥有自己独立的数据。
3、SpringCloud是什么?
答:1)、SpringCloud,基于SpringBoot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负载均衡,熔断器等组件,除了基于NetFlix的开源组件做高度抽象封装之外,还有一些选型中立的开源组件。
2)、SpringCloud利用SpringBoot的开发便利性巧妙地简化了分布式系统基础设施的开发,SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等,它们都可以用SpringBoot的开发风格做到一键启动和部署。
3)、SpringBoot并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过SpringBoot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
4)、SpringCloud等于分布式服务架构下的一站式解决方案,是各个微服务架构落地技术的集合体,俗称微服务全家桶。
4、微服务全家桶。SpringCloud集成相关优质项目推荐,这些项目是SpringCloud官方项目或者是对SpringCloud进行了有益的补充以及基于SpringCloud最佳实践。
1)、Spring Cloud Config,配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储,Git以及Subversion。 2)、Spring Cloud Bus,事件,消息总线,用于在集群(例如,配置变化事件)中传播状态变化,可以与Spring Cloud Config联合实现热部署。 3)、Eureka,云端服务发现,一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。 4)、Hystrix,熔断器,容错管理工具,旨在通过熔断机制控制服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。 5)、Zuul,Zuul是在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul相当于是设备和Netflix流应用的web网站后端所有请求的前门。 6)、Archaius,配置管理api,包含一系列配置管理api,提供动态类型化属性,线程安全配置操作,轮询框架,回调机制等等功能。 7)、Consul,封装了Consul操作,consul是一个服务发现于配置工具,与Docker容器可以无缝集成。 8)、Spring Cloud for Cloud Foundry,通过Oauth2协议绑定服务到CloudFoundry,CloudFoundry是Vmware推出的开源PaaS云平台。 9)、Spring Cloud Sleuth,日志收集工具包,封装了Dapper和log-based追踪以及Zipkin和HTrace操作,为Spring Cloud应用实现了一种分布式追踪解决方案。 10)、Spring Cloud Data Flow,大数据操作工具,作为Spring XD替代产品,它是一个混合计算模型,结合了流数据与批量数据的处理方式。 11)、Spring Cloud Security,基于spring security的安全工具包,为你的应用程序添加安全控制。 12)、Spring Cloud Zookeeper,操作Zookkeper的工具包,用于使用zookeeper方式的服务发现和配置管理。 13)、Spring Cloud Stream,数据流操作开发包,封装了与Redis,Rabbit,kafka等发送接收消息。 14)、Spring Cloud Cli,基于Spring Boot Cli,可以让你以命令行方式快速建立云组件。 15)、Ribbon,提供云端负载均衡,有多种负载均衡策略可供选择,可以配置服务发现和断路器的使用。 16)、Turbine,Turbine是聚合服务器发送事件流数据的一个工具,用来监控集群下hystrix的metrics情况。 17)、Feign,Feign是一种声明式,模板化的http客户端。 18)、Spring Cloud Task提供云端计划任务管理,任务调度。 19)、Spring Cloud Connectors,便于云端应用程序在各种PaaS平台连接到后端,如数据库和消息代理服务。 20)、Spring Cloud Cluster,提供Leadership选举,如zookeeper,redis,hazelcast,consul等等常见状态模式的抽象和实现。 21)、Spring Cloud Starters,Spring Boot式的启动项目,为Spring Cloud提供开箱即用的依赖管理。
5、SpringCloud和SpringBoot是什么关系。
1)、SpringBoot专注于快速方便的开发单个个体微服务。 2)、SpringCloud是关注全局的微服务协调整理治理框架,它将SpringBoot开发的一个个单体微服务整合并管理起来,为各个微服务之间提供,配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等集成服务。 3)、SpringBoot可以离开SpringCloud独立使用开发项目,但是SpringCloud离不开SpringBoot,属于依赖的关系。 4)、SpringBoot专注于快速、方便的开发单个微服务个体,SpringCloud关注全局的服务治理框架。
6、Eureka服务注册与发现。Eureka是什么?
答:Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。 服务注册与发现对于微服务架构来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了。功能类似于dubbo的注册中心,比如Zookeeper。Netflix在设计Eureka的时候遵循的就是AP原则。
Spring Cloud封装了Netflix公司开发的Eureka模块来实现服务注册和发现(请对比Zookeeper)。Eureka采用了C-S的设计架构。Eureka Server作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud的一些其他模块(比如Zuul)就可以通过 Eureka Server来发现系统中的其他微服务,并执行相关的逻辑。
7、Eureka包含两个组件:Eureka Server和Eureka Client。
答:Eureka Server提供服务注册服务,各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。 EurekaClient是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)。
Eureka的三大角色。Eureka Server提供服务的注册与发现。Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到。Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务。
8、构建一个聚合工程,父工程创建maven类型packaging是pom类型的,使用的jdk1.8版本,Spring Cloud版本是Dalston.SR1,SpringBoot版本是1.5.9.RELEASE。详细依赖如下所示:
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <groupId>com.bie.springcloud</groupId>
7 <artifactId>microservicecloud</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 <packaging>pom</packaging>
10
11 <properties>
12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13 <maven.compiler.source>1.8</maven.compiler.source>
14 <maven.compiler.target>1.8</maven.compiler.target>
15 <junit.version>4.12</junit.version>
16 <log4j.version>1.2.17</log4j.version>
17 <lombok.version>1.16.18</lombok.version>
18 </properties>
19
20 <dependencyManagement>
21 <dependencies>
22 <!-- spring-cloud -->
23 <dependency>
24 <groupId>org.springframework.cloud</groupId>
25 <artifactId>spring-cloud-dependencies</artifactId>
26 <version>Dalston.SR1</version>
27 <type>pom</type>
28 <scope>import</scope>
29 </dependency>
30 <!-- spring-boot -->
31 <dependency>
32 <groupId>org.springframework.boot</groupId>
33 <artifactId>spring-boot-dependencies</artifactId>
34 <version>1.5.9.RELEASE</version>
35 <type>pom</type>
36 <scope>import</scope>
37 </dependency>
38 <!-- mysql -->
39 <dependency>
40 <groupId>mysql</groupId>
41 <artifactId>mysql-connector-java</artifactId>
42 <version>5.0.4</version>
43 </dependency>
44 <!-- druid -->
45 <dependency>
46 <groupId>com.alibaba</groupId>
47 <artifactId>druid</artifactId>
48 <version>1.0.31</version>
49 </dependency>
50 <!-- mybatis -->
51 <dependency>
52 <groupId>org.mybatis.spring.boot</groupId>
53 <artifactId>mybatis-spring-boot-starter</artifactId>
54 <version>1.3.0</version>
55 </dependency>
56 <!-- logback -->
57 <dependency>
58 <groupId>ch.qos.logback</groupId>
59 <artifactId>logback-core</artifactId>
60 <version>1.2.3</version>
61 </dependency>
62 <!-- junit -->
63 <dependency>
64 <groupId>junit</groupId>
65 <artifactId>junit</artifactId>
66 <version>${junit.version}</version>
67 <scope>test</scope>
68 </dependency>
69 <!-- log4j -->
70 <dependency>
71 <groupId>log4j</groupId>
72 <artifactId>log4j</artifactId>
73 <version>${log4j.version}</version>
74 </dependency>
75 </dependencies>
76 </dependencyManagement>
77
78 <build>
79 <finalName>microservicecloud</finalName>
80 <resources>
81 <resource> <!-- 允许加载src/main/resources下面的所有配置文件 --->
82 <directory>src/main/resources</directory>
83 <filtering>true</filtering>
84 </resource>
85 </resources>
86 <plugins>
87 <plugin> <!-- 此插件负责解析和解读的,所有以$符号开头,以$符号结尾的,在上面的配置路径下面的配置文件信息,都可以读取。可以自定义添加信息。 -->
88 <groupId>org.apache.maven.plugins</groupId>
89 <artifactId>maven-resources-plugin</artifactId>
90 <configuration>
91 <delimiters>
92 <delimit>$</delimit>
93 </delimiters>
94 </configuration>
95 </plugin>
96 </plugins>
97 </build>
98
99 <!-- 父工程里面显示子模块 -->
100 <modules>
101 <module>microservicecloud-api</module>
102 <module>microservicecloud-provider-dept-8001</module>
103 <module>microservicecloud-consumer-dept-80</module>
104 <module>microservicecloud-eureka-7001</module>
105 </modules>
106
107 </project>
创建Eureka Server注册中心模块(注意,聚合工程,这里创建的是Maven Module),引入Eureka Server的依赖包,(注意,如果是SpringBoot2.x版本,对应的Spring Cloud版本已经发生了巨大变化,引入的依赖包坐标也发生了巨大变化,请仔细,认真,对比区别)如下所示:
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <parent>
7 <groupId>com.bie.springcloud</groupId>
8 <artifactId>microservicecloud</artifactId>
9 <version>0.0.1-SNAPSHOT</version>
10 </parent>
11 <artifactId>microservicecloud-eureka-7001</artifactId>
12
13 <dependencies>
14 <!-- eureka-server服务端 -->
15 <dependency>
16 <groupId>org.springframework.cloud</groupId>
17 <artifactId>spring-cloud-starter-eureka-server</artifactId>
18 </dependency>
19 <!-- 修改后立即生效,热部署 -->
20 <dependency>
21 <groupId>org.springframework</groupId>
22 <artifactId>springloaded</artifactId>
23 </dependency>
24 <dependency>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-devtools</artifactId>
27 </dependency>
28 </dependencies>
29
30 </project>
Eureka Server注册中心的配置文件application.yml,如下所示:
1 server:
2 port: 7001 # 端口号
3
4 eureka:
5 instance:
6 hostname: localhost #eureka服务端的实例名称,因为注册中心安装到本机,又是单节点Eureka Server注册中心,起名为了localhost。
7 client:
8 register-with-eureka: false # false表示不向注册中心注册自己
9 fetch-registry: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
10 service-url:
11 #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
12 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Eureka Server注册中心的启动主类,如下所示:
1 package com.bie;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6
7 @SpringBootApplication
8 @EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
9 public class MicroServiceCloudEurekaServerApplication {
10
11 public static void main(String[] args) {
12 SpringApplication.run(MicroServiceCloudEurekaServerApplication.class, args);
13 }
14
15 }
Eureka Server注册中心,主类启动以后,可以访问界面,效果如下所示:
9、Eureka Server注册中心搭建完毕以后,就可以开发服务的提供者和服务的消费者,现在将服务的提供者注册到Eureka Server注册中心。如下所示:
注意使用的Springboot的版本、SpringCloud的版本,我使用的是低版本的。不是Springboot的2.x版本。注意对比。
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <parent>
7 <groupId>com.bie.springcloud</groupId>
8 <artifactId>microservicecloud</artifactId>
9 <version>0.0.1-SNAPSHOT</version>
10 </parent>
11 <artifactId>microservicecloud-provider-dept-8001</artifactId>
12
13 <dependencies>
14 <dependency><!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
15 <groupId>com.bie.springcloud</groupId>
16 <artifactId>microservicecloud-api</artifactId>
17 <version>${project.version}</version>
18 </dependency>
19 <dependency>
20 <groupId>junit</groupId>
21 <artifactId>junit</artifactId>
22 </dependency>
23 <dependency>
24 <groupId>mysql</groupId>
25 <artifactId>mysql-connector-java</artifactId>
26 </dependency>
27 <dependency>
28 <groupId>com.alibaba</groupId>
29 <artifactId>druid</artifactId>
30 </dependency>
31 <dependency>
32 <groupId>ch.qos.logback</groupId>
33 <artifactId>logback-core</artifactId>
34 </dependency>
35 <dependency>
36 <groupId>org.mybatis.spring.boot</groupId>
37 <artifactId>mybatis-spring-boot-starter</artifactId>
38 </dependency>
39 <dependency>
40 <groupId>org.springframework.boot</groupId>
41 <artifactId>spring-boot-starter-jetty</artifactId>
42 </dependency>
43 <dependency>
44 <groupId>org.springframework.boot</groupId>
45 <artifactId>spring-boot-starter-web</artifactId>
46 </dependency>
47 <dependency>
48 <groupId>org.springframework.boot</groupId>
49 <artifactId>spring-boot-starter-test</artifactId>
50 </dependency>
51 <!-- 修改后立即生效,热部署 -->
52 <dependency>
53 <groupId>org.springframework</groupId>
54 <artifactId>springloaded</artifactId>
55 </dependency>
56 <dependency>
57 <groupId>org.springframework.boot</groupId>
58 <artifactId>spring-boot-devtools</artifactId>
59 </dependency>
60 <!-- 将微服务provider侧注册进eureka -->
61 <dependency>
62 <groupId>org.springframework.cloud</groupId>
63 <artifactId>spring-cloud-starter-eureka</artifactId>
64 </dependency>
65 <dependency>
66 <groupId>org.springframework.cloud</groupId>
67 <artifactId>spring-cloud-starter-config</artifactId>
68 </dependency>
69 <!-- actuator监控信息完善,主管监控和信息配置 -->
70 <dependency>
71 <groupId>org.springframework.boot</groupId>
72 <artifactId>spring-boot-starter-actuator</artifactId>
73 </dependency>
74 </dependencies>
75
76 </project>
修改服务提供者的配置文件application.yml。客户端注册进eureka服务列表内。
1 server:
2 port: 8001
3
4 mybatis:
5 config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
6 type-aliases-package: com.bie.po # 所有实体类别名类所在包
7 mapper-locations:
8 - classpath:mybatis/mapper/**/*.xml # mapper映射文件
9
10 spring:
11 application:
12 name: microservicecloud-provider-dept # 微服务的名称
13 datasource:
14 type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
15 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
16 url: jdbc:mysql://localhost:3306/cloudDb # 数据库名称
17 username: root
18 password: 123456
19 dbcp2:
20 min-idle: 5 # 数据库连接池的最小维持连接数
21 initial-size: 5 # 初始化连接数
22 max-total: 5 # 最大连接数
23 max-wait-millis: 200 # 等待连接获取的最大超时时间
24
25 eureka:
26 client: # 客户端注册进eureka服务列表内
27 service-url:
28 defaultZone: http://localhost:7001/eureka
29 instance:
30 instance-id: microservicecloud-provider-dept8001 # 将eureka-server注册中心的服务,显示你想看的名称
31 prefer-ip-address: true # 访问路径可以显示IP地址
32
33 info: # 微服务info内容显示详细信息
34 app.name: microservicecloud-provider-dept # 应用名称
35 company.name: www.baidu.com # 公司地址
36 build.artifactId: $project.artifactId$ # 构建项目artifactId
37 build.version: $project.version$ # 构建项目版本号
38
1 package com.bie.controller;
2
3 import java.util.List;
4
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.cloud.client.ServiceInstance;
7 import org.springframework.cloud.client.discovery.DiscoveryClient;
8 import org.springframework.web.bind.annotation.PathVariable;
9 import org.springframework.web.bind.annotation.RequestBody;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RequestMethod;
12 import org.springframework.web.bind.annotation.RestController;
13
14 import com.bie.po.Dept;
15 import com.bie.service.DeptService;
16
17 /**
18 *
19 *
20 * @author biehl
21 *
22 */
23 @RestController
24 public class DeptController {
25
26 @Autowired
27 private DeptService deptService;
28
29 // 服务的发现接口。
30 @Autowired
31 private DiscoveryClient discoveryClient;
32
33 @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
34 public boolean addDept(@RequestBody Dept dept) {
35 return deptService.addDept(dept);
36 }
37
38 @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
39 public Dept getById(@PathVariable(value = "id") Long id) {
40 Dept dept = deptService.getById(id);
41 return dept;
42 }
43
44 @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
45 public List<Dept> getAll() {
46 List<Dept> list = deptService.findAll();
47 return list;
48 }
49
50 /**
51 * 服务的发现
52 *
53 * @return
54 */
55 @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
56 public Object discovery() {
57 List<String> list = discoveryClient.getServices();
58 System.out.println("**********" + list);
59
60 List<ServiceInstance> srvList = discoveryClient.getInstances("MICROSERVICECLOUD-PROVIDER-DEPT");
61 for (ServiceInstance element : srvList) {
62 System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
63 + element.getUri());
64 }
65 return this.discoveryClient;
66 }
67
68 }
修改服务提供者的主类,@EnableEurekaClient本服务启动后会自动注册进eureka服务中。
1 package com.bie;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6
7 /**
8 *
9 *
10 * @author biehl
11 *
12 */
13 @SpringBootApplication
14 @EnableEurekaClient // 本服务启动后会自动注册进eureka服务中
15 public class MicroServiceCloudProviderApplication {
16
17 public static void main(String[] args) {
18 SpringApplication.run(MicroServiceCloudProviderApplication.class, args);
19 }
20
21 }
先启动Eureka Server服务注册中心,再启动服务的提供者。可以看到已经将服务提供者注册到Eureka Server注册中心了。
注册到注册中心的服务名称可以进行修改的,访问信息有ip提示可以修改的,微服务info信息访问可以修改的(需要添加spring-boot-starter-actuator监控依赖,定义info配置信息)。效果如下所示:
10、什么是Eureka Server注册中心的自我保护模式?
答:默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过"自我保护模式"来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。
在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false 禁用自我保护模式。建议不要禁用。切记,在Eureka-server注册中心添加该配置。
11、Eureka服务的注册与发现,Eureka服务的提供者Provider已经注册到了Eureka Server注册中心,如何进行服务的发现呢。
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <parent>
7 <groupId>com.bie.springcloud</groupId>
8 <artifactId>microservicecloud</artifactId>
9 <version>0.0.1-SNAPSHOT</version>
10 </parent>
11 <artifactId>microservicecloud-consumer-dept-80</artifactId>
12
13 <dependencies>
14 <!-- 自己定义的api -->
15 <dependency>
16 <groupId>com.bie.springcloud</groupId>
17 <artifactId>microservicecloud-api</artifactId>
18 <version>${project.version}</version>
19 </dependency>
20 <dependency>
21 <groupId>org.springframework.boot</groupId>
22 <artifactId>spring-boot-starter-web</artifactId>
23 </dependency>
24 <!-- 修改后立即生效,热部署 -->
25 <dependency>
26 <groupId>org.springframework</groupId>
27 <artifactId>springloaded</artifactId>
28 </dependency>
29 <dependency>
30 <groupId>org.springframework.boot</groupId>
31 <artifactId>spring-boot-devtools</artifactId>
32 </dependency>
33 <!-- 将微服务provider侧注册进eureka -->
34 <dependency>
35 <groupId>org.springframework.cloud</groupId>
36 <artifactId>spring-cloud-starter-eureka</artifactId>
37 </dependency>
38 <dependency>
39 <groupId>org.springframework.cloud</groupId>
40 <artifactId>spring-cloud-starter-config</artifactId>
41 </dependency>
42 </dependencies>
43
44 </project>
配置服务发现的Consumer的配置文件application.yml。
1 server:
2 port: 80
3
4 eureka:
5 client: # 客户端注册进eureka服务列表内
6 service-url:
7 defaultZone: http://localhost:7001/eureka
RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
1 package com.bie.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 /**
8 *
9 *
10 * @author biehl
11 *
12 * RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,
13 * 是Spring提供的用于访问Rest服务的客户端模板工具集
14 *
15 */
16 @Configuration
17 public class ConfigBean {
18
19 @Bean
20 public RestTemplate getRestTemplate() {
21 return new RestTemplate();
22 }
23 }
如何进行服务的发现呢,开发消费者的控制层,然后可以使用Spring封装好的RestTemplate进行访问。
1 package com.bie.controller;
2
3 import java.util.List;
4
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.web.bind.annotation.PathVariable;
7 import org.springframework.web.bind.annotation.RequestBody;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RestController;
10 import org.springframework.web.client.RestTemplate;
11
12 import com.bie.po.Dept;
13
14 /**
15 *
16 * RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问RESTFul服务模板类,
17 * 是Spring提供的用于访问Rest服务的客户端模板工具集
18 *
19 * 使用restTemplate访问restful接口非常的简单粗暴无脑。 (url, requestMap,
20 * ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
21 *
22 *
23 * @author biehl
24 *
25 */
26 @RestController
27 public class DeptControllerConsumer {
28
29 private static final String REST_URL_PREFIX = "http://localhost:8001";
30 // private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
31
32 @Autowired
33 private RestTemplate restTemplate;
34
35 /**
36 *
37 * @param dept
38 * @return
39 */
40 @RequestMapping(value = "/consumer/dept/add")
41 public boolean addDept(@RequestBody Dept dept) {
42
43 return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
44 }
45
46 /**
47 * 注意,从路径中获取到参数值,使用注解@PathVariable
48 *
49 * @param id
50 * @return
51 */
52 @RequestMapping(value = "/consumer/dept/get/{id}")
53 public Dept getById(@PathVariable(value = "id") Long id) {
54
55 return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
56 }
57
58 /**
59 *
60 * @return
61 */
62 @SuppressWarnings("unchecked")
63 @RequestMapping(value = "/consumer/dept/list")
64 public List<Dept> list() {
65
66 return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
67 }
68
69 // 测试@EnableDiscoveryClient,消费端可以调用服务发现
70 @RequestMapping(value = "/consumer/dept/discovery")
71 public Object discovery() {
72 return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
73 }
74
75 }
效果如下所示:
12、Eureka Server注册中心的集群配置。
新建microservicecloud-eureka-7002、microservicecloud-eureka-7003,再加上microservicecloud-eureka-7001可以配置成三台节点的Eureka Server注册中心集群。将microservicecloud-eureka-7001节点的pom.xml配置复制到microservicecloud-eureka-7002、microservicecloud-eureka-7003,这里省略,修改microservicecloud-eureka-7001、microservicecloud-eureka-7002、microservicecloud-eureka-7003三个主启动类的名称,避免启动的时候出现重复。修改C:\Windows\System32\drivers\etc\host的映射地址,如下所示:
1 127.0.0.1 eureka7001.com
2 127.0.0.1 eureka7002.com
3 127.0.0.1 eureka7003.com
修改三个节点的配置文件application.yml,microservicecloud-eureka-7001如下所示:
1 server:
2 port: 7001
3
4 eureka:
5 server:
6 enable-self-preservation: false
7 instance:
8 hostname: eureka7001.com #eureka服务端的实例名称
9 client:
10 register-with-eureka: false # false表示不向注册中心注册自己
11 fetch-registry: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
12 service-url:
13 #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
14 # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
15 defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
16
17
18
microservicecloud-eureka-7002如下所示:
1 server:
2 port: 7002
3
4 eureka:
5 server:
6 enable-self-preservation: false
7 instance:
8 hostname: eureka7002.com #eureka服务端的实例名称
9 client:
10 register-with-eureka: false # false表示不向注册中心注册自己
11 fetch-registry: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
12 service-url:
13 #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
14 # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
15 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
16
17
18
microservicecloud-eureka-7003如下所示:
1 server:
2 port: 7003
3
4 eureka:
5 server:
6 enable-self-preservation: false
7 instance:
8 hostname: eureka7003.com #eureka服务端的实例名称
9 client:
10 register-with-eureka: false # false表示不向注册中心注册自己
11 fetch-registry: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
12 service-url:
13 #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
14 # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
15 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
16
17
修改服务提供者microservicecloud-provider-dept-8001的application.yml配置文件,如下所示:
1 server:
2 port: 7003
3
4 eureka:
5 server:
6 enable-self-preservation: false
7 instance:
8 hostname: eureka7003.com #eureka服务端的实例名称
9 client:
10 register-with-eureka: false # false表示不向注册中心注册自己
11 fetch-registry: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
12 service-url:
13 #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
14 # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
15 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
16
17
启动microservicecloud-eureka-7001、microservicecloud-eureka-7002、microservicecloud-eureka-7003、microservicecloud-provider-dept-8001注册中心和服务提供者,可以看到监控界面,如下所示:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有