SpringCloud Alibaba入门教程(详细)

Provider:

pom.xml


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


	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.SR1
				pom
				import
			
			
				org.springframework.cloud
				spring-cloud-alibaba-dependencies
				0.2.1.RELEASE
				pom
				import
			
		
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.cloud
			spring-cloud-starter-alibaba-nacos-discovery
		

		
		
			org.springframework.cloud
			spring-cloud-starter-alibaba-nacos-config
		
		
		
	    
	        org.springframework.cloud
	        spring-cloud-starter-alibaba-sentinel
	    
    
    
	

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

bootstrap.properties

# 这里的应用名对应 Nacos Config 中的 Data ID,
spring.application.name=MyNacosProvider
# 指定配置名为 MyNacosProvider 的配置文件,下面配置是后缀
spring.cloud.nacos.config.file-extension=yaml
# 注册中心 的地址
spring.cloud.nacos.config.server-addr=127.0.0.1:8848

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class StartApplication {

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

}

Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@Autowired
	private ConfigurableApplicationContext ctx;
	
	@RequestMapping("/test/{id}")
	public String getMsg(@PathVariable Integer id) {
		return "msg "+ id + " >> " + ctx.getEnvironment().getProperty("server.port");
	}
}

Nacos配置信息:

如果做负载需要建多个Data ID

================================================================================================

Consumer:


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


	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.SR1
				pom
				import
			
			
				org.springframework.cloud
				spring-cloud-alibaba-dependencies
				0.2.1.RELEASE
				pom
				import
			
		
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.cloud
			spring-cloud-starter-alibaba-nacos-discovery
		

		
		
			org.springframework.cloud
			spring-cloud-starter-alibaba-nacos-config
		
		
		
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

		
		
			org.springframework.cloud
			spring-cloud-starter-alibaba-sentinel
		


	


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

bootstrap.properties

# 这里的应用名对应 Nacos Config 中的 Data ID,
spring.application.name=MyNacosFrontendConfig
# 指定配置名为 MyNacosFrontendConfig 的配置文件,下面配置是后缀
spring.cloud.nacos.config.file-extension=yaml
# 注册中心 的地址
spring.cloud.nacos.config.server-addr=127.0.0.1:8848

#spring.cloud.nacos.discovery.server-addr=192.168.220.128:8848

#4、限流监控中心
#spring.cloud.sentinel.transport.dashboard=192.168.220.128:8080
#spring.cloud.sentinel.eager=true

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class StartApplication {

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

}

Feign:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value="MyNacosProvider")
public interface ProviderFeign {

	@RequestMapping("/test/{id}")
	public String getMsg(@PathVariable Integer id);
}

Controller:

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

import com.jacky.feign.ProviderFeign;

@RestController
public class FrontendController {
	
	@Autowired
	private ProviderFeign providerFeign;
	
	@RequestMapping("/ftest/{id}")
	public String getMsg(@PathVariable Integer id) {
		return "frontend: "+providerFeign.getMsg(id);
	}
}

Nacos配置信息:

你可能感兴趣的:(java,java,后端)