一、项目思路
- 使用idea工具创建一个maven空项目,删除不需要的src目录等东西,用来做父项目,只剩下pom.xml文件
- 在父项目中依次创建三个模块module,分别为eruekaservice、provide、consumer三个子项目(springboot项目)。
- 在erueka中添加配置信息、在启动类上添加@EnableEurekaServer注解。
- 在provide中添加配置信息、在启动类上添加@EnableEurekaClient注解(consumer的步骤相同)。
- 在provide子项目中创建entity实体类、DAO层、service层、controller层,此处使用mybatis连接数据库、Druid连接池来连接MySQL(DataSource配置略)。
- 在consumer子项目中创建controller层,使用restTemplate来访问provide中的api(消费者controller的接口)。
- 项目运行顺序,erueka>provide>comsumer。
二、实现步骤
1、创建maven空项目
删除不需要的目录和文件
2、创建erueka-service
选择需要导入的模块(依赖)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <?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> <groupId>com.yiblue</groupId> <artifactId>eureka-server</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>eureka-server</name> <description>eureka-server</description> <properties> <java.version> 1.8 </java.version> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <project.reporting.outputEncoding>UTF- 8 </project.reporting.outputEncoding> <spring-boot.version> 2.3 . 7 .RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <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>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins><br> //编译插件 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version> 3.8 . 1 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> <encoding>UTF- 8 </encoding> </configuration> </plugin><br> //maven插件 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version> 2.3 . 7 .RELEASE</version> <configuration> <mainClass>com.yiblue.eurekaserver.EurekaServerApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
修改配置文件application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 | server: port: 8080 spring: application: name: eureka-server eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http: //${eureka.instance.hostname}:${server.port}/eureka/ |
在启动类EurekaServerApplication 上添加注解:@EnableEurekaServer
1 2 3 4 5 6 7 | @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication. class , args); } } |
3、创建provider项目(与erueka一样,这省略)
pom.xml依赖导入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | <?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> <groupId>com.yiblue</groupId> <artifactId>provider- 8001 </artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>provider- 8001 </name> <description>provider- 8001 </description> <properties> <java.version> 1.8 </java.version> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <project.reporting.outputEncoding>UTF- 8 </project.reporting.outputEncoding> <spring-boot.version> 2.3 . 7 .RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version> 2.1 . 4 </version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional> true </optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional> true </optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version> 1.1 . 17 </version> </dependency> </dependencies> <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>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version> 3.8 . 1 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> <encoding>UTF- 8 </encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version> 2.3 . 7 .RELEASE</version> <configuration> <mainClass>com.yiblue.provider8001.Provider8001Application</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
application.yml修改配置(provider),配置mybatis连接MySQL信息
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/shiro?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
username: caidongji
password: abc123
application:
name: provider-8001
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka/
server:
port: 8001
mybatis:
type-aliases-package: com.yiblue.provider8001.entity
mapper-locations: classpath:mappers/*xml
在启动类Provider8001Application上添加注解@EnableEurekaClient
1 2 3 4 5 6 7 | @SpringBootApplication @EnableEurekaClient public class Provider8001Application { public static void main(String[] args) { SpringApplication.run(Provider8001Application. class , args); } } |
编写实体类
1 2 3 4 5 6 7 8 9 | @Data @AllArgsConstructor @NoArgsConstructor public class ShiroUser { private Integer uid; private String uname; private String password; private String datasource; } |
编写接口(这里省略dao,合并到service中了)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Mapper @Repository public interface ShiroUserService { @Select ( "select * from usershiro" ) public List<ShiroUser> selectAll(); @Select ( "select * from usershiro where uname=#{uname}" ) public ShiroUser selectOne(String uname); @Select ( "insert into usershiro (uname,password,datasource) values (#{uname},#{password},#{datasource})" ) void insertOne(ShiroUser shiroUser); @Select ( "delete from usershiro where uname=#{uname}" ) void deleteOne(String uname); } |
编写controller
1 2 3 4 5 6 7 8 9 10 11 12 | @RestController public class ShiroUserController { @Autowired ShiroUserService shiroUserService; @GetMapping ( "/getAll" ) public List<ShiroUser> getAll(){ return shiroUserService.selectAll(); }<br> @GetMapping ( "/getOne" ) public ShiroUser getOne(String uname){ return shiroUserService.selectOne(uname);} } |
4、创建comsumer项目(与erueka一样,这省略)
配置依赖,pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | <?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> <groupId>com.yiblue</groupId> <artifactId>eureka-server</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>eureka-server</name> <description>eureka-server</description> <properties> <java.version> 1.8 </java.version> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <project.reporting.outputEncoding>UTF- 8 </project.reporting.outputEncoding> <spring-boot.version> 2.3 . 7 .RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <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>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version> 3.8 . 1 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> <encoding>UTF- 8 </encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version> 2.3 . 7 .RELEASE</version> <configuration> <mainClass>com.yiblue.eurekaserver.EurekaServerApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
修改application.yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 | server: port: 8080 spring: application: name: eureka-server eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http: //${eureka.instance.hostname}:${server.port}/eureka/ |
在启动类Consumer8002Application上添加注解
1 2 3 4 5 6 7 | @SpringBootApplication @EnableEurekaClient public class Consumer8002Application { public static void main(String[] args) { SpringApplication.run(Consumer8002Application. class , args); } } |
spring不会自动装配restTemplate,需要配置类注入容器中
1 2 3 4 5 6 7 | @Configuration public class restTemplateconfig { @Bean // @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate();} } |
创建controller类,调用提供者的api
1 2 3 4 5 6 7 8 9 10 | @RestController public class ShiroUserController { @Resource RestTemplate restTemplate; @GetMapping ( "/findAll" ) public List<ShiroUser> findAll(){ return restTemplate.getForObject( "http://localhost:8001/getAll" , List. class ); } } |
5、项目运行顺序:erueka>provider>consumer
在浏览器中访问:localhost:8002/findAll
6、end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具