利用springcloud搭建微服務集成中心

大綱
  • 註冊中心
  • 功能服務one
  • 功能服務two

整體目錄結構如下



這裏整個功能是一個maven項目,註冊中心與功能服務都是maven項目裏面的模塊。

註冊中心
利用eureka搭建服務註冊中心模塊server-eureka

這是一個空的springboot項目,主要就是把他當做服務註冊中心使用
1.springboot運行入口代碼

package org.server.eureka;

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

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

通過@EnableEurekaServer註解將應用標記爲服務註冊中心

2.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>
  <packaging>jar</packaging>
  <name>server-eureka</name>
  <description>eureka-server project for Spring Boot</description>
  <groupId>cn.ysh.cloud</groupId>
  <artifactId>server-eureka</artifactId>
  <parent>
      <groupId>cn.ysh</groupId>
      <artifactId>cloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </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.SR1</spring-cloud.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>
</project>

最主要的是要引入spring-cloud-starter-netflix-eureka-server這個jar包,用於支持以eureka作爲服務註冊中心

3.eureka服務的基本配置

server:
 port: 8761(指定應用端口)
spring:
 application:
   name: eureka-server(指定應用名稱)
eureka:
 instance:
   hostname: localhost
 client:
   register-with-eureka: false (不註冊自己)
   fetch-registry: false(不開啓檢索)
   service-url:
     defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

做好這些配置,就完成了。

啓動eureka-server,界面如下:

標紅的應用中心顯示目前沒有服務註冊到eureka中心

搭建第一個服務應用client-one-eureka

1.springboot入口代碼

package org.client.one.eureka;

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

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

兩個重要註解@EnableEurekaClient、@EnableFeignClients。
@EnableEurekaClient標記爲Eureka客服端(就是標記爲Eureka一個服務)
@EnableFeignClients開啓Feign調用服務的方式。(調用服務有兩種方式,一種是ribbon,一種是Feign),我這裏想實現的是基於Feign實現client-one-eureka調用client-two-eureka服務方法。

2.pom依賴

<?xml version="1.0"?>
<project
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven->4.0.0.xsd"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
      <groupId>cn.ysh</groupId>
      <artifactId>cloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>cn.ysh.cloud</groupId>
  <artifactId>client-one-eureka</artifactId>
  <name>client-one-eureka</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <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-server</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
  </dependencies>
</project>

主要是spring-cloud-starter-netflix-eureka-server的jar和spring-cloud-starter-openfeign的jar。

3.應用的基本配置

server:
 port: 2001 
spring:
 application:
   name: client-two-eureka
eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka/(這個和server配置的是一致的)
   fetch-registry: true
   register-with-eureka: true

到此第一個應用配置完成。

搭建第二個服務應用client-two-eureka

1.springboot入口代碼

package org.client.two.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
      return new RestTemplate();
  }
}

client-two-eureka與client-one-eureka區別在於引入了restTemplate類。因爲ribbon調用其他的服務接口需要restTemplate配合。

2.pom配置文件

<?xml version="1.0"?>
<project
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven->4.0.0.xsd"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
      <groupId>cn.ysh</groupId>
      <artifactId>cloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>cn.ysh.cloud</groupId>
  <artifactId>client-two-eureka</artifactId>
  <name>client-two-eureka</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
  </dependencies>
</project>

spring-cloud-starter-netflix-ribbon的jar,這裏主要是我想讓client-two-eureka通過ribbon的方式調用client-one-eureka的服務接口。

3.配置文件

server:
 port: 2001
spring:
 application:
   name: client-two-eureka
eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka/
   fetch-registry: true
   register-with-eureka: true

到此完成第二個服務應用的搭建。

啓動client-one-eureka、client-two-eureka、server-eureka。當然server-eureka啓動完成後,再啓動client-one-eureka、client-two-eureka。原因很簡單,服務中心起來之後,才能註冊服務。
服務中心管理頁面如下:



可以看到應用中心裏面已經有兩個了。到這裏服務註冊功能已經完成。

服務之間相互調用

1.client-one-eureka
創建一個Client1Test的controller

package org.client.one.eureka.controller;

import org.client.one.eureka.service.FeignSevice;
import org.springframework.beans.factory.annotation.Autowired;
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;

import colud.clent.model.User;

@RestController
@RequestMapping("/client1Test")
public class Client1Test {

  @Autowired
  FeignSevice feignSevice;
     /**通過client-one-eureka調用client-two-eureka裏面的client2Test/clint1(Feign方式)
   * @param id
   * @return
   */
  @GetMapping("/clint1")
  public String name(@RequestParam String id) {
      System.out.println("進入client-one-eureka服務client1Test/clint1方法");
      return feignSevice.test(id, new User("1", "ysh"));
  }

    /**等待被client-two-eureka調用
   * 
   * @param id
   * @return
   */
  @GetMapping("/toCall")
  public String test1client(@RequestParam String id) {
      System.out.println("進入client-one-eureka服務client1Test/toCall方法");
      return "調用eureka-client1服務test1client方法,參數id=" + id;
  }
}

client-one-eureka調用client-two-eureka是通過Feign的方式。所以這邊創建了一個FeignSevice。代碼如下:

package org.client.one.eureka.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import colud.clent.model.User;

@Component
@FeignClient(name = "client-two-eureka")
public interface FeignSevice {

  @RequestMapping(value = "/client2Test/test2", method = RequestMethod.GET)
  public String test(@RequestParam("id") String id, @RequestBody User user);

}

代碼說明:@FeignClient指定了我們所要調用服務的名稱,@RequestMapping後面跟的就是我們所要調用服務的具體路徑。
2.client-two-eureka
創建的controller代碼

package org.client.two.eureka.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import colud.clent.model.User;

@RestController
@RequestMapping("/client2Test")
public class Client2Test {

  @Autowired
  RestTemplate restTemplate;

  /**通過client-two-eureka調用client-one-eureka的client1Test/toCall方法(ribbon方式)
   * 
   * @param id
   * @return
   */
  @GetMapping("/hi")
  public String hi(@RequestParam String id) {
      System.out.println("進入client-two-eureka服務client2Test/hi方法");
      return restTemplate.getForObject("http://client-one-eureka/client1Test/toCall?id=" + id, >String.class);
  }

  /**等待被client-one-eureka調用
   * 
   * @param id
   * @param user
   * @return
   */
  @PostMapping("/test2")
  public String testOne2(@RequestParam(required = false) String id, @RequestBody User user) {
      System.out.println("進入client2Test/test2");
      return "調用client-two-eureka服務test2方法,參數id=" + id + "user=" + user;
  }
}

通過代碼可以知道client-one-eureka的client1Test/clint1?id=''可以調用到client-two-eureka的client2Test/test2接口。這個接口採用了返回複雜對象予以說明。由於採用了複雜對象。爲了服務間共享模型數據。所以我新建了clent-model模塊。並在client-one-eureka服務、client-two-eureka服務中相繼加入該模塊依賴,完成數據結構共享。調用結果顯示如下:


同樣通過client-two-eureka的client2Test/hi?id=''可以調用到client-one-eureka的client1Test/toCall方法。結果演示如下:


文章到此就完成了服務註冊與通過ribbon、Feign兩種方式完成服務間相互調用。

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