spring-cloud入门之eureka-client(服务注册)
作者:大黄蜂coder 时间:2023-12-16 22:42:51
前言
上一节我们搭建起了服务注册中心,为各个服务提供者和消费者提供一个桥梁,这一节我们搭建一个服务提供者,注册到注册中心
开源地址:https://github.com/bigbeef
新建eureka-client模块
代码结构如下:
代码编写
cppba-spring-cloud-eureka-client > pom.xml
<?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
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
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
标签:spring,cloud,eureka,client
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WPF实现半圆形导航菜单
2023-06-09 07:02:56
![](https://img.aspxhome.com/file/2023/6/129646_0s.gif)
python gstreamer实现视频快进/快退/循环播放功能
2022-07-06 10:09:20
Java实现级联下拉结构的示例代码
2023-11-03 18:22:06
Java面试题冲刺第五天--基础篇2
2023-10-07 13:17:04
![](https://img.aspxhome.com/file/2023/7/58617_0s.png)
Spring Security角色继承分析
2022-02-03 03:39:12
详解Java并发编程之原子类
2022-05-28 01:36:06
android 获取视频第一帧作为缩略图的方法
2022-11-22 19:30:25
Android控件之SeekBar的用法总结
2022-10-25 00:15:38
![](https://img.aspxhome.com/file/2023/3/137583_0s.png)
利用C#快速查出哪些QQ好友空间屏蔽了自己
2023-10-07 22:14:28
![](https://img.aspxhome.com/file/2023/0/106430_0s.jpg)
Android画板开发之撤销反撤销功能
2023-11-28 10:08:37
![](https://img.aspxhome.com/file/2023/7/126737_0s.gif)
解析Kotlin JSON格式
2021-07-16 08:09:35
![](https://img.aspxhome.com/file/2023/0/104650_0s.png)
关于Spring Data Jpa 自定义方法实现问题
2023-11-28 10:08:32
Android自定义View实现环形进度条的思路与实例
2023-07-03 05:55:47
![](https://img.aspxhome.com/file/2023/4/139044_0s.jpg)
JAVA用递归实现全排列算法的示例代码
2023-06-01 09:09:58
flutter升级3.7.3报错Unable to find bundled Java version解决
2023-07-29 16:57:52
![](https://img.aspxhome.com/file/2023/9/116149_0s.jpg)
Spring Cloud Alibaba使用Nacos作为注册中心和配置中心
2021-07-15 18:18:42
![](https://img.aspxhome.com/file/2023/3/67983_0s.png)
java8中parallelStream性能测试及结果分析
2021-09-26 15:46:43
必须要学会的JMM与volatile
2021-07-30 14:07:53
![](https://img.aspxhome.com/file/2023/2/61932_0s.png)
Java spring webmvc如何实现控制反转
2023-03-12 06:35:56
Android Handler机制详解原理
2023-10-29 22:03:44