微服務-springcloud-註冊中心

建立服務註冊中心(eureka-server)

1.建立項目,選擇 Eureka Server 別的都不要選擇,next-finish
java

2.application.yml中寫入以下信息:經過eureka.client.registerWithEureka:false和fetchRegistry:false來代表本身是一個eureka server.spring

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.EurekaServerApplication加入註解  @EnableEurekaServer瀏覽器

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

4.啓動項目,打開瀏覽器,輸入http://localhost:8761/app

No instances available 沒有服務被發現 ……^_^ 
由於沒有註冊服務固然不可能有服務被發現了。

  

建立服務client(server-user)

當client向server註冊時,它會提供一些元數據,例如主機和端口,URL,主頁等。Eureka server 從每一個client實例接收心跳消息。 若是心跳超時,則一般將該實例從註冊server中刪除。fetch

建立過程同建立註冊中心相似.net

1.經過註解@EnableEurekaClient 代表本身是一個eurekaclient.server

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ServerUserApplication {

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

}

2.配置文件中註明本身的服務註冊中心的地址,application.yml配置文件以下:blog

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: server-user

3.啓動項目get

 

打開http://localhost:8761 ,即eureka server 的網址it

相關文章
相關標籤/搜索