Nacos应用
Nacos应用
快速入门
- 创建父工程
- 导入相关坐标
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 创建子工程(nacos-restful-provider)
- 导入 相关 坐标
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 创建 controller层
@RestController
public class RestProviderController {
@GetMapping(value = "service")
public String service() {
return "provider invoke";
}
}
- 书写启动类
@SpringBootApplication
public class SpringRestProviderBootstrap {
public static void main(String[] args) {
SpringApplication.run(SpringRestProviderBootstrap.class,args);
}
}
- 书写.yml配置文件
server:
port: 56010
- 创建子工程 (nacos-restful-consumer)
- 创建controller层
@RestController
public class RestConsumerController {
@Value("${provider.address}")
private String providerAddress;
@GetMapping("service1")
public String service() {
RestTemplate restTemplate = new RestTemplate();
//调用服务
String providerResult = restTemplate.getForObject("http://" + providerAddress + "/service", String.class);
return "consumer invoke | " + providerResult;
}
}
- 书写启动类
@SpringBootApplication
public class SpringRestConsumerBootstrap {
public static void main(String[] args) {
SpringApplication.run(SpringRestConsumerBootstrap.class,args);
}
}
- 书写配置文件
server:
port: 56020
provider:
address: 127.0.0.1:56010
直接访问:http://127.0.0.1:56020/service1
Nacos服务发现
restFul风格
- 父工程导入相关 坐标
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 连个子工程里面导入相关坐标
<!--导入nacos坐标-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
负载均衡
只需要在消费 者配置文件中 加入
nacos-restful-provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Dubbo风格
- 在父工程下面创建一个service2工程
- service2工程下面创建两个子工程service2-api,service2-server
- service-server导入相关坐标
<dependency>
<groupId>cn.nacos</groupId>
<artifactId>service2-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
- service2-api中创建 接口
public interface Service2Api {
public String dubboService2();
}
- 在service2-server中书写实现类
@Service
public class Service2ApiImpl implements Service2Api{
@Override
public String dubboService2() {
return "You're an idiot.";
}
}
- 在service2-server中书写启动类
@SpringBootApplication
@EnableDiscoveryClient
public class Service2Bootstrap {
public static void main(String[] args) {
SpringApplication.run(Service2Bootstrap.class,args);
}
}
- 在nacos-restful-consumer中测试
- 导入相关坐标
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>cn.nacos</groupId>
<artifactId>service2-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
- 书写实现类
public class RestConsumerController {
@Reference
private Service2Api service2Api;
@GetMapping(value = "/service2")
public String service2() {
//远程调用service2
String providerResult = service2Api.dubboService2();
return "consumer dubbo invoke | " + providerResult;
}
}