SpringCloudAlibaba + Nacos整合完成
This commit is contained in:
parent
e9ca234f24
commit
f022d1a9a4
12
.run/BookApplication-01.run.xml
Normal file
12
.run/BookApplication-01.run.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="BookApplication-01" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="图书服务">
|
||||||
|
<envs>
|
||||||
|
<env name="server.port" value="8201" />
|
||||||
|
</envs>
|
||||||
|
<module name="book-service" />
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.BookApplication" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
12
.run/BookApplication-02.run.xml
Normal file
12
.run/BookApplication-02.run.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="BookApplication-02" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="图书服务">
|
||||||
|
<envs>
|
||||||
|
<env name="server.port" value="8202" />
|
||||||
|
</envs>
|
||||||
|
<module name="book-service" />
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.BookApplication" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
15
.run/BorrowApplication.run.xml
Normal file
15
.run/BorrowApplication.run.xml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="BorrowApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" nameIsGenerated="true">
|
||||||
|
<module name="borrow-service" />
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.BorrowApplication" />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="com.test.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
14
.run/ConfigApplication.run.xml
Normal file
14
.run/ConfigApplication.run.xml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="ConfigApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" nameIsGenerated="true">
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.ConfigApplication" />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="com.test.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
11
.run/EurekaApplication-01.run.xml
Normal file
11
.run/EurekaApplication-01.run.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="EurekaApplication-01" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="Eureka集群">
|
||||||
|
<envs>
|
||||||
|
<env name="spring.profiles.active" value="01" />
|
||||||
|
</envs>
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.EurekaApplication" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
11
.run/EurekaApplication-02.run.xml
Normal file
11
.run/EurekaApplication-02.run.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="EurekaApplication-02" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="Eureka集群">
|
||||||
|
<envs>
|
||||||
|
<env name="spring.profiles.active" value="02" />
|
||||||
|
</envs>
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.EurekaApplication" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
14
.run/GatewayApplication.run.xml
Normal file
14
.run/GatewayApplication.run.xml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="GatewayApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" nameIsGenerated="true">
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.GatewayApplication" />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="com.test.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
8
.run/HystrixDashBoardApplication.run.xml
Normal file
8
.run/HystrixDashBoardApplication.run.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="HystrixDashBoardApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="Hystrix监控服务" nameIsGenerated="true">
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.HystrixDashBoardApplication" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
18
.run/UserApplication-01.run.xml
Normal file
18
.run/UserApplication-01.run.xml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="UserApplication-01" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="用户服务">
|
||||||
|
<envs>
|
||||||
|
<env name="server.port" value="8101" />
|
||||||
|
</envs>
|
||||||
|
<module name="user-service" />
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.UserApplication" />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="com.test.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
12
.run/UserApplication-02.run.xml
Normal file
12
.run/UserApplication-02.run.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="UserApplication-02" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="用户服务">
|
||||||
|
<envs>
|
||||||
|
<env name="server.port" value="8102" />
|
||||||
|
</envs>
|
||||||
|
<module name="user-service" />
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.UserApplication" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
15
.run/UserApplication.run.xml
Normal file
15
.run/UserApplication.run.xml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="UserApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" nameIsGenerated="true">
|
||||||
|
<module name="user-service" />
|
||||||
|
<option name="SPRING_BOOT_MAIN_CLASS" value="com.test.UserApplication" />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="com.test.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
@ -19,6 +19,10 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
@ -23,6 +23,7 @@ public class BookController {
|
|||||||
|
|
||||||
@RequestMapping("/book/{bid}")
|
@RequestMapping("/book/{bid}")
|
||||||
Book findBookById(@PathVariable("bid") int bid) {
|
Book findBookById(@PathVariable("bid") int bid) {
|
||||||
|
System.out.println("调用图书服务");
|
||||||
return service.getBookById(bid);
|
return service.getBookById(bid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,3 +6,11 @@ spring:
|
|||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
# 应用名称 bookservice
|
||||||
|
application:
|
||||||
|
name: bookservice
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 配置Nacos注册中心地址
|
||||||
|
server-addr: localhost:8848
|
||||||
|
@ -18,6 +18,19 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 这里需要单独导入LoadBalancer依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
@ -2,6 +2,7 @@ package com.test;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClassName: BorrowApplication
|
* ClassName: BorrowApplication
|
||||||
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
* @author yovinchen
|
* @author yovinchen
|
||||||
* @Create 2023/8/14 16:25
|
* @Create 2023/8/14 16:25
|
||||||
*/
|
*/
|
||||||
|
@EnableFeignClients
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class BorrowApplication {
|
public class BorrowApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.test.service.client;
|
||||||
|
|
||||||
|
import com.test.entity.Book;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassName: BookClient
|
||||||
|
* Package: com.test.service.client
|
||||||
|
*
|
||||||
|
* @author yovinchen
|
||||||
|
* @Create 2023/8/16 10:55
|
||||||
|
*/
|
||||||
|
@FeignClient("bookservice")
|
||||||
|
public interface BookClient {
|
||||||
|
|
||||||
|
@RequestMapping("/book/{bid}")
|
||||||
|
Book getBookById(@PathVariable("bid") int bid);
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.test.service.client;
|
||||||
|
|
||||||
|
import com.test.entity.User;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassName: UserClient
|
||||||
|
* Package: com.test.service.client
|
||||||
|
*
|
||||||
|
* @author yovinchen
|
||||||
|
* @Create 2023/8/16 10:55
|
||||||
|
*/
|
||||||
|
@FeignClient("userservice")
|
||||||
|
public interface UserClient {
|
||||||
|
|
||||||
|
@RequestMapping("/user/{uid}")
|
||||||
|
User getUserById(@PathVariable("uid") int uid);
|
||||||
|
}
|
@ -1,15 +1,15 @@
|
|||||||
package com.test.service.impl;
|
package com.test.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import com.test.entity.Book;
|
import com.test.entity.Book;
|
||||||
import com.test.entity.Borrow;
|
import com.test.entity.Borrow;
|
||||||
import com.test.entity.User;
|
import com.test.entity.User;
|
||||||
import com.test.entity.UserBorrowDetail;
|
import com.test.entity.UserBorrowDetail;
|
||||||
import com.test.mapper.BorrowMapper;
|
import com.test.mapper.BorrowMapper;
|
||||||
import com.test.service.BorrowService;
|
import com.test.service.BorrowService;
|
||||||
|
import com.test.service.client.BookClient;
|
||||||
|
import com.test.service.client.UserClient;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -28,16 +28,20 @@ public class BorrowServiceImpl implements BorrowService {
|
|||||||
@Resource
|
@Resource
|
||||||
BorrowMapper mapper;
|
BorrowMapper mapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
UserClient userClient;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
BookClient bookClient;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
|
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
|
||||||
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
|
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
|
||||||
//RestTemplate支持多种方式的远程调用
|
User user = userClient.getUserById(uid);
|
||||||
RestTemplate template = new RestTemplate();
|
List<Book> bookList = borrow
|
||||||
//这里通过调用getForObject来请求其他服务,并将结果自动进行封装
|
.stream()
|
||||||
//获取User信息
|
.map(b -> bookClient.getBookById(b.getBid()))
|
||||||
User user = template.getForObject("http://localhost:8101/user/" + uid, User.class);
|
.collect(Collectors.toList());
|
||||||
//获取每一本书的详细信息
|
|
||||||
List<Book> bookList = borrow.stream().map(b -> template.getForObject("http://localhost:8201/book/" + b.getBid(), Book.class)).collect(Collectors.toList());
|
|
||||||
return new UserBorrowDetail(user, bookList);
|
return new UserBorrowDetail(user, bookList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,3 +6,11 @@ spring:
|
|||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
# 应用名称 borrowservice
|
||||||
|
application:
|
||||||
|
name: borrowservice
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 配置Nacos注册中心地址
|
||||||
|
server-addr: localhost:8848
|
||||||
|
21
pom.xml
21
pom.xml
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.7.14</version>
|
<version>2.6.6</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>com.example</groupId>
|
<groupId>com.example</groupId>
|
||||||
@ -43,6 +43,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<version>8.0.32</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@ -53,6 +54,24 @@
|
|||||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
<version>2.2.0</version>
|
<version>2.2.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 这里引入最新的SpringCloud依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>2021.0.1</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 这里引入最新的SpringCloudAlibaba依赖,2021.0.1.0版本支持SpringBoot2.6.X -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||||
|
<version>2021.0.1.0</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
<build>
|
<build>
|
||||||
|
@ -18,6 +18,10 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
@ -24,6 +24,7 @@ public class UserController {
|
|||||||
//这里以RESTFul风格为例
|
//这里以RESTFul风格为例
|
||||||
@RequestMapping("/user/{uid}")
|
@RequestMapping("/user/{uid}")
|
||||||
public User findUserById(@PathVariable("uid") int uid) {
|
public User findUserById(@PathVariable("uid") int uid) {
|
||||||
|
System.out.println("调用图书服务");
|
||||||
return service.getUserById(uid);
|
return service.getUserById(uid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,3 +6,11 @@ spring:
|
|||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
# 应用名称 userservice
|
||||||
|
application:
|
||||||
|
name: userservice
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 配置Nacos注册中心地址
|
||||||
|
server-addr: localhost:8848
|
||||||
|
Loading…
Reference in New Issue
Block a user