1. 程式人生 > >Spring cloud 微服務架構 Eureka篇

Spring cloud 微服務架構 Eureka篇

ring enabled 密碼 config lns 用戶 one ima nap

1 服務發現
## 關於服務發現
在微服務架構中,服務發現(Service Discovery)是關鍵原則之一。手動配置每個客戶端或某種形式的約定是很難做的,並且很脆弱。Spring Cloud提供了多種服務發現的實現方式,例如:Eureka、Consul、Zookeeper。
Spring Cloud支持得最好的是Eureka,其次是Consul,最次是Zookeeper。

2、創建一個Maven工程(microservice-discovery-eureka),並在pom.xml中加入如下內容

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-discovery-eureka</artifactId> <packaging>jar</packaging
> <parent> <groupId>com.xujin.study</groupId> <artifactId>microservice-spring-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath /> <!-- lookup parent from repository --> </parent> <
properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> </project>

3、創建啟動類 EurekaApplication

package com.xujin.study;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

4、配置Eureka

server:
  port: 8761                    # 指定該Eureka實例的端口
security:
  basic:
    enabled: true
  user:
    name: root
    password: root
eureka:
  instance:
    hostname: discovery         # 指定該Eureka實例的主機名
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
5、啟動工程後,訪問:[http://127.0.0.1:8761/] 輸入你配置的用戶名和密碼進入Eureka頁面 技術分享

Spring cloud 微服務架構 Eureka篇