springCloud-微服务间调用

springCloud的一大优势就是分布式,下面来看下各微服务模块间的调用

首先创建一个maven项目引入如下依赖



	4.0.0

	com.spring.cloud.client1
	springCloudClient1
	0.0.1-SNAPSHOT
	jar

	springCloudClient11
	springCloudClient1

	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Greenwich.M1
	

	
		    
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
		
			org.springframework.cloud
			spring-cloud-starter-openfeign
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
	




配置文件(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);
    }
}

调用结果如下

springCloud-微服务间调用_第1张图片
这个微服务间调用的好处就是,就算其它服务换了ip,只要服务名不换就可以动态调用

最后附上源码链接

你可能感兴趣的:(springCloud)