SpringCloud系列之服务注册发现(Eureka)应用篇

前言

大家好,距离上周发布的配置中心基础使用已过去差不多一周啦,趁着周末继续完善后续SpringCloud组件的集成,本次代码基于配置中心代码的基础上进行集成。
SpringCloud Config 文章
SpringCloud Config demo01

项目版本

spring-boot-version:2.2.5.RELEASE
spring-cloud.version:Hoxton.SR3

Eureka服务端

首先构建Eureka服务端项目,最近阿里云开放了自己的项目快速构建平台,那这次就使用下吧。Alibaba initializr 操作基本上和Spirng官方提供的一模一样。搜索并选择eureka server即可。
pom主要信息

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

application.properties

# 服务应用名
spring.application.name=eureka-server
# 服务端口
server.port=9003
# 禁止将本服务注册至eureka
eureka.client.register-with-eureka=false
# 屏蔽注册信息
eureka.client.fetch-registry=false
# eureka服务地址
eureka.client.serviceUrl.defaultZone=http://localhost:9003/eureka/

EurekaServerApplication.java

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

@EnableEurekaServer 申明注解此应用为eureka服务端
项目启动成功后访问 http://localhost:9003/,即可看到Eureka服务后台
在这里插入图片描述

Eureka客户端

客户端代码基于上篇SpringCloud配置中心(Config)使用说明,文章地址及代码见本文开头,这边主要说明一些调整的地方。
先调整spring-cloud-config-server项目,我们需要将其注册至Eureka服务上,调整如下
1.pom文件
增加eureka客户端相关依赖

<!--eureka客户端-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.application.properties
配置文件增加以下配置项

# eureka服务端地址
eureka.client.serviceUrl.defaultZone=http://localhost:9003/eureka/

3.ConfigServerApplication.java
增加@EnableDiscoveryClient注解标签

@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigServerApplication

调整完后,我们再启动Config服务端,这时可以在Eureka服务后台看到config-server已经注册上了,如下图。
在这里插入图片描述
接着调整spring-cloud-config-client项目
1.pom文件
增加eureka客户端相关依赖

        <!--eureka客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2.bootstrap.properties
将原先直接通过Config服务端地址调整为基于服务发现的配置,调整如下

# 配置服务端请求地址
#spring.cloud.config.uri=http://localhost:9001/
# 开启服务发现
spring.cloud.config.discovery.enabled=true
# 配置Config服务应用名称
spring.cloud.config.discovery.service-id=config-server

3.ConfigClientApplication.java
增加@EnableDiscoveryClient注解标签

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication

启动Config客户端后,即可在Eureka服务后台看到服务已注册进来。再访问下Config 客户端暴露的接口,http://localhost:9002/getEnv,成功获取到配置信息。
在这里插入图片描述

服务访问

前面是基于SprongCloud Config配置中心集成的Eureka,接下来将介绍下如何使用Eureka中已注册的服务。
这边先构建一个System模块,用于访问Config client提供的接口服务。
部分pom信息

        <!--eureka客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--服务访问-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

application.properties

# 服务应用名称
spring.application.name=system-server
# 服务端口
server.port=9004
# eureka服务端地址
eureka.client.serviceUrl.defaultZone=http://localhost:9003/eureka/

SystemServerApplication.java
@EnableFeignClients开启Feign的支持

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SystemServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SystemServerApplication.class, args);
    }

}

ConfigService.java
@FeignClient 申明Feign客户端信息,其中name对应应用服务提供方的应用服务名称
@GetMapping(path = “/getEnv”) 对应应用服务提供方暴露的接口地址

@FeignClient(name = "config-client")
public interface ConfigService {

    @GetMapping(path = "/getEnv")
    String getEnvName();

}

SystemController.java

@RestController
@RequestMapping(value = "/web/system")
public class SystemController {

    @Autowired
    ConfigService configService;

    @RequestMapping(value = "/getEnvName", method = RequestMethod.GET)
    public String getEnvName() {
        return configService.getEnvName();
    }

}

启动系统服务模块后,访问 http://localhost:9004/web/system/getEnvName 即可访问到信息,如下图
在这里插入图片描述
访问返回结果和访问 http://localhost:9002/getEnv 接口返回是一样的,仅仅只是通过Feign访问了下 http://localhost:9002/getEnv,内部是基于通过注册在Eureka上的config-client服务调用得到的结果,内部服务地址 http://config-client/getEnv。
项目代码结构如下
在这里插入图片描述
在这里插入图片描述
本次示例代码地址
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Config Server注册Eureka注册中心的步骤如下: 1. 首先需要在pom.xml文件中添加Eureka Client的依赖,如下所示: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 2. 在应用程序的配置文件中添加以下配置: ```yaml eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ ``` 其中,`defaultZone`指定了Eureka Server的地址。 3. 在Spring Boot应用程序类上添加@EnableEurekaClient注解,以将Config Server注册Eureka Server上: ```java @EnableConfigServer @EnableDiscoveryClient @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` 4. 运行Config Server应用程序,查看Eureka Server的管理页面(http://localhost:8761)上是否已经注册成功。 5. 客户端应用程序可以通过以下方式从Config Server获配置: ```yaml spring: application: name: client-service cloud: config: uri: http://localhost:8888 fail-fast: true retry: maxAttempts: 20 multiplier: 1.5 maxInterval: 5000 label: master ``` 其中,`cloud.config.uri`指定了Config Server的地址,`spring.application.name`指定了客户端应用程序的名称,`label`指定了Git仓库的分支名称。 6. 运行客户端应用程序,即可从Config Server获配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值