1.完成微服务场景模拟案例
1、创建nacos-discovery父工程
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">
<modelVersion>4.0.0</modelVersion>
<groupId>om.yh.nacos</groupId>
<artifactId>nacos-discovery</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<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>
</project>
2、Service B(服务生产者)
创建服务提供者 nacos-restful-provider。
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>nacos-discovery</artifactId>
<groupId>om.yh.nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-restful-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Service B是服务的生产方,暴露/service服务地址,实现代码如下:
1、创建Controller
package com.yh.nacos.provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestProviderController {
@GetMapping(value = "service")
public String service(){
System.out.println("provider invoke");
return "provider invoke";
}
}
2、创建启动类
package com.yh.nacos.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringRestProviderBootstrap {
public static void main(String[] args) {
SpringApplication.run(SpringRestProviderBootstrap.class,args);
}
}
配置文件:
创建application.yml,内容如下
server:
port: 56010
3、Service A(服务消费者)
创建nacos-restful-consumer 服务消费工程。
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>nacos-discovery</artifactId>
<groupId>om.yh.nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-restful-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
</dependencies>
</project>
实现代码:
1、创建controller
package com.yh.nacos.consumer.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class RestConsumerController {
@Value("${provider.address}")
private String providerAddress;
@GetMapping("service")
public String service() {
RestTemplate restTemplate = new RestTemplate();
//调用服务
String providerResult = restTemplate.getForObject("http://" + providerAddress + "/service", String.class);
return "consumer invoke | " + providerResult;
}
}
2、创建启动类
package com.yh.nacos.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
public class SpringRestConsumerBootstrap {
public static void main(String[] args) {
SpringApplication.run(SpringRestConsumerBootstrap.class,args);
}
}
配置文件:
创建application.yml,内容如下:
server:
port: 56020
provider:
address: 127.0.0.1:56010
访问 http://127.0.0.1:56020/service,输出以下内容:
consumer invoke | provider invoke
2. 安装启动nacos
1.预备环境准备
Nacos 依赖 Java 环境来运行。如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用:
-
64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac。
2.预备环境准备
Nacos Server 下载 | Nacos 官网
https://nacos.io/download/nacos-server/
3 启动服务器
nacos的默认端口是8848,需要保证8848默认端口没有被其他进程占用。进入安装程序的bin目录:
cd nacos/bin
Linux/Unix/Mac启动方式:
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
bash startup.sh -m standalone
Windows启动方式: 启动命令:
cmd startup.cmd 或者 双击startup.cmd运行文件。
启动成功,可通过浏览器访问 http://127.0.0.1:8848/nacos ,打开如下nacos控制台登录页面:
3. 注册restful服务到nacos,完成调用测试
步骤一:创建Spring Boot RESTful服务
首先,你需要创建一个Spring Boot项目,然后添加Spring Web依赖。在pom.xml
中加入以下依赖:
Xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Nacos Discovery Client -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>
步骤二:配置Nacos Discovery
在application.properties
中配置Nacos Discovery服务的地址和当前服务的名称:
Properties
spring.application.name=my-rest-service
spring.cloud.nacos.discovery.server-addr=localhost:8848
步骤三:创建RESTful Controller
在你的Spring Boot项目中创建一个Controller,提供RESTful接口:
Java
@RestController
@RequestMapping("/api")
public class MyRestController {
@GetMapping("/hello")
public String hello() {
return "Hello from my REST service!";
}
}
步骤四:启动服务并测试
运行你的Spring Boot应用,然后在Nacos控制台上可以看到新注册的服务。你可以使用Postman或curl命令测试RESTful接口:
Bash
curl http://localhost:port/api/hello
4. 完成dubbo服务的注册与发现
步骤一:创建Dubbo服务提供者
创建一个Spring Boot项目,并添加Dubbo和Nacos依赖:
Xml
<dependencies>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!-- Nacos Discovery for Dubbo -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>
步骤二:配置Dubbo和Nacos
在application.properties
中配置Dubbo和Nacos的参数:
Properties
spring.application.name=my-dubbo-provider
spring.cloud.nacos.discovery.server-addr=localhost:8848
dubbo.protocol.port=20880
dubbo.registry.address=nacos://localhost:8848
步骤三:创建Dubbo服务接口和实现
创建一个Dubbo服务接口和其实现:
Java
// Service Interface
@DubboService
public interface MyDubboService {
String sayHello(String name);
}
// Service Implementation
@Service
public class MyDubboServiceImpl implements MyDubboService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
步骤四:创建Dubbo服务消费者
创建另一个Spring Boot项目作为Dubbo服务消费者,并配置同样的Nacos和Dubbo依赖。在消费者项目中,注入并使用Dubbo服务:
Java
@SpringBootApplication
public class DubboConsumerApplication {
@Autowired
private MyDubboService myDubboService;
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
@RestController
public class ConsumerController {
@GetMapping("/consume")
public String consume() {
return myDubboService.sayHello("World");
}
}
}
步骤五:启动服务并测试
分别启动Dubbo服务提供者和消费者,确保它们都成功注册到了Nacos上。然后,通过访问消费者项目的端点来测试Dubbo服务的调用:
Bash
curl http://localhost:consumer-port/consume