spring cloud 学习之 服务注册和发现(Eureka)
一:服務(wù)注冊(cè)和發(fā)現(xiàn)(Eureka)
1:采用Eureka作為服務(wù)注冊(cè)和發(fā)現(xiàn)組件
2:Eureka 項(xiàng)目中 主要在啟動(dòng)類(lèi)加上 注解@EnableEurekaServer?
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run( EurekaServerApplication.class, args );} }3:eureka 是 一個(gè)高可用的組件,它沒(méi)有后端緩存,每一個(gè)實(shí)例注冊(cè)之后需要向注冊(cè)中心發(fā)送心跳
在默認(rèn)情況下erureka server也是一個(gè)eureka client ,必須要指定一個(gè) server。eureka server的配置文件appication.yml
server:port: 8761eureka:instance:hostname: localhost client: registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:application:name: eurka-server通過(guò) eureka。client.registerWithEureka:false 和 fetchRegistry:false 來(lái)聲明是eureka server
?
二:服務(wù)提供者(Eureka client)
1:當(dāng)client向server注冊(cè)時(shí),它會(huì)提供一些元數(shù)據(jù),例如主機(jī)和端口,URL,主頁(yè)等。Eureka server 從每個(gè)client實(shí)例接收心跳消息。
如果心跳超時(shí),則通常將該實(shí)例從注冊(cè)server中刪除
2:通過(guò)注解@EnableEurekaClient 表明自己是一個(gè)eurekaclient.
@SpringBootApplication @EnableEurekaClient @RestController public class ServiceHiApplication {public static void main(String[] args) {SpringApplication.run( ServiceHiApplication.class, args );}@Value("${server.port}")String port;@RequestMapping("/hi") public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) { return "hi " + name + " ,i am from port:" + port; } }3:需要在配置文件中注明自己的服務(wù)注冊(cè)中心地址,application.yml 配置文件
server:port: 8762spring:application:name: service-hieureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/需要指明 spring.application.name 服務(wù)和服務(wù)之間的相互調(diào)用一般是根據(jù)name
轉(zhuǎn)載于:https://www.cnblogs.com/but009/p/9590216.html
總結(jié)
以上是生活随笔為你收集整理的spring cloud 学习之 服务注册和发现(Eureka)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java 原生日志 java.util.
- 下一篇: Floatingip