-
下载Nacos
下载地址https://github.com/alibaba/nacos/releases -
复制文件到Linux的/usr/local目录下
cd /usr/local
tar -vxf nacos-server-1.4.0.tar.gz
cd nacos/bin
sh startup.sh -m standalone
PS:启动文件位于nacos的bin目录下,这里以单机模式启动,除此还有集群模式 -
打开浏览器,输入http://Linux主机IP:8848/nacos/index.html,就可以看到Nacos的登录界面
创建父项目
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
相当于可用的注册中心
创建子项目(服务者)
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
创建实体类
public class Goods {
private Long id;
private String name;
private Double price;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Goods(long l, String name, double v) {
this.id = l;
this.name = name;
this.price = v;
}
}
创建service
@Service
public class GoodsService {
public Goods findGoods(){
return new Goods(1L,"MI笔记本5400",5555.0);
}
}
创建controller
@RestController
@RequestMapping("goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@GetMapping("Goods")
public ResponseEntity<Goods> findGoods(){
return ResponseEntity.ok(goodsService.findGoods());
}
}
在启动类上面加@EnableDiscoveryClient
访问ip:8848/goods/Goods查看结果
创建子项目(消费者)
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
创建config
@Configuration
public class RestTemplateConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
entity
public class Goods {
private Long id;
private String name;
private Double price;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Goods(long l, String name, double v) {
this.id = l;
this.name = name;
this.price = v;
}
public Goods() {
}
}
创建controller
@RestController
@RequestMapping("order")
public class GoodsController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("goods")
public ResponseEntity<Order> findGoods(){
Goods goods = restTemplate.getForEntity("http://goods-service/goods/Goods", Goods.class).getBody();
Order order = new Order(1L,"张三",555.6,goods);
return ResponseEntity.ok(order);
}
}
在启动类上面加@EnableDiscoveryClient
在浏览器访问地址 localhost:8848/order/goods
配置中心的使用
在goods-service中添加依赖
<!--config需要的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
配置文件
bootstrap.yml或者bootstrap.application
spring:
cloud:
nacos:
config:
server-addr: 192.168.1.24:8848
file-extension: yaml
prefix: goods-service
profiles:
active: dev
#spring.cloud.nacos.config.server-addr=192.168.1.24:8848
#spring.cloud.nacos.config.file-extension=properties
#spring.cloud.nacos.config.prefix=goods-service
#spring.profiles.active=dev
项目测试类
@RestController
@RefreshScope
public class NacosConfigController {
@Value("${server.port}")
private String port;
@Value("${spring.datasource.driver-class-name}")
private String driver;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@RequestMapping("/config")
public String config(){
return "{port='" + port + '\'' +
", driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
直接访问:
localhost:8848/config