首页 > 其他分享 >springcloud快速入门

springcloud快速入门

时间:2022-09-24 23:01:46浏览次数:56  
标签:入门 springcloud boot springframework eureka spring org 快速 public

一、项目思路

  1. 使用idea工具创建一个maven空项目,删除不需要的src目录等东西,用来做父项目,只剩下pom.xml文件
  2. 在父项目中依次创建三个模块module,分别为eruekaservice、provide、consumer三个子项目(springboot项目)。
  3. 在erueka中添加配置信息、在启动类上添加@EnableEurekaServer注解。
  4. 在provide中添加配置信息、在启动类上添加@EnableEurekaClient注解(consumer的步骤相同)。
  5. 在provide子项目中创建entity实体类、DAO层、service层、controller层,此处使用mybatis连接数据库、Druid连接池来连接MySQL(DataSource配置略)。
  6. 在consumer子项目中创建controller层,使用restTemplate来访问provide中的api(消费者controller的接口)。
  7. 项目运行顺序,erueka>provide>comsumer。

二、实现步骤

1、创建maven空项目

 

 

删除不需要的目录和文件

 

 

 2、创建erueka-service

 

 

  选择需要导入的模块(依赖)

<?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>
//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

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

3、创建provider项目(与erueka一样,这省略)

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>
    <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

@SpringBootApplication
@EnableEurekaClient
public class Provider8001Application {
    public static void main(String[] args) {
        SpringApplication.run(Provider8001Application.class, args);
    }
}

编写实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShiroUser {
    private Integer uid;
    private String uname;
    private String password;
    private String datasource;
}

编写接口(这里省略dao,合并到service中了)

@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

@RestController
public class ShiroUserController {
    @Autowired
    ShiroUserService shiroUserService;

    @GetMapping("/getAll")
    public List<ShiroUser> getAll(){
        return shiroUserService.selectAll();
    }
@GetMapping("/getOne") public ShiroUser getOne(String uname){ return shiroUserService.selectOne(uname);} }

  

4、创建comsumer项目(与erueka一样,这省略)

配置依赖,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>
    <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文件

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上添加注解

@SpringBootApplication
@EnableEurekaClient
public class Consumer8002Application {
    public static void main(String[] args) {
        SpringApplication.run(Consumer8002Application.class, args);
    }
}

  spring不会自动装配restTemplate,需要配置类注入容器中

@Configuration
public class restTemplateconfig {

    @Bean
//    @LoadBalanced
    RestTemplate restTemplate(){ return new RestTemplate();}
}

  创建controller类,调用提供者的api

@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

 

标签:入门,springcloud,boot,springframework,eureka,spring,org,快速,public
From: https://www.cnblogs.com/yiblue/p/16726914.html

相关文章

  • coredump快速定位
    https://utcc.utoronto.ca/~cks/space/blog/linux/KernelSegfaultMessageMeaningWhattheLinuxkernel'smessagesaboutsegfaultingprogramsmeanon64-bitx86Feb......
  • SpringCloud微服务架构
    认识微服务单体架构将业务的所有功能集中在一个项目中开发,打成一个包部署。优点:1.架构简单2.部署成本低缺点:耦合度高分布式架构根据业务功能对系统进行拆分,......
  • Spring 基于注解配置bean之简单入门
    Spring注解配置bean复习注解相关的知识啥是注解?直接是一种特殊的标识符。可在源码或运行阶段起作用。注解类型元注解如**@Target**自定义注解Spring中注......
  • scala 快速上手
    Scala特性基于JVM:可以与Java混合编程,且可相互调包。类型推测:不需要显式定义数据类型,\(var\)表示变量,\(val\)表示常量。并发和分布式(Actor,类似Java中的多线程T......
  • Rust学习入门
    介绍特性:高性能,内存利用率高,没有运行时和垃圾回收可靠,丰富的类型系统和所有权模型保证内存和线程安全,编译器可以消除各种错误生产力,包管理器、构建工具一流,......
  • Servlet快速入门
     创建Servlet:创建web项目,导入Servlet依赖坐标<dependency><groupld>javax.servlet</groupld><artifactld>javax.servlet-api</artifactld><version>3.1.0</version>......
  • python入门03
    python入门day3目录昨日内容回顾§一、计算机的五大组成部分详解和三大核心硬件1、计算机的五大组成部分详解2、计算机的三大核心硬件3、操作系统OperatingSystem4、编......
  • Maven快速配置和入门
    概念Maven其实就是一个管理项目、构建项目的工具。它有标准化的项目结构、构建流程、依赖管理。功能Maven提供了一套标准的项目结构Maven提供了一套标准的构建流程Ma......
  • Python基础教程,Python入门教程(超详细)
    Python由荷兰数学和计算机科学研究学会于1990年代初设计,作为一门叫做ABC语言的替代品。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应......
  • LOJ #162. 快速幂 2
    题意要求一个\(O(\sqrtP)-O(1)\)的快速幂。幂可以用扩展欧拉定理规约到\([1,P-1]\)中。分析分块。定个阈值\(B=\sqrtP\)+1。\(a^t=a^{t\bmodB}\cdot......