入门搭建一个简单的springcloud

Posted MonstersU

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了入门搭建一个简单的springcloud相关的知识,希望对你有一定的参考价值。

搭建父工程

创建新的项目,这里我习惯先创建一个空的项目

 

 命名这个项目和工程存放的路径

 

 然后点ok会弹出这个窗口, 然后点击new Module

  选择maven,然后不用勾选任何东西,直接next

 

 填写完毕后直接next

  然后点击finish

 

 点击ok即可

 

 

 这样父工程就被我们创建出来了,然后我们删除一些不用的文件夹。

  然后我们导入父工程的相关依赖

 1 <packaging>pom</packaging>
 2 
 3     <parent>
 4         <groupId>org.springframework.boot</groupId>
 5         <artifactId>spring-boot-starter-parent</artifactId>
 6         <version>2.1.3.RELEASE</version>
 7         <relativePath/>
 8     </parent>
 9 
10     <properties>
11         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
13         <java.version>1.8</java.version>
14         <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
15         <mapper.starter.version>2.1.5</mapper.starter.version>
16         <mysql.version>5.1.47</mysql.version>
17         <pageHelper.starter.version>1.2.10</pageHelper.starter.version>
18         <mapper.version>1.1.5</mapper.version>
19     </properties>
20 
21     <dependencyManagement>
22         <dependencies>
23             <!-- springCloud -->
24             <dependency>
25                 <groupId>org.springframework.cloud</groupId>
26                 <artifactId>spring-cloud-dependencies</artifactId>
27                 <version>${spring-cloud.version}</version>
28                 <type>pom</type>
29                 <scope>import</scope>
30             </dependency>
31             <!-- 通用Mapper启动器 -->
32             <dependency>
33                 <groupId>tk.mybatis</groupId>
34                 <artifactId>mapper-spring-boot-starter</artifactId>
35                 <version>${mapper.starter.version}</version>
36             </dependency>
37             <!-- 分页助手启动器 -->
38             <dependency>
39                 <groupId>com.github.pagehelper</groupId>
40                 <artifactId>pagehelper-spring-boot-starter</artifactId>
41                 <version>${pageHelper.starter.version}</version>
42             </dependency>
43             <!-- mysql驱动 -->
44             <dependency>
45                 <groupId>mysql</groupId>
46                 <artifactId>mysql-connector-java</artifactId>
47                 <version>${mysql.version}</version>
48             </dependency>
49         </dependencies>
50     </dependencyManagement>
51 
52     <dependencies>
53         <dependency>
54             <groupId>org.apache.commons</groupId>
55             <artifactId>commons-lang3</artifactId>
56             <version>3.4</version>
57         </dependency>
58         <dependency>
59             <groupId>org.projectlombok</groupId>
60             <artifactId>lombok</artifactId>
61         </dependency>
62     </dependencies>

搭建eureka在父工程下新建一个模块

 

 选择maven直接next

 填写完后next

 

 注意:改路径

 

 如下:然后点击finish

 

 然后引入eureka的依赖

 1  <!--引入eureka组件依赖-->
 2     <dependencies>
 3         <dependency>
 4             <groupId>org.springframework.cloud</groupId>
 5             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 6         </dependency>
 7     </dependencies>
 8 
 9     <build>
10         <plugins>
11             <plugin>
12                 <groupId>org.springframework.boot</groupId>
13                 <artifactId>spring-boot-maven-plugin</artifactId>
14             </plugin>
15         </plugins>
16     </build>

创建eureka启动类

 

 在resource目录下创建application.yml配置文件,这里我们配置的是单实例的eureka

 

#springcloud中Eureka注册中心的端口号
server:
  port: 10086
#服务的名称
spring:
  application:
    name: eureka
eureka:
  client:
    fetch-registry: false #true的时候Eureka client在启动时会从Eureka server获取注册信息并缓存到本地,之后只会增量获取信息 false则是每次全量获取
    register-with-eureka: false #是否开启相互注册
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/ #eureka的url
  instance:
    prefer-ip-address: true

启动测试

搭建网关zuul在父工程下新建模块

和新建eureka步骤一样

 

 

 引入网关相关依赖

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.cloud</groupId>
 4             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.springframework.cloud</groupId>
 8             <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
 9         </dependency>
10         <dependency>
11             <groupId>org.springframework.boot</groupId>
12             <artifactId>spring-boot-configuration-processor</artifactId>
13         </dependency>
14     </dependencies>
15     <build>
16         <plugins>
17             <plugin>
18                 <groupId>org.springframework.boot</groupId>
19                 <artifactId>spring-boot-maven-plugin</artifactId>
20             </plugin>
21         </plugins>
22     </build>

创建网关的启动类

 

 

 创建网关的配置文件

 

 

 

 1 server:
 2   port: 10010 #开启的端口
 3 spring:
 4   application:
 5     name: gateway  #服务的名称
 6 eureka:
 7   client:
 8     service-url:
 9       defaultZone: http://127.0.0.1:10086/eureka/ #注册到eureka的地址
10     registry-fetch-interval-seconds: 5  #eureka client间隔5s去拉取服务注册信息
11 zuul:
12   prefix: /api # 添加路由前缀
13 
14   sensitive-headers: #忽略请求头设置为null
15   host:
16     connect-timeout-millis: 15000
17     socket-timeout-millis: 60000

启动测试

创建通用类在父工程下新建module

 

 

 引入依赖

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-base</artifactId>
            <version>${mapper.version}</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-core</artifactId>
            <version>${mapper.version}</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-extra</artifactId>
            <version>${mapper.version}</version>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.10.5</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.10.5</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.10.5</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

通用mapper

 

 

 

1 @tk.mybatis.mapper.annotation.RegisterMapper
2 public interface CommonMapper<T,Number> extends Mapper<T>,IdListMapper<T,Number>,InsertListMapper<T> {
3 }

通用返回

 

 

 

 1 public class RestfulResult {
 2  
 3     private String result = "Success";
 4     private String message;
 5     private Object data;        // 返回数据
 6     private int cntPage;        // page数
 7     private long cntData;        // 返回数据总数
 8     
 9     public String getResult() {
10         return result;
11     }
12     public void setResult(String result) {
13         this.result = result;
14     }
15     public String getMessage() {
16         return message;
17     }
18     public void setMessage(String message) {
19         this.message = message;
20     }
21     public Object getData() {
22         return data;
23     }
24     public void setData(Object data) {
25         this.data = data;
26     }
27     public int getCntPage() {
28         return cntPage;
29     }
30     public void setCntPage(int cntPage) {
31         this.cntPage = cntPage;
32     }
33     public long getCntData() {
34         return cntData;
35     }
36     public void setCntData(long cntData) {
37         this.cntData = cntData;
38     }
39 }

创建用户微服务在父工程下新建module

 

 

 引入依赖

 1     <dependencies>
 2         <!--web启动器-->
 3         <dependency>
 4             <groupId>org.springframework.boot</groupId>
 5             <artifactId>spring-boot-starter-web</artifactId>
 6         </dependency>
 7         <!--eureka客户端-->
 8         <dependency>
 9             <groupId>org.springframework.cloud</groupId>
10             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
11         </dependency>
12         <!--通用mapper-->
13         <dependency>
14             <groupId>tk.mybatis</groupId>
15             <artifactId>mapper-spring-boot-starter</artifactId>
16         </dependency>
17         <!--数据库驱动-->
18         <dependency>
19             <groupId>mysql</groupId>
20             <artifactId>mysql-connector-java</artifactId>
21         </dependency>
22         <!--实体类-->
23         <!--单元测试-->
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-test</artifactId>
27         </dependency>
28         <!--分页助手-->
29         <dependency>
30             <groupId>com.github.pagehelper</groupId>
31             <artifactId>pagehelper-spring-boot-starter</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>com.jia</groupId>
36             <artifactId>common</artifactId>
37             <version>1.0-SNAPSHOT</version>
38         </dependency>
39         
40         </dependencies>
41         <build>
42             <plugins>
43                 <plugin>
44                     <groupId>org.springframework.boot</groupId>
45                     <artifactId>spring-boot-maven-plugin</artifactId>
46                 </plugin>
47             </plugins>
48         </build>

  创建用户微服务启动类

 

 

 新建实体对象

1 @Data
2 @NoArgsConstructor
3 @Table(name = "student")
4 public class Student {
5     @NonNull
6     private String id;
7     private String name;
8     private String age;
9 }

新建mapper(存放自定义mapper)

1 public interface StudentMapper<T> extends Mapper<Student>{
2 }

新建service

 1 @Service
 2 public class StudentService {
 3 
 4     @Autowired
 5     private StudentMapper studentMapper;
 6 
 7     public List<Student> queryStudent(Student student){
 8         List list = studentMapper.select(student);
 9         return list;
10     }
11 
12 }

新建controller

 1 @RestController
 2 @RequestMapping("/hello")
 3 public class StudentController {
 4 
 5     @Autowired
 6     private StudentService studentService;
 7 
 8     @RequestMapping("/student")
 9     public RestfulResult queryStudent(@RequestBody Student student){
10         RestfulResult result = new RestfulResult();
11         result.setMessage("1");
12         result.setData(studentService.queryStudent(student));
13         return result;
14     }
15 }

配置yml文件

 1 server:
 2   port: 8086
 3 spring:
 4   application:
 5     name: student-service
 6   datasource:
 7     driver-class-name: com.mysql.jdbc.Driver
 8     url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false
 9     username: root
10     password: 12345678
11 mybatis:
12   mapper-locations: mappers/*.xml
13   configuration:
14     map-underscore-to-camel-case: true  #驼峰命名映射
15   type-aliases-package: com.jia.pojo
16 mapper:
17   wrap-keyword: "`{0}`" #通用mapper关键字处理
18 eureka:
19   client:
20     service-url:
21       defaultZone: http://localhost:10086/eureka
22     registry-fetch-interval-seconds: 5
23   instance:
24     prefer-ip-address: true
25     ip-address: 127.0.0.1

resource下新建mapper.xml用来存放自定义的mapperxml

 

 

启动测试

新建Feign微服务在父工程下新建module

 

 

 引入依赖

 1 <dependencies>
 2         <!--web启动器-->
 3         <dependency>
 4             <groupId>org.springframework.boot</groupId>
 5             <artifactId>spring-boot-starter-web</artifactId>
 6         </dependency>
 7         <!--eureka客户端-->
 8         <dependency>
 9             <groupId>org.springframework.cloud</groupId>
10             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
11         </dependency>
12         <!--通用mapper-->
13         <dependency>
14             <groupId>tk.mybatis</groupId>
15             <artifactId>mapper-spring-boot-starter</artifactId>
16         </dependency>
17         <!--数据库驱动-->
18         <dependency>
19             <groupId>mysql</groupId>
20             <artifactId>mysql-connector-java</artifactId>
21         </dependency>
22         <!--实体类-->
23         <!--单元测试-->
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-test</artifactId>
27         </dependency>
28         <!--分页助手-->
29         <dependency>
30             <groupId>com.github.pagehelper</groupId>
31             <artifactId>pagehelper-spring-boot-starter</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>com.jia</groupId>
36             <artifactId>common</artifactId>
37             <version>1.0-SNAPSHOT</version>
38         </dependency>
39 
40         <!--添加feign-->
41        <dependency>
42             <groupId>org.springframework.cloud</groupId>
43             <artifactId>spring-cloud-starter-openfeign</artifactId>
44         </dependency>
45 
46         <!--<dependency>
47             <groupId>org.springframework.cloud</groupId>
48             <artifactId>spring-cloud-openfeign-core</artifactId>
49         </dependency>-->
50 
51         <dependency>
52             <groupId>org.springframework.cloud</groupId>
53             <artifactId>spring-cloud-starter-hystrix</artifactId>
54             <version>1.4.7.RELEASE</version>
55         </dependency>
56 
57         <dependency>
58             <groupId>org.springframework.boot</groupId>
59             <artifactId>spring-boot-starter-actuator</artifactId>
60         </dependency>
61 
62         <dependency>
63             <groupId>com.netflix.hystrix</groupId>
64             <artifactId>hystrix-javanica</artifactId>
65             <version>RELEASE</version>
66         </dependency>
67 
68     </dependencies>
69     <build>
70         <plugins>
71             <plugin>
72                 <groupId>org.springframework.boot</groupId>
73                 <artifactId>spring-boot-maven-plugin</artifactId>
74             </plugin>
75         </plugins>
76     </build>

新建feign启动类

 

 

 新建pojo

 

 

 新建client

 

 

 serviceFeignClient

1 @Component
2 @FeignClient(name= "student-service",fallback = ServiceFallback.class)
3 public interface ServiceFeignClient {
4     @RequestMapping(value = "/hello/student",method = RequestMethod.POST)
5     RestfulResult queryStudent(@RequestBody Student student);
6 }

serviceFallback

1 @Component
2 public class ServiceFallback implements ServiceFeignClient {
3     @Override
4     public RestfulResult queryStudent(Student student) {
5         RestfulResult result = new RestfulResult();
6         result.setData("服务调用失败");
7         return result;
8     }
9 }

新建controller,注入feignServiceClient

 1 @RestController
 2 @RequestMapping("/hi")
 3 public class StudentController {
 4 
 5     @Autowired
 6     private ServiceFeignClient serviceFeignClient;
 7 
 8     @RequestMapping("/student")
 9     public RestfulResult queryStudent(@RequestBody Student student){
10         return  serviceFeignClient.queryStudent(student);
11     }
12 
13 }

yml配置文件

 1 server:
 2   port: 8087
 3 spring:
 4   application:
 5     name: feign-service
 6   datasource:
 7     driver-class-name: com.mysql.jdbc.Driver
 8     url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false
 9     username: root
10     password: 12345678
11 mybatis:
12   mapper-locations: mappers/*.xml
13   configuration:
14     map-underscore-to-camel-case: true  #驼峰命名映射
15   type-aliases-package: com.jia.pojo
16 mapper:
17   wrap-keyword: "`{0}`" #通用mapper关键字处理
18 eureka:
19   client:
20     service-url:
21       defaultZone: http://localhost:10086/eureka
22     registry-fetch-interval-seconds: 5
23   instance:
24     prefer-ip-address: true
25     ip-address: 127.0.0.1
26 feign:
27   hystrix:
28     enabled: true

pojo

@Data
@NoArgsConstructor
@Table(name = "student")
public class Student {
    @NonNull
    private String id;
    private String name;
    private String age;
}

 

复制一份用户微服务,修改下配置端口,启动

启动feign测试

以上是关于入门搭建一个简单的springcloud的主要内容,如果未能解决你的问题,请参考以下文章

《SpringCloud超级入门》使用Eureka集群搭建《十三》

SpringCloud从入门到进阶——使用SpringBoot搭建微服务

SpringCloud之Eureka集群搭建

springcloud基础入门

Spring Cloud 入门教程 - 搭建配置中心服务

Springcloud使用入门