Spring Cloud Alibaba 路由网关(Gateway,完整可用)

1.先完成nacos-server项目,提供服务

2.pom.xml




  4.0.0

  com.cxb
  nacos-server
  0.0.1-SNAPSHOT

  nacos-server
  
  http://www.example.com

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

  
    UTF-8
    1.8
    1.8
  

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

  
    
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			com.alibaba.cloud
			spring-cloud-starter-alibaba-nacos-discovery
		
		
			org.projectlombok
			lombok
			 1.16.22
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
  

  
    
  

3.application.properties

server.port=8070
spring.application.name=cloud-nacos-server

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

4.CloudNacosServerApplication

package com.cxb.nacos_server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello world!
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class CloudNacosServerApplication 
{
    public static void main( String[] args )
    {
        SpringApplication.run(CloudNacosServerApplication.class, args);
    }
    
    @RestController
	class EchoController {
		@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
		public String echo(@PathVariable String string) {
			return "hello Nacos Discovery " + string;
		}

		@RequestMapping(value = "/divide", method = RequestMethod.GET)
		public String divide(@RequestParam Integer a, @RequestParam Integer b) {
			return String.valueOf(a / b);
		}
	}
}

5.启动服务

Spring Cloud Alibaba 路由网关(Gateway,完整可用)_第1张图片

******************gateway******************

1.pom.xml




	4.0.0

	com.cxb
	nacos-gataway
	0.0.1-SNAPSHOT

	nacos-gataway
	
	http://www.example.com
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.5.RELEASE 
	
	
		UTF-8
		1.8
		1.8
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.SR1
				pom
				import
			
			
				com.alibaba.cloud
				spring-cloud-alibaba-dependencies
				2.0.0.RELEASE
				pom
				import
			
		
	
	
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			com.alibaba.cloud
			spring-cloud-starter-alibaba-nacos-discovery
		
		
			org.springframework.cloud
			spring-cloud-starter-gateway
		
		
			org.projectlombok
			lombok
			 1.16.22
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

	

	

	

2.application.properties

server.port=18085
#\u914D\u7F6E Nacos Server \u5730\u5740
spring.application.name=nacos-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*

# \u914D\u7F6E\u6587\u4EF6\u4E2D\u914D\u7F6E Spring Cloud Gateway \u8DEF\u7531
spring.cloud.gateway.routes[0].id=nacos-route
#\u683C\u5F0F\u4E3A\uFF1Alb://\u5E94\u7528\u6CE8\u518C\u670D\u52A1\u540D
spring.cloud.gateway.routes[0].uri=lb://cloud-nacos-server
spring.cloud.gateway.routes[0].predicates[0].name=Path
spring.cloud.gateway.routes[0].predicates[0].args[pattern]=/nacos/**
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1

3.GatawayApplication

package com.cxb.nacos_gataway;

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

/**
 * Hello world!
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class GatawayApplication 
{
    public static void main( String[] args )
    {
        SpringApplication.run(GatawayApplication.class, args);
    }
}

4.启动项目测试

Spring Cloud Alibaba 路由网关(Gateway,完整可用)_第2张图片

访问http://localhost:8070/echo/456 调用服务本身的接口

Spring Cloud Alibaba 路由网关(Gateway,完整可用)_第3张图片

通过gateway配置的访问:http://localhost:18085/nacos/echo/8999

Spring Cloud Alibaba 路由网关(Gateway,完整可用)_第4张图片

http://localhost:18085/nacos/divide?a=10&b=2

Spring Cloud Alibaba 路由网关(Gateway,完整可用)_第5张图片

这里实现找了很多demo参考,要么pom文件的依赖各种报错,要么properties里面的配置各种不能用,虽说文件少,特么花了我大半天的时间,心塞啊(桑心0.0)

你可能感兴趣的:(springcloud,nacos,gateway)