SpringBoot+Eureka註冊中心+Feign進行微服務之間呼叫

小金莫慌發表於2019-05-29

   手把手教你如何建立springboot專案並將專案註冊到註冊中心,利用feign進行微服務之間的呼叫,跟著我的步伐一步一步來。

  有個坑就是:版本問題,按照我的版本來 不然會被坑死!

目標:一個註冊中心 兩個客戶端個服務  服務之間用feign呼叫

步驟1 : 建立maven專案 作為父專案  例 eureka-parent

直接next  建立完之後 加入pom依賴

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.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>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<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>

這樣 父專案就鬧好了 刪除他的src 目錄 沒啥用

第二步:建立 eureka-server 服務端
在父專案下 建立module 專案

 


此處要選擇父專案
建立完之後 加入pom依賴

<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-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

Yml配置

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

啟動類如下 
加入註解@EnableEurekaServer 標註為註冊中心服務

package com.baidu;

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

/**
 * @Author:jinjian.song
 * @Date:2019/3/8 14:12
 * @Description:
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApp.class,args);
    }
}

至此 eureka的服務端已經搭建完畢 啟動專案 訪問 http://localhost:8761/
也面如下 沒有任何服務註冊進來

 

步驟2:建立eureka-client服務
步驟如上 選擇eureka-parent作為父專案
加入pom依賴

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

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

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

Yml 配置

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

啟動類 加註解@EnableEurekaClient

package com.baidu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:jinjian.song
 * @Date:2019/3/8 14:12
 * @Description:
 */
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApp.class,args);
    }

    @GetMapping("/list")
    public String list(@RequestParam("name") String name){
        return "Hello==="+name+", boby do you love me";
    }
}

順路在裡邊寫了一個方法 返回一個字串 以便 另一個服務用feign呼叫此服務

醬紫的話 啟動服務 訪問介面 看頁面能否返回字串 再看看註冊中心那裡 是不是多了一個服務
步驟3:建立eureka-feign-client專案 方式如上:
Pom依賴 新增open-feign 依賴

<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.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>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Yml配置 此處我將此服務也註冊到註冊中心去了

server:
  port: 8763
spring:
  application:
    name: eureka-feign-client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

啟動類

package com.baidu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @Author:jinjian.song
 * @Date:2019/3/8 14:55
 * @Description:
 */
@SpringBootApplication
@EnableFeignClients     //這是新增的註解 表示開啟feign
@EnableEurekaClient
public class EurekaFeignClient {
    public static void main(String[] args) {
        SpringApplication.run(EurekaFeignClient.class,args);
    }
}

然後 再創一個呼叫的介面

package com.baidu.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Component   //注入到spring 中 否則 無法注入的
@FeignClient(value = "eureka-client",configuration = FeignConfig.class) //value表示呼叫的服務名
public interface EurekaClientFeign {

    @GetMapping("/list")    //客戶端的方法地址
    String getMessageFromClient(@RequestParam(value = "name")String name);
}

然後寫service 注入這個介面 呼叫方法 然後controller再呼叫

@Service
public class FeignService {

    @Autowired
    EurekaClientFeign eurekaClientFeign;

   public String get(String name){
       return eurekaClientFeign.getMessageFromClient(name);
   }
}

Controller

@RestController
public class FeignController {

    @Autowired
    FeignService feignService;

    @GetMapping("/list")
    public String get(@RequestParam(defaultValue = "sjj",required = false)String name){
        return feignService.get(name);
    }

然後再去頁面訪問 此服務的list方法 就會發現呼叫了client的方法 返回了值

啟動順序 sever – clinet – feignclient
至此 feign呼叫方式 演示完畢 當然這是最簡單的方式啦 

對於初學者來說 這已經很夠用啦!!

相關文章