1. 程式人生 > >spring cloud 服務註冊/發現/提供/呼叫 demo

spring cloud 服務註冊/發現/提供/呼叫 demo

spring cloud 服務註冊/發現

Spring Cloud使用erureka server,  然後所有需要訪問配置檔案的應用都作為一個erureka client註冊上去。eureka是一個高可用的元件,它沒有後端快取,每一個例項註冊之後需要向註冊中心傳送心跳,在預設情況下erureka server也是一個eureka client ,必須要指定一個 server。

建立Eureka Server

1).建立一個Maven工程helloworld.eureka.server, 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> <groupId>com.lx</groupId> <artifactId>helloworld.eureka.consumer</artifactId> <version>0.0
.1-SNAPSHOT</version> <packaging>jar</packaging> <name>helloworld.eureka.consumer</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5
.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>

 2). 用Spring Boot建立一個服務類EurekaServerApplication,需要一個註解@EnableEurekaServer加在springboot工程的啟動類上

/**
 * 
 */
package com.lx.helloworld.eureka.server;

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

/* author:tracy.bai
date:2018年10月18日 下午3:12:13
 **/
/**
 * @author tracy.bai
 *
 */

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

}

  3).eureka server的配置檔案appication.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一個eureka server

server:
   port: 8761

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

  4)啟動eureka server,然後訪問http://localhost:8761, 介面如下, "No instances available" 表示無client註冊

建立Eureka Client

1). 建立一個Maven工程helloworld.eureka.client, 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>

  <groupId>com.lx</groupId>
  <artifactId>helloworld.eureka.client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld.eureka.client</name>
  <url>http://maven.apache.org</url>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<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>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RC1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
</project>
View Code

2).  建立主類EurekaClientApplication

使用@EnableEurekaClient註解表明是client

/**
 * 
 */
package com.lx.helloworld.eureka.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/* author:tracy.bai
date:2018年10月18日 下午3:47:16
 **/
/**
 * @author tracy.bai
 *
 */

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

         @Value("${server.port}")
         String port;
         @RequestMapping("/")
         public String home() {
             return "hello world from port " + port;  
             
         }

         @RequestMapping("/home2")
         public String home2() {
             return "hello world from port home2 " + port;  
             
         }

}
View Code

3) eureka client的配置檔案appication.yml

eureka:
     client:
        serviceUrl:
             defaultZone: http://localhost:8761/eureka/
server:
    port: 8762
spring:
    application:
        name: service-helloworld
View Code

4). Client啟動後, 可以訪問http://localhost:8762

建立消費者

1). 建立一個Maven工程helloworld.eureka.consumer, 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>

  <groupId>com.lx</groupId>
  <artifactId>helloworld.eureka.consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld.eureka.consumer</name>
  <url>http://maven.apache.org</url>
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
  <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.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RC1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
</project>
View Code

2).  建立主類ConsumerApplication

/**
 * 
 */
package com.lx.helloworld.eureka.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

/* author:tracy.bai
date:2018年10月18日 下午4:53:41
 **/
/**
 * @author tracy.bai
 *
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
    
    public static void main(String[] args) {
            
            SpringApplication.run(ConsumerApplication.class, args);
            
        }


}
View Code

3).  寫一個針對於提供者的rest服務的介面.

/**
 * 
 */
package com.lx.helloworld.eureka.consumer;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/* author:tracy.bai
date:2018年10月18日 下午5:02:17
 **/
/**
 * @author tracy.bai
 *
 */

@FeignClient(name="service-helloworld")
public interface HelloProvider {
    
    @RequestMapping(value = "/")
    public String hello();

}
View Code

4) 將這個bean 注入到control中,像普通的訪問方式進行訪問即可.

/**
 * 
 */
package com.lx.helloworld.eureka.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/* author:tracy.bai
date:2018年10月18日 下午5:04:01
 **/
/**
 * @author tracy.bai
 *
 */

@RestController
public class HelloControl {
    
    @Autowired
    HelloProvider HelloProvider;
 
    @RequestMapping("/")
    public String index() {
        return HelloProvider.hello();
    }


}
View Code

5)配置檔案

application.properties

spring.application.name=spring-cloud-caller
server.port=8002
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
View Code

6)啟動類

/**
 * 
 */
package com.lx.helloworld.eureka.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

/* author:tracy.bai
date:2018年10月18日 下午4:53:41
 **/
/**
 * @author tracy.bai
 *
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
    
    public static void main(String[] args) {
            
            SpringApplication.run(ConsumerApplication.class, args);
            
        }


}
View Code

程式碼:https://gitee.com/baixingfa/springclouddemo/