前言
上一节我们搭建起了服务注册中心,为各个服务提供者和消费者提供一个桥梁,这一节我们搭建一个服务提供者,注册到注册中心
开源地址:https://github.com/bigbeef
代码结构如下:
代码编写
cppba-spring-cloud-eureka-client > pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?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> <artifactid>cppba-spring-cloud-eureka-client</artifactid> <packaging>jar</packaging> <name>${project.artifactid}</name> <parent> <groupid>com.cppba</groupid> <artifactid>cppba-spring-cloud</artifactid> <version> 1.0 . 0 </version> </parent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> </dependencies> </project> |
springcloudeurekaclientapplication.java
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
|
package com.cppba; import org.springframework.beans.factory.annotation.value; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.netflix.eureka.enableeurekaclient; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; @springbootapplication @enableeurekaclient @restcontroller public class springcloudeurekaclientapplication { public static void main(string[] args) { springapplication.run(springcloudeurekaclientapplication. class , args); } @value ( "${server.port}" ) private string port; @requestmapping ( "/hi" ) public string sayhi( @requestparam string name) { return "hi " + name + ",i am from port:" + port; } } |
application.properties
1
2
3
4
5
|
server.port= 8764 eureka.client.serviceurl.defaultzone=http: //localhost:8761/eureka/ spring.application.name=service-say |
到此项目搭建完成
启动项目
启动eureka-client之前,我们先启动上一节搭建的eureka-server,两个服务都启动好以后,我们访问注册中心,可以看到:
说明我们服务注册成功!访问一下http://127.0.0.1:8764/hi?name=hornet
到此,eureka-client(服务发现)项目搭建成功。
参考项目:https://github.com/bigbeef/cppba-spring-cloud
github地址:https://github.com/bigbeef
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/45f344d2c34b