1.Nacos
2.1Nacos Server启动
#进入bin文件夹
bash -f ./startup.sh
#关闭
./shutdown.sh
访问链接:http://127.0.0.1:8848/nacos/index.html
1.2Nacos client配置
settings->Build,Execution,Deployment->Build Tools->Maven->Ignored Files
OrderApplication.java
package order;
import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args){
SpringApplication.run(OrderApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
OrderController.java
package order.controllor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/add")
public String add(){
System.out.println("下单成功!");
String msg = restTemplate.getForObject("http://stock-service/stock/reduct",String.class);
System.out.println(msg);
return "hello, world!";
}
}
application.yml
server:
port: 8084
spring:
application:
#应用名称,nacos会将该名称当作服务名称
name: stock-service
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
username: nacos
password: nacos
namespace: public
#为false时为永久实例,宕机了也不会删除实例
#ephemeral: false
#分组管理,默认EFAULT_GROUP
#group: DEFAULT_GROUP
#权重
#weight: 1
pom.xml
<?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">
<parent>
<artifactId>cloud</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-nacos</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
2.Gateway
2.1基本使用
pom.xml
<?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">
<parent>
<artifactId>cloud</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<name>gateway</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8088
spring:
application:
name: api-gateway
cloud:
#gateway配置
gateway:
#路由规则,数据类型为List
routes:
#路由的唯一标识
- id: order_route
#需要转发的地址
uri: http://127.0.0.1:8081
#断言规则,用于路由规则的匹配
predicates:
- Path=/order/**
# filters:
# - StripPrefix=1
2.2结合nacos
server:
port: 8088
spring:
application:
name: api-gateway
cloud:
#gateway配置
gateway:
#路由规则,数据类型为List
routes:
#路由的唯一标识
- id: order_route
#需要转发的地址
uri: lb://order-service
#断言规则,用于路由规则的匹配
predicates:
- Path=/order/**
# filters:
# - StripPrefix=1
nacos:
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: public