一、nacos的安装
1、下载nacos在安装包
2、直接解压到随意目录(不能出现中文和特殊字符)
二、nacos的启动
1、在bin文件下启动dos窗口(nacos默认配置为集群启动,个人使用命令行startup.cmd -m standalone 启动即可)
2、启动后出现图一,随后访问Console网址出现图二证明启动成功(username和password都为nacos)
二、创建服务提供者provider8001
provider8001的pom文件如下:
<dependencies>
<!--nacos场景启动器-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--spring cloud alibaba 2.2.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot整合Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- actuator监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
provider8001的YML配置如下
server:
port: 8001 //服务端口号
spring:
application:
name: provider
cloud:
nacos:
discovery:
server-addr: localhost:8848 //nacos的url 默认即可
management: //端点启用和暴露
endpoints:
web:
exposure:
include: "*"
provider8001的controller层
package com.buka.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;
@RestController
@RequestMapping("/payment")
public class Controller {
@Value("${server.port}")
private String serverPort;
@GetMapping("/nacos")
public String getPayment(@RequestParam("id")Integer id){
return "nacos registry,serverPort:"+serverPort+"\t id"+id;
}
}
provider8001的主启动类
package com.buka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // 开启服务注册发现功能
public class NacosMain {
public static void main(String[] args) {
SpringApplication.run(NacosMain.class,args);
}
}
想配置第二个服务提供者只需要重复上述步骤不同点为server: port: 不能相同
三、启动服务提供者provider8001,provider8002
1、启动并未报错
2、查看nacos管理中心(如图即为注册成功)
四、编写服务消费者order7001
order7001的pom文件如下:
<dependencies>
<!--nacos场景启动器-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--spring cloud alibaba 2.2.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot整合Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- actuator监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
order7001的yml配置文件:
server:
port: 7001
spring:
application:
name: order
cloud:
nacos:
discovery:
server-addr: localhost:8848
service-url:
nacos-user-service: http://provider
order7001的主启动类
package com.buka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderMain {
public static void main(String[] args) {
SpringApplication.run(OrderMain.class,args);
}
}
order7001的controller层
package com.buka.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RequestMapping("/order")
@Slf4j
@RestController
public class Controller {
@Autowired
private RestTemplate restTemplate;
@Value("${service-url.nacos-user-service}")
private String serverURL;
@GetMapping("/order")
public String paymentInfo(@RequestParam("id") Integer id) {
return restTemplate.getForObject(serverURL + "/payment/nacos?id=" + id, String.class);
}
}
在controller中通过RestTemplate调用Nacos的服务编写config
package com.buka.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class Template {
@Bean
@LoadBalanced //负载均衡
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
由于服务提供者是两个,所以需要进行负载均衡分摊服务提供者压力。使用cloud提供的原生注解@LoadBalanced就可以提供负载均衡功能,默认使用的是轮询算法。
url:http://localhost:7001/order/order?id=1