下载解压启动
bin/startup.sh -m standalone
访问
ip:8848/nacos/#/login
创建环境,引入依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
dependencies>
@SpringBootApplication
@EnableDiscoveryClient
public class UserManagerApplication {
public static void main(String[] args) {
SpringApplication.run(UserManagerApplication.class,args);
}
}
位置在:src\main\resources\application.yml
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.88.51:8848
application:
name: ordermanager
server:
port: 8080
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
在src\main\resources创建配置文件 bootstrap.yml
spring:
application:
name: ordermanager
cloud:
nacos:
config:
server-addr: 192.168.88.51:8848
在nacos 中发布 Data Id 为**${appllication-name}
.${file-extension}
** 的配置文件
user.id=1
user.name=james
user.age=23
@RestController
@RefreshScope //实时刷新
public class SampleCroller {
@Value("${user.name}")
String userName;
@Value("${user.age}")
int age;
@RequestMapping("/user")
public String simple() {
return "Hello Nacos Config!" + "Hello " + userName + " " + age ;
}
}
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<version>2.2.2.RELEASEversion>
dependency>
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserManagerApplication {
public static void main(String[] args) {
SpringApplication.run(UserManagerApplication.class,args);
}
}
客户端代码
@Component
@FeignClient("ordermanager")
public interface OrderManager {
@RequestMapping("/user")
public String getConfig();
}
服务端代码
@RestController
@RefreshScope
public class SampleCroller {
@Value("${user.name}")
String userName;
@Value("${user.age}")
int age;
@RequestMapping("/user")
public String simple() {
return "Hello Nacos Config!" + "Hello " + userName + " " + age ;
}
}
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
<version>2.2.2.RELEASEversion>
<exclusions>
<exclusion>
<artifactId>spring-webartifactId>
<groupId>org.springframeworkgroupId>
exclusion>
<exclusion>
<artifactId>spring-boot-starter-webfluxartifactId>
<groupId>org.springframework.bootgroupId>
exclusion>
exclusions>
dependency>
spring:
cloud:
gateway:
routes:
- id: test_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu
- id: qq_route
uri: https://www.qq.com
predicates:
- Query=url,qq