1、创建一个Maven项目
编辑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>com.demo</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/>
</parent>
<properties>
<spring-cloud.version>Hoxton.SR10</spring-cloud.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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!--参考文章:https://blog.csdn.net/liupeifeng3514/article/details/80236077-->
<plugin>
<!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- 源代码使用的JDK版本 -->
<source>1.8</source>
<!-- 需要生成的目标class文件的编译版本 -->
<target>1.8</target>
<!-- 字符集编码 -->
<encoding>UTF-8</encoding>
<!-- 跳过测试 -->
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
删除src文件夹
2、添加微服务,添加两个Module,user-service和order-service
编辑pom.xml,添加相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
创建启动类
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
添加配置文件,application.yml
server:
port: 8081
添加访问的接口
package com.demo.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 UserController {
@GetMapping("/user/{id}")
public String user(@PathVariable String id) {
return "用户:" + id;
}
}
启动该服务并且访问
使用相同的步骤创建order-service
不同的地方,如下:
启动类
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
配置文件application.yml
server:
port: 8091
控制器
package com.demo.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 OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/order/{id}")
public String order(@PathVariable String id) {
// 这里访问user-service的微服务
String user = restTemplate.getForObject("http://localhost:8081/user/" + id, String.class);
return "订单:" + id + "=====" + user;
}
}
访问
现在已经打开了两个服务
如果没有这样显示,可以按这个来操作:IDEA 开启Services窗口_生骨大头菜的博客-CSDN博客
3、安装nacos
选择相关版本安装
nacos默认端口是8848,如果已经被占用,需要修改nacos端口
启动nacos
命令:startup.cmd -m standalone
访问:http://localhost:8848/nacos
账号:nacos 密码:nacos
4、将服务注册到nacos
编辑spring-cloud-demo的pom.xml文件,添加spring-cloud-alibaba的管理依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
在user-service和order-service中添加nacos的客户端依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
编辑user-service的application.yml,添加配置
spring:
application:
name: userservice # nacos的服务名称
cloud:
nacos:
server-addr: localhost:8848
编辑order-service的application.yml,添加配置
spring:
application:
name: orderservice # nacos的服务名称
cloud:
nacos:
server-addr: localhost:8848
重启user-service跟order-service服务,可以在nacos中看到服务:
在order-service的启动类中为RestTemplate添加@LoadBalanced注解
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
修改order-service中的接口,使用user-service的服务名来代替ip:port访问:
package com.demo.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 OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/order/{id}")
public String order(@PathVariable String id) {
// 这里访问user-service的微服务
String user = restTemplate.getForObject("http://userservice/user/" + id, String.class);
return "订单:" + id + "=====" + user;
}
}
重启order-service,并且访问:http://localhost:8091/order/订单用户
可以访问了,成功