nacos安装这里就不细说了,(Nacos下载以及搭建环境_你非柠檬为何心酸142的博客-CSDN博客)
大家也可以去网上安装好,这里主要讲搭建 ,我们需要手动启动nacos,
输入(.\startup.cmd -m standalone),出现一下图标就代表ok
下面是我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<packaging>pom</packaging>
<modules>
<module>service-provider</module>
<module>service-const</module>
<module>service-API</module>
<module>OpenFeign-API</module>
<module>service-gatwave</module>
<module>order-8039</module>
<module>gateway</module>
<module>hystrix</module>
</modules>
<groupId>com.example</groupId>
<artifactId>SpringCloud-6-8</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringCloud-6-8</name>
<description>SpringCloud-6-8</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<!-- <spring-boot.version>2.3.12.RELEASE</spring-boot.version>-->
<spring-cloud-alibaba.version>2.2.9.RELEASE</spring-cloud-alibaba.version>
<mysql-connector-j.version>8.0.33</mysql-connector-j.version>
<mybatis.version>3.0.1</mybatis.version>
<spring-cloud-starter-netflix-hystrix>2.2.9.RELEASE</spring-cloud-starter-netflix-hystrix>
</properties>
<!-- 依赖管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql-connector-j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>${spring-cloud-starter-netflix-hystrix}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
首先是父工程所需要的依赖,需要注意的是版本一定要对应上,我这里以及对上的,也可以去github上面参考相对应的版本,——(版本说明 · alibaba/spring-cloud-alibaba Wiki (github.com))
网址放括号里了
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<!-- <spring-boot.version>2.3.12.RELEASE</spring-boot.version>-->
<spring-cloud-alibaba.version>2.2.9.RELEASE</spring-cloud-alibaba.version>
</properties>
<!-- 依赖管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
然后是子项目需要添加的依赖
<!--子项目添加依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
子模块的配置
以及controller的编写
@RestController
@RequestMapping("/provider")
public class ProviderController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/add")
public String hello() {
System.out.println("下单成功我是8050端口我要去连接8060端口");
String forObject = restTemplate.getForObject("http://service-const/stock/chd", String.class);
System.out.println("通信成功" + forObject);
return "hello word" + forObject;
}
}
controller的编写
@RestController
@RequestMapping("/stock")
public class ConstController {
@RequestMapping("/chd")
public String hello() {
System.out.println("陈厚德傻逼这是通信,我是8060端口");
return "陈厚德傻逼嘿嘿嘿这个用来通信";
}
}
到这里就已经所有工程完成了。直接启动,总体项目结构如下
我们在网页地址输入localhost:8048,
看到自己注册的两个,就代表成功了,希望能帮到各位小伙伴 文章来源:https://www.toymoban.com/news/detail-480684.html
文章来源地址https://www.toymoban.com/news/detail-480684.html
到了这里,关于springcloud使用nacos搭建注册中心的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!