一、项目思路
- 使用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
选择需要导入的模块(依赖)
java;gutter:true;
4.0.0
com.yiblue
eureka-server
0.0.1-SNAPSHOT
eureka-server
eureka-server</p>
<pre><code> 1.8
UTF-8
UTF-8
2.3.7.RELEASE
Hoxton.SR9
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
//编译插件
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
1.8
UTF-8
//maven插件
org.springframework.boot
spring-boot-maven-plugin
2.3.7.RELEASE
com.yiblue.eurekaserver.EurekaServerApplication
repackage
repackage
</code></pre>
<pre><code>
修改配置文件application.yml
;gutter:true;
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
java;gutter:true;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}</p>
<pre><code>
3、创建provider项目(与erueka一样,这省略)
pom.xml依赖导入
;gutter:true;
4.0.0
com.yiblue
provider-8001
0.0.1-SNAPSHOT
provider-8001
provider-8001
1.8
UTF-8
UTF-8
2.3.7.RELEASE
Hoxton.SR9
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
com.alibaba
druid
1.1.17
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
1.8
UTF-8
org.springframework.boot
spring-boot-maven-plugin
2.3.7.RELEASE
com.yiblue.provider8001.Provider8001Application
repackage
repackage
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-8001eureka: client: service-url: defaultZone: http://localhost:8080/eureka/server: port: 8001mybatis: type-aliases-package: com.yiblue.provider8001.entity mapper-locations: classpath:mappers/*xml
在启动类Provider8001Application上添加注解@EnableEurekaClient
java;gutter:true;
@SpringBootApplication
@EnableEurekaClient
public class Provider8001Application {
public static void main(String[] args) {
SpringApplication.run(Provider8001Application.class, args);
}
}</p>
<pre><code>
编写实体类
;gutter:true;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShiroUser {
private Integer uid;
private String uname;
private String password;
private String datasource;
}
编写接口(这里省略dao,合并到service中了)
java;gutter:true;
@Mapper
@Repository
public interface ShiroUserService {
@Select("select * from usershiro")
public List selectAll();</p>
<pre><code>@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);
</code></pre>
<p>}</p>
<pre><code>
编写controller
;gutter:true;
@RestController
public class ShiroUserController {
@Autowired
ShiroUserService shiroUserService;
@GetMapping("/getAll")
public List getAll(){
return shiroUserService.selectAll();
}
@GetMapping("/getOne")
public ShiroUser getOne(String uname){ return shiroUserService.selectOne(uname);}
}
4、创建comsumer项目(与erueka一样,这省略)
配置依赖,pom.xml
java;gutter:true;
4.0.0
com.yiblue
eureka-server
0.0.1-SNAPSHOT
eureka-server
eureka-server</p>
<pre><code> 1.8
UTF-8
UTF-8
2.3.7.RELEASE
Hoxton.SR9
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
1.8
UTF-8
org.springframework.boot
spring-boot-maven-plugin
2.3.7.RELEASE
com.yiblue.eurekaserver.EurekaServerApplication
repackage
repackage
</code></pre>
<pre><code>
修改application.yml文件
;gutter:true;
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上添加注解
java;gutter:true;
@SpringBootApplication
@EnableEurekaClient
public class Consumer8002Application {
public static void main(String[] args) {
SpringApplication.run(Consumer8002Application.class, args);
}
}</p>
<pre><code>
spring不会自动装配restTemplate,需要配置类注入容器中
;gutter:true;
@Configuration
public class restTemplateconfig {
@Bean
// @LoadBalanced
RestTemplate restTemplate(){ return new RestTemplate();}
}
创建controller类,调用提供者的api
java;gutter:true;
@RestController
public class ShiroUserController {
@Resource
RestTemplate restTemplate;</p>
<pre><code>@GetMapping("/findAll")
public List findAll(){
return restTemplate.getForObject("http://localhost:8001/getAll", List.class);
}
</code></pre>
<p>}
5、项目运行顺序:erueka>provider>consumer
在浏览器中访问:localhost:8002/findAll
6、end
Original: https://www.cnblogs.com/yiblue/p/16726914.html
Author: 一个不会玩的狗子
Title: springcloud快速入门
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/621620/
转载文章受原作者版权保护。转载请注明原作者出处!