天天看點

利用springcloud搭建微服務內建中心

大綱
  • 注冊中心
  • 功能服務one
  • 功能服務two
整體目錄結構如下
利用springcloud搭建微服務內建中心

整體目錄結構

這裡整個功能是一個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>

作者:帥哥哥寫代碼
連結:https://www.jianshu.com/p/48d2ef887530
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。           

複制

最主要的是要引入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,界面如下:
利用springcloud搭建微服務內建中心

eureka-server管理界面.png

标紅的應用中心顯示目前沒有服務注冊到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>

作者:帥哥哥寫代碼
連結:https://www.jianshu.com/p/48d2ef887530
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。           

複制

主要是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();
  }
}           

複制

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>

作者:帥哥哥寫代碼
連結:https://www.jianshu.com/p/48d2ef887530
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。           

複制

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。原因很簡單,服務中心起來之後,才能注冊服務。

服務中心管理頁面如下:

利用springcloud搭建微服務內建中心

服務中心管理

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

服務之間互相調用

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服務中相繼加入該子產品依賴,完成資料結構共享。調用結果顯示如下:
利用springcloud搭建微服務內建中心

one call two

同樣通過client-two-eureka的client2Test/hi?id=''可以調用到client-one-eureka的client1Test/toCall方法。結果示範如下:
利用springcloud搭建微服務內建中心

two call one

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