栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring-cloud入门之eureka-client(服务注册)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring-cloud入门之eureka-client(服务注册)

前言

上一节我们搭建起了服务注册中心,为各个服务提供者和消费者提供一个桥梁,这一节我们搭建一个服务提供者,注册到注册中心

开源地址:https://github.com/bigbeef

新建eureka-client模块

代码结构如下:

代码编写

cppba-spring-cloud-eureka-client > pom.xml



  4.0.0
  cppba-spring-cloud-eureka-client
  jar
  ${project.artifactId}

  
    com.cppba
    cppba-spring-cloud
    1.0.0
  

  
    
      org.springframework.cloud
      spring-cloud-starter-eureka
    
  


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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/141989.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号