微服務------Spring-Cloud-Eureka搭建客戶端(服務提供者,把服務註冊到註冊中心上)

項目名稱爲spring-cloud-eureka-client
一、使用IDEA工具創建一個SpringBoot項目
注意:這裏要勾選Web和Eureka Discovery選項
 
 
主要是爲了引入Eureka客戶端的依賴如下:(選擇Eureka Discovery之後,會自動加入依賴在pom.xml中)
<groupId>com.etc</groupId>
<artifactId>spring-cloud-eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-eureka-client</name>

二、創建完之後,項目如下:

 
pom.xml配置如下:
<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.etc</groupId>
    <artifactId>spring-cloud-eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-cloud-eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.M3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>${spring-cloud.version}</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>

三、修改配置文件,刪除application.properties文件,新建application.yml文件

server:
    port:
        8762
spring:
    application:
        name:
            service-hello #服務名爲 service-hello,將會被調用者使用
eureka:
    client:
        service-url:
            defaultZone:
                http://localhost:8761/eureka/

注意:application.yml的格式一定要注意,格式不正確時,啓動報錯如下:

 
 
正確格式是:
 
四、在啓動類上添加註解
package com.etc.springcloudeurekaclient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Eureka客戶端,提供服務,進行服務註冊
* @EnableEurekaClient 和@EnableDiscoveryClient共同點就是:都是能夠讓註冊中心發現,掃描到該服務。
* 不同點:@EnableEurekaClient只適用於Eureka作爲註冊中心,@EnableDiscoveryClient可以是其他註冊中心。
*/
@EnableEurekaClient //表示他是一個 Eureka 客戶端,它會在註冊中心註冊自己
@SpringBootApplication
public class SpringCloudEurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaClientApplication.class, args);
    }
}

五、新建一個Controller類,定義一個方法

package com.etc.springcloudeurekaclient.controller;

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

@RestController
public class HelloController {
    @Value("${server.port}")
    String port;
    @RequestMapping("/hello")
    public String hello(@RequestParam(value="name",defaultValue = "zoey") String name){
        return "hello "+name+",i am port:"+port;
    }
}

六、啓動完成

 
七、重新訪問Eureka
 
 八、頁面訪問方法如下:
 
啓動SpringCloudEureka 報錯:
 
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
 
解決方法:
    先仔細檢查,指定的註冊中心(此處指的是之前創建的項目Spring-Cloud-Eureka) eureka.client.serviceUrl.defaultZone的地址是否正確,端口號有沒有寫錯。
     然後,在application.yml 添加以下配置:
#Eureka服務註冊中心也會將自己作爲客戶端來嘗試註冊它自己,所以我們需要禁用它的客戶端註冊行爲。
 
eureka:
    client:
        register-with-eureka: false
        fetch-registry: false
    service-url:
        defaultZone:
            http://${eureka.instance.hostname}:${server.port}/eureka/

客戶端要正常啓動,首先要保證註冊中心的代碼是正確的,可以正常啓動的

 

參考博客:https://blog.csdn.net/forezp/article/details/81040925

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章