在这之前我们需要在 windows 或者 linux … 搭建Nacos服务
https://nacos.io/zh-cn/docs/quick-start.html 官方文档教程
还可以参考我写的 Centos7-Nacos-服务注册中心搭建
( 手动版 ,docker版 -包括集群 )
我自己在Centos7中搭建了集群配合nginx做了代理 http://192.168.81.100/nacos
看下图(服务一个都没有)
接下来我们就将项目注册到Nacos中
爆红的话别管就行 可以正常使用的
<!-- spring.cloud.alibaba -spring.boot - spring.cloud 毕业版本依赖关系 推荐使用-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.cloud.alibaba.version>2.2.3.RELEASE</spring.cloud.alibaba.version>
<spring.boot>2.3.2.RELEASE</spring.boot>
<spring.cloud>Hoxton.SR8</spring.cloud>
</properties>
<!-- Springboot 版本要高于 spring-cloud-alibaba 否则报错-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot}</version>
</parent>
<!-- 使用>spring-cloud-alibaba必须的-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- spring-cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- web服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot test启动组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 将服务注册到Nacos里-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 192.168.81.100:80 #注册中心地址
server:
port: 11111
@SpringBootApplication
@EnableDiscoveryClient // 开启服务注册发现功能:
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class,args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.nacos.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
//消费者服务
@RestController
public class NacosConsumerController {
// http模板
private final RestTemplate restTemplate;
@Autowired
public NacosConsumerController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
// http://localhost:11112/echo/Hello
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
System.out.println("消费者"+string);
// 调用 nacos-provider服务 的 /echo/xx接口
return restTemplate.getForObject("http://nacos-provider/echo/" + string, String.class);
}
}
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 192.168.81.100:80 #注册中心地址
server:
port: 11111
package com.nacos.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册发现功能:
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class,args);
}
}
package com.nacos.provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
//消费者服务
@RestController
public class NacosProviderControler {
// http://localhost:11111/echo/Hello
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
System.out.println("服务提供者"+string);
return "Hello Nacos Discovery " + string;
}
}
在IDEA中Springboot项目可以按住Alt+8 打开Service多服务窗口 - 点击+添加 然后添加Springboot就行了 自动将当前项目的所有服务
添加到窗体里
同时启动服务提供者和消费者
然后访问消费者的接口 http://localhost:11112/echo/Hello
然后我们查看服务提供者的控制台
可以发现服务提供者和消费者都能使用
大家疑问RestTemplate是什么 这个是Springboot3.0之后出现的 http的模板用于专门访问各种接口服务的,用法同上…
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_45203607/article/details/120248667
内容来源于网络,如有侵权,请联系作者删除!