1. 程式人生 > >Spring Cloud第一章節(服務的註冊)

Spring Cloud第一章節(服務的註冊)


服務的註冊與發現Eureka(Finchley版本)

建立父專案section1

父專案pom檔案

<?xml version="1.0" encoding="UTF-8"?>
<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> <!-- 繼承父配置,使用Spring boot Start --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository --> </parent> <!-- 繼承父配置,使用Spring boot End --> <!-- 本專案資訊定義 Start --> <groupId>com.thirty</groupId> <artifactId>section1</artifactId> <version>0.0.1-SNAPSHOT</version
>
<packaging>pom</packaging> <name>section1</name> <description>Demo project for Spring Boot</description> <!-- 本專案資訊定義 End --> <!-- 定義配置項 Start --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <!-- 定義配置項 End --> <!-- 外掛列表 Start --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- 外掛列表 End --> <!-- 外掛管理器 End --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 外掛管理器 Start--> <!-- 編譯設定 Start--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- 編譯設定 End--> </project>

建立Eureka-Server

新增EureKa依賴

Eureka_Dependencies

pom檔案如下

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <!-- 繼承父pom檔案-->
        <groupId>com.thirty</groupId>
        <artifactId>section1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.thirty</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <!-- 新增 eureka依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

application.yml檔案如下

server:
  port: 8100 # 埠
eureka:
  instance:
    hostname: localhost # 
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
  application:
    name: eurka-server

啟動類需要新增@EnableEurekaServer註釋

建立Eureka-Client

步驟同Eureka-Service

依賴加入Eureka Discovery

pom檔案如下

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>com.thirty</groupId>
        <artifactId>section1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.thirty</groupId>
    <artifactId>eureka-client1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-client1</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency><!--客戶端依賴,用來宣告本module是一個client-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency><!--web依賴,用來訪問測試-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

application.yml檔案如下

server:
  port: 8101 #埠

spring:
  application:
    name: client1 # 服務名
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka/ #定義服務端連結地址

啟動類檔案EurekaClient1Application.java

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClient1Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClient1Application.class, args);
    }
    @RequestMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "Hello World";
    }

}

啟動專案

先啟動Eureka-server,瀏覽器訪問[http://localhost:8100/]:http://localhost:8100/ 可以看到視覺化介面,但是並無client

再啟動Eureka-Client,再次訪問[http://localhost:8100/]:http://localhost:8100/ 可以看到client已註冊

瀏覽器訪問[http://localhost:8101/hello]:http://localhost:8101/hello 可以看到Hello World