springcloud服務註冊與發現(二)

Spring Cloud Eureka是Spring Cloud Netflix項目下的服務治理模塊。而Spring Cloud Netflix項目是Spring Cloud的子項目之一,主要內容是對Netflix公司一系列開源產品的包裝,它爲Spring Boot應用提供了自配置的Netflix OSS整合。經過一些簡單的註解,開發者就能夠快速的在應用中配置一下經常使用模塊並構建龐大的分佈式系統。它主要提供的模塊包括:服務發現(Eureka),斷路器(Hystrix),智能路由(Zuul),客戶端負載均衡(Ribbon)等。java

服務註冊中心 :eureka-serverweb

新建一個springboot項目:eureka-server,其pom.xml配置以下spring

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

想要實現一個服務註冊中心的功能很是簡單,只須要在項目的啓動類EurekaServerApplication上使用@EnableEurekaServer註解便可springboot

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication{
 
    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaServerApplication.class)
                    .web(true).run(args);
    }
}

默認狀況下,該服務註冊中心也會將本身做爲客戶端來嘗試註冊它本身,因此咱們須要禁用它的客戶端註冊行爲,只須要在application.properties配置文件中增長以下信息:app

spring.application.name=eureka-server
server.port=1001
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

啓動EurekaServerApplication,訪問 http://localhost:9001/能夠看到Eureka的頁面。負載均衡

服務提供方 :eureka-client分佈式

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

新建一個springboot項目:eureka-client,其pom.xml配置以下:fetch

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

想要實現一個服務提供方也很簡單,只要在項目的啓動類EurekaClientApplication上使用@EnableEurekaClient註解便可ui

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
 
     public static void main(String[] args) {
            new SpringApplicationBuilder(
                    EurekaClientApplication.class)
                .web(true).run(args);
        }
}

在application.properties中進行以下配置感興趣的能夠朋友企鵝二零四二八四九二三七 

spring.application.name=eureka-client
server.port=9002
eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/

啓動EurekaClientApplication類

刷新 http://localhost:9001/,能夠看到我們的服務提供方已經註冊到了服務註冊中心

新建一個DiscoveryController 使用discoveryClient.getServices()獲取已經註冊的服務名,使用@value將配置文件中的信息賦值到ip

@RestController
public class DiscoveryController {
    
    @Autowired
    private DiscoveryClient discoveryClient;
    @Value("${server.port}")
    private String ip;
    
    @GetMapping("/client")
    public String client() {
        String services = "Services: " + discoveryClient.getServices()+" ip :"+ip;
        
        System.out.println(services);
        return services;
    }
}

訪問:http://localhost:9002/client

最後說明一下@EnableEurekaClient 與@EnableDiscoveryClient這兩個註解

 首先這個兩個註解均可以實現服務發現的功能,在spring cloud中discovery service有許多種實現(eureka、consul、 zookeeper等等)

 @EnableEurekaClient基於spring-cloud-netflix。服務採用eureka做爲註冊中心,使用場景較爲單一。

 @EnableDiscoveryClient基於spring-cloud-commons。服務採用其餘註冊中心。

相關文章
相關標籤/搜索