spring cloud 学习之 服务注册和发现(Eureka)
一:服務注冊和發現(Eureka)
1:采用Eureka作為服務注冊和發現組件
2:Eureka 項目中 主要在啟動類加上 注解@EnableEurekaServer?
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run( EurekaServerApplication.class, args );} }3:eureka 是 一個高可用的組件,它沒有后端緩存,每一個實例注冊之后需要向注冊中心發送心跳
在默認情況下erureka server也是一個eureka client ,必須要指定一個 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通過 eureka。client.registerWithEureka:false 和 fetchRegistry:false 來聲明是eureka server
?
二:服務提供者(Eureka client)
1:當client向server注冊時,它會提供一些元數據,例如主機和端口,URL,主頁等。Eureka server 從每個client實例接收心跳消息。
如果心跳超時,則通常將該實例從注冊server中刪除
2:通過注解@EnableEurekaClient 表明自己是一個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:需要在配置文件中注明自己的服務注冊中心地址,application.yml 配置文件
server:port: 8762spring:application:name: service-hieureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/需要指明 spring.application.name 服務和服務之間的相互調用一般是根據name
轉載于:https://www.cnblogs.com/but009/p/9590216.html
總結
以上是生活随笔為你收集整理的spring cloud 学习之 服务注册和发现(Eureka)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 原生日志 java.util.
- 下一篇: Floatingip