springCloud-客户端

创建一个maven工程引入以下依赖



	4.0.0

	com.spring.cloud.client
	springCloudClient
	0.0.1-SNAPSHOT
	jar

	springCloudClient
	springCloudClient

	
		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-openfeign
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		
		
			org.springframework.cloud
			spring-cloud-starter-config
		
	

	
		
			
				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
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:启用feign进行远程调用

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

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudClientApplication.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;
    }

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
    

创建一个测试方法


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestClass {
	// 获取远程配置文件的值
	@Value("${test.name}")
	private String name;

	// 获取远程配置文件的值
	@RequestMapping("/testPro")
	public String testproper() {
		return name;
	}

//对外暴露接口,通过微服务或者接口调用
	@RequestMapping("/client1")
	public User testUser(@RequestBody User user) {
		System.out.println("client接收参数:"+user);
		return user;
	}

testproper方法是为了验证远程是否能正确读取配置文件,testUser方法是为了测试微服务间调用写的

接下来用postman测试下

先试下配置文件读取
springCloud-客户端_第1张图片
没问题

基本上就这样了
附上源码地址

你可能感兴趣的:(springCloud)