天天看點

用戶端的搭建與注冊

用戶端的搭建與注冊

上文說道eureka服務中心後,接下來需要介紹以下服務間的調用了。

1.什麼是用戶端

微服務的每一個業務功能服務都叫做用戶端。即每一個被拆分出來的功能子產品服務。

2.搭建用戶端

用戶端就是普通的springboot項目,在其中添加一些注解就被叫做微服務的用戶端。

1.建立springboot項目

按照傳統方法建立springboot項目。

2.修改pom

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 https://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.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.baocl</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</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>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </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>

</project>      
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>      
這裡有個坑!!!!最新的client是這個jar包,以前的包和現在的不同,如果eureka上沒有注冊進去,那麼看看是不是這個原因…

3.修改EurekaClientApplication

這裡@EnableDiscoveryClient注解說明是spring cloud的用戶端。

package com.cloud;

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

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

4.編寫接口

然後咱們這裡寫一個接口,沒啥說的。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/aaa")
public class DcController {
    @Value("${server.port}")
    String port;

    @GetMapping("/dc")  
    public String dc(@RequestParam(value = "dk") String dk) {
//      try {
//          Thread.sleep(100L);
//      } catch (InterruptedException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
        //String services = "Services: " + discoveryClient.getServices();
        //System.out.println(services);
        // HttpServletRequest request = ((ServletRequestAttributes)
        // (RequestContextHolder.getRequestAttributes()));
        // ServletRequestAttributes se =
        // (ServletRequestAttributes)(RequestContextHolder.getRequestAttributes());
        // HttpServletRequest request = se.getRequest();
        return "服務提供者端口為" + port + "。。。。消費者端口為" + dk;
    }

}      

5.修改application.properties

1.必要配置

最後着重說一下配置檔案。以下是是必不可少的配置。

spring.profiles.active=dev
application-dev.properties

spring.application.name=eureka-client
server.port=2001     #指定服務端口
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/      

2.其他配置解析

發呆時間,即服務續約到期時間(預設為90s) 表示eureka server至上一次收到client的心跳之後,等待下一次心跳的逾時時間,
#在這個時間内若沒收到下一次心跳,則将移除該instance。 (上下兩個一起使用)
#eureka.instance.lease-expiration-duration-in-seconds=10
心跳時間,即服務續約間隔時間(預設為30s)
#eureka.instance.lease-renewal-interval-in-seconds = 5

#連接配接 Eureka Server 的逾時時間,機關:秒
#eureka.client.eureka-server-connect-timeout-seconds=10
#從Eureka伺服器端擷取注冊資訊的間隔時間,機關:秒 預設30
#eureka.client.registery-fetch-interval-seconds=1000
#啟動服務注冊 預設true
#eureka.client.register-with-eureka = true


#擷取執行個體時是否過濾,隻保留UP狀态的執行個體 預設true
#eureka.client.filter-only-up-instances = true
開啟健康檢查(需要spring-boot-starter-actuator依賴)
#eureka.client.healthcheck.enabled  = true
#擷取注冊資訊并緩存到本地
#eureka.client.fetch-registry=  true      

這裡還有一點 我們可以更改application.properties确定我們啟動時啟動的配置檔案,同時可以在啟動一個項目後修改application.properties,這樣我們自己就可以建立一個叢集環境。

用戶端的搭建與注冊

然後,我們啟動了三次(三個配置的端口号需要不同),這樣我們就建立了一個叢集了,然後eureka上就可以看到我們用戶端的資訊了,如下圖!

用戶端的搭建與注冊