一、spring cloud简介
spring cloud是一个基千springboot实现的微服务架构开发 工具。它为微服务架构中涉及的 配置管理、服务治理、 断路器、 智能路由、微代理、 控制总线、 全局锁、 决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。
spring cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品,还可能会新增),如下所述。
spring cloud config: 配置管理工具、spring cloud netflix: 核心组件、spring cloud bus: 事件、消息总线等等。
二、spring cloud eureka
spring cloud eureka 是 spring cloud netflix 微服务套件中的一部分, 它基于 netflixeureka 做了二次封装, 主要负责完成微服务架构中的服务治理功能。 spring cloud 通过为eureka 增加了 spring boot 风格的自动化配置,我们只需通过简单引入依赖和注解配置就能让 spring boot 构建的微服务应用轻松地与 eureka 服务治理体系进行整合。
服务治理可以说是微服务架构中最为核心和基础的模块, 它主要用来实现各个微服务实例的自动化注册与发现。
三、实例
(1)工具:intellij idea
(2)新建一个空项目
(3)新建一个eureka server,module,名为:eurekaserver
工程右键->创建module-> 选择spring initialir ->填好项目名,下一步->,如图选择eureka server:
(3-1)pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> |
(3-2)application.yml
1
2
3
4
5
6
7
8
9
10
11
|
server: port: 5555 eureka: instance: hostname: localhost client: registerwitheureka: false fetchregistry: false serviceurl: defaultzone: http: //${eureka.instance.hostname}:${server.port}/eureka/ |
备注:eureka.client.register-with-eureka: 由于该应用为注册中心,所以设置为 false, 代表不向注册中心注册自己。
eureka.client.fetch-registry: 由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为 false。
(3-3)入口类
1
2
3
4
5
6
7
8
9
10
11
12
|
import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.netflix.eureka.server.enableeurekaserver; @enableeurekaserver @springbootapplication public class eurekaserverapplication { public static void main(string[] args) { springapplication.run(eurekaserverapplication. class , args); } } |
(3-4)启动测试
在完成了上面的配置后,启动应用并访问 http://localhost:5555/。可以看到如下图所示的 eureka 信息面板, 其中 instances currently registered with eureka 栏是空的, 说明该注册中心还没有注册任何服务。
(4)注册服务提供者在完成了服务注册中心的搭建之后,接下来我们尝试将一个既有的 spring boot 应用加入 emeka 的服务治理体系中去。
(5)再新建一个eureka client,module,名为:helloserver,这个helloserver作为eurekaserver的子model
(6)改造父model和子model的pom配置(6-1)eurekaserver的pom.xml配置:
1
2
3
4
|
<packaging>pom</packaging> <modules> <module>../helloserver</module> </modules> |
(6-2)eurekaserver的全部pom.xml:
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>demoeurekaserver</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>pom</packaging> <modules> <module>../helloserver</module> </modules> <name>eurekaserver</name> <description>demo project for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 9 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> <spring-cloud.version>edgware.release</spring-cloud.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>${spring-cloud.version}</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> |
(6-3)helloserver的pom.xml配置:
1
2
3
4
5
6
|
<parent> <groupid>com.example</groupid> <artifactid>demoeurekaserver</artifactid> <version> 0.0 . 1 -snapshot</version> <relativepath>../eurekaserver/pom.xml</relativepath> </parent> |
(6-4)helloserver的全部pom.xml配置:
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <parent> <groupid>com.example</groupid> <artifactid>demoeurekaserver</artifactid> <version> 0.0 . 1 -snapshot</version> <relativepath>../eurekaserver/pom.xml</relativepath> </parent> <artifactid>helloserver</artifactid> <packaging>jar</packaging> <name>helloserver</name> <description>demo project for spring boot</description> <properties> <start- class >com.example.helloserver.helloserverapplication</start- class > </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>${spring-cloud.version}</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> |
(6-5)helloserver的application.yml配置:
1
2
3
4
5
6
7
8
9
10
|
server: port: 5556 spring: application: name: helloserver eureka: client: serviceurl: defaultzone: http: //localhost:5555/eureka/ |
(6-6)helloserver的启动类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@enableeurekaserver @springbootapplication @restcontroller public class helloserverapplication { private final logger log = (logger) loggerfactory.getlogger(helloserverapplication. class ); @autowired private discoveryclient client; @requestmapping (name = "/hello" , method = requestmethod.get) public string index() { serviceinstance instance = client.getlocalserviceinstance(); log.info( "/hello, host:" + instance.gethost() + ",service_id:" + instance.getserviceid()); return "hello springcloud~" ; } public static void main(string[] args) { springapplication.run(helloserverapplication. class , args); } } |
(7)分别启动eurekaserver和helloserver,并测试:
(7-1)访问eurekaserver:(可以很清楚的看到helloserver信息)
(7-2)访问helloserver:
(7-3)查看helloserver控制台信息:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/smartdt/article/details/79002719