Spring Cloud 入门教程 - Eureka服务注册与发现

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.cloud

spring-cloud-starter-config

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

Finchley.M9

pom

import

org.springframework.boot

spring-boot-maven-plugin

spring-milestones

Spring Milestones

https://repo.spring.io/libs-milestone

false

这里用到了spring-cloud-starter-netflix-eureka-server这个eureka-server核心依赖,还有访问配置中心服务的客户端组件spring-cloud-starter-config

然后在src/main/resources下创建bootstrap.yml文件,添加如下配置:

spring:

application:

name: eureka-server

cloud:

config:

uri: http://localhost:8888

此文件配置了用以读取配置文件的应用名,即spring.application.name,它的名字对应于服务中心的文件名。另外Eureka的自动注册和发现也是基于这个参数的。然后配置了配置服务中心的uri。

新建一个Java类,cn.zxuqian.Application,使用如下代码:

package cn.zxuqian;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

这里只用@EnableEurekaServer一条注解即把该应用配置为Eureka Server。然后在配置中心的git仓库中创建eureka-server.yml文件,添加如下配置:

server:

port: 8761

eureka:

client:

register-with-eureka: false

fetch-registry: false

此文件配置了eureka-server的端口,以及关闭eureka自我注册和发现,因为如果不关闭的话,eureka在启动过程中就会去尝试注册自己,然而发现服务并没有启动就会报错。到此,eureka server就配置完了。

更新Web Client


现在我们要更新web client,使之可以被eureka自动注册和发现。首页在pom.xml中添加eureka client的依赖:

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

然后在Application类中加上@EnableDiscoveryClient注解:

@EnableDiscoveryClient

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

最后我们创建一个类用以测试服务是不是已成功注册和发现。新建一个Java类cn.zxuqian.controllers.ServiceInstanceController,添加如下代码:

package cn.zxuqian.controllers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;

import org.springframework.cloud.client.discovery.DiscoveryClient;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class ServiceInstanceController {

@Autowired

private DiscoveryClient discoveryClient;

@RequestMapping(“/service-instances/{applicationName}”)

public List serviceInstancesByApplicationName(

@PathVariable String applicationName) {

return this.discoveryClient.getInstances(applicationName);

}

}

最后

由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档

MySQL全家桶笔记

还有更多面试复习笔记分享如下

Java架构专题面试复习

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
);

}

}

最后

由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档

[外链图片转存中…(img-q35C1mSk-1714659929709)]

还有更多面试复习笔记分享如下

[外链图片转存中…(img-KSIrrr4F-1714659929710)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值