上文已经写了如何去搭建注册中心,仅有注册中心是远远不够的,所以我们需要注册到注册中心并提供服务的节点,这里称为注册服务提供者
前提
阅读上文,并成功搭建注册中心,环境无需改变
项目搭建
这里我们需要新建一个maven项目,项目名称之前没有起好,这里就参考一下,我的是springclouddemo,不要在意这些细节!
修改pom文件,参考如下:
注意:请看好这些jar包的版本号,文末我会贴出之前我搭建的两个比较简单的demo的github路径
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
81
82
83
84
85
86
|
<?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.hellxz</groupid> <artifactid>springclouddemo</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>springclouddemo</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> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>camden.sr3</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </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> <!--用于监控项目,提供项目中的状态信息--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> <!--junit测试--> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version> 4.8 . 2 </version> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-config-server</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <configuration> <source> 1.8 </source> <target> 1.8 </target> </configuration> </plugin> </plugins> </build> </project> |
虽然版本号不同于eurekaserver注册中心项目,但是经实践是可以正常使用的,请放心
新建一个启动类(每个springboot项目中都有)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.hellxz.springcloudhelloworld; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; /** * @author : hellxz * @description: eurekaclient * @date : 2018/4/13 16:57 */ @enablediscoveryclient @springbootapplication public class springclouddemoapplication { public static void main(string[] args) { springapplication.run(springclouddemoapplication. class , args); } } |
新建一个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
|
package com.hellxz.springcloudhelloworld; import org.apache.log4j.logger; import org.springframework.beans.factory.annotation.autowired; import org.springframework.cloud.client.serviceinstance; import org.springframework.cloud.client.discovery.discoveryclient; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; /** * @author : hellxz * @description: 服务提供者 * @date : 2018/4/12 11:36 */ @restcontroller public class springbootcontroller { @autowired private discoveryclient client; //注入发现客户端 private final logger logger = logger.getlogger(springbootcontroller. class ); @requestmapping (value = "/hello" , method = requestmethod.get) public string hello(){ //获取服务实例,作用为之后console显示效果 serviceinstance serviceinstance = client.getlocalserviceinstance(); logger.info( "/hello host:" +serviceinstance.gethost()+ " service_id:" +serviceinstance.getserviceid()); return "hello" ; } } |
在src/resources文件夹下创建application.yml 这次使用yaml进行配置,如果想尝试properties文件方式,请参考上文,此处配置的提供服务地址请参考注册中心的配置
1
2
3
4
5
6
7
8
9
10
|
server: port: 8080 spring: application: name: hello-service eureka: client: serviceurl: defaultzone: http: //localhost:1111/eureka/ |
好了,我们将这个项目跑在8080端口,并可以去注册中心注册服务了
先启动注册中心的项目,待其启动完毕之后,在来启动本项目。
测试
输入注册中心的url查看:localhost:1111
访问刚才配置的controller路径: http://localhost:8080/hello
如右图所示,注册成功。
此时我们就可以使用这个项目进行提供服务了
示例demo:
https://github.com/hellxz/eurekaserver
https://github.com/hellxz/eurekaclient
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/hellxz/p/8855417.html