1. 首先启动 nacos 注册中心(windows版本 + 单机模式):
****\nacos\distribution\target\nacos-server-2.0.3-SNAPSHOT\nacos\bin
startup.cmd -m standalone

备注:如果不想通过外部启动,想要通过像是Eureka那样构建Springboot-server应该也可以,通过Git下载Nacos的源码来启动试试吧,这里我就没有尝试了。
2. 生产者:
(最近看了看,发现生产者应该是将业务构建在【Service】之中才能称之为一个服务)
=>application.properties
server.port=8070
# spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=test-provider
- spring.cloud.nacos.discovery.server-addr:nacos注册中心访问地址
- spring.cloud.nacos.discovery.service:服务名称(生产者)
=>pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${nacos.version}</version>
</dependency>
备注:这里spring-cloud-starter-alibaba-nacos-config 似乎是没有用上,等之后在测试的。。。
=>启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注意这里要添加注解 @EnableDiscoveryClient
=>Controller
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController01 {
@RequestMapping("/echo/{string}")
public String echo(@PathVariable String string){
return "Hello" + string;
}
}
没什么好说的,普通的Controller
3. 消费者:
=>application.properties
server.port=8071
# spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=test-consumer
说明同生产者。
备注:这不过有关于这里的服务名称,由于生产者会被消费者所使用,所以生产者的服务名称是必须要有的,而消费者的服务名称又没有被别的微服务使用,会不会不设置也可以呢,但我感觉不设置的话,在启动的时候会报错,回来试试的
=>启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
说明同生产者。
备注:这里生产者和消费者的启动类名称没注意给弄成一样的了,因为麻烦就没改过来。
=>pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${nacos.version}</version>
</dependency>
说明同生产者。
=>配置类(为了使用Ribben的RestTemplate客户端负载均衡)
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 MyConfiguration {
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
不是重点说Ribben,因此没有说明~~
=>Controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController01 {
private final RestTemplate restTemplate;
@Autowired
public ConsumerController01(RestTemplate restTemplate){
this.restTemplate = restTemplate;
}
@RequestMapping("/echo/{str}")
public String echo(@PathVariable String str){
return restTemplate.getForObject("http://test-provider/echo/" + str, String.class);
}
}
通过restTemplate的getForObject()方法调用生产者提供的服务【http://生产者服务名称(就是在这里使用的)/**】
4. 测试结果:
- 直接调用生产者能够访问:http://localhost:8070/echo/12
- 通过调用消费者也能够访问:http://localhost:8071/echo/12
5. Nacos控制台登录,查看注册的服务:
>>http://localhost:8848/nacos/

默认的用户名与密码为:nacos/nacos
已注册的服务如上图所示。
本文介绍了如何使用Nacos作为微服务注册中心,通过Spring Cloud实现服务提供者(生产者)与服务消费者(消费者)的交互,包括配置、启动类、依赖管理和实际调用过程。展示了如何在Nacos控制台查看注册的服务实例。
2087

被折叠的 条评论
为什么被折叠?



