springCloud-微服務間調用

springCloud的一大優勢就是分佈式,下面來看下各微服務模塊間的調用

首先創建一個maven項目引入如下依賴

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

	<groupId>com.spring.cloud.client1</groupId>
	<artifactId>springCloudClient1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springCloudClient11</name>
	<description>springCloudClient1</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.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>Greenwich.M1</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-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</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>

配置文件(bootstrap.properties)

這個是優於application.properties文件加載的

#註冊中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
#對應git的分支。如果配置中心使用的是本地存儲,則該參數無用
spring.cloud.config.label= master
#對應{profile}部分
spring.cloud.config.profile=test
#對應{application}部分
spring.cloud.config.name=application
#配置中心的具體地址
#spring.cloud.config.uri=http://localhost:8889
#自動尋找配置中心
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

配置文件(application.properties)

spring.application.name=config-client-1
server.port=8891
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/

修改啓動類

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.cloud.openfeign.EnableFeignClients;

@EnableDiscoveryClient
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudClient11Application {

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

實體類,同上屆中實體類方便測試

public class User {
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

微服務間調用方法

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Component
//要調用的服務名
@FeignClient("config-client")
public interface FeignClientTest {
	//要調用的服務的暴露接口
    @RequestMapping("/client1")
    @ResponseBody
    User testClient(User user);
}

最後通過調用接口實現調用另一個微服務的方法

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;

@RestController
public class TestClass {
@Autowired
private FeignClientTest feignClientTest;
    @RequestMapping("/client2")
    public User port(@RequestBody User user)
    {
        return feignClientTest.testClient(user);
    }
}

調用結果如下

在這裏插入圖片描述
這個微服務間調用的好處就是,就算其它服務換了ip,只要服務名不換就可以動態調用

最後附上源碼鏈接

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