spring-cloud入门之eureka-client(服务注册)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
前言
上一节我们搭建起了服务注册中心,为各个服务提供者和消费者提供一个桥梁,这一节我们搭建一个服务提供者,注册到注册中心
开源地址: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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。
相关文章
Mybatis使用@one和@Many实现一对一及一对多关联查询
本文主要介绍了Mybatis使用@one和@Many实现一对一及一对多关联查询,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-09-09Java中的字符流FileReader与FileWriter详解
这篇文章主要介绍了Java中的字符流FileReader与FileWriter详解,在Java中,使用Unicode约定存储字符,字符流自动允许我们逐字符读/写数据,有助于执行16位Unicode的输入和输出,它是以reader和writer结尾的,需要的朋友可以参考下2023-10-10IntelliJ IDEA2023中运行Spring Boot找不到VM options进
这篇文章主要介绍了IntelliJ IDEA2023中运行Spring Boot找不到VM options进行端口的修改的问题解决,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友可以参考下2023-11-11
最新评论