广告位联系
返回顶部
分享到

SpringCloud中的服务接口(api)介绍

java 来源:互联网 作者:佚名 发布时间:2024-04-19 22:35:38 人浏览
摘要

SpringCloud服务接口(api) SpringCloud的服务接口基本等同于SpringBoot开发的api接口 只不过在springCloud需要将自己的服务注册到注册中心,通过网关统一访问路径 一个简单的api接口 pom.xml 1 2 3 4 5 6 7 8

SpringCloud服务接口(api)

SpringCloud的服务接口基本等同于SpringBoot开发的api接口

只不过在springCloud需要将自己的服务注册到注册中心,通过网关统一访问路径

一个简单的api接口

  • 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

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.springcloud.demo</groupId>

  <artifactId>eureka-user</artifactId>

  <version>dev</version>

    <packaging>jar</packaging>

  

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.7.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

  

    <properties>

        <!--main方法类 -->

        <start-class>com.cc.yonyou.rndealer.BootMain</start-class>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.8</java.version>

        <spring-cloud.version>Dalston.SR4</spring-cloud.version>

        <!-- 同该版本feign集成需要swagger 2.5.0版本以上 -->

        <swagger.version>2.5.0</swagger.version>

    </properties>

  

    <dependencies>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-eureka</artifactId>

        </dependency>

         

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

        </dependency>

         <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>

        </dependency>

     

        <!--base64加密解密  -->

        <dependency>

            <groupId>commons-codec</groupId>

            <artifactId>commons-codec</artifactId>

            <version>1.10</version>

        </dependency>

        

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-eureka</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>1.1.1</version>

        </dependency>

        <dependency>

            <groupId>org.mybatis.generator</groupId>

            <artifactId>mybatis-generator-core</artifactId>

            <version>1.3.2</version>

            <scope>compile</scope>

            <optional>true</optional>

        </dependency>

         

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger-ui</artifactId>

            <version>2.2.2</version>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.25</version>

        </dependency>

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger2</artifactId>

            <version>2.2.2</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.25</version>

        </dependency>

         

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>druid</artifactId>

            <version>1.0.20</version>

        </dependency>

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>fastjson</artifactId>

            <version>1.2.12</version>

        </dependency>

        <dependency>

            <groupId>com.github.pagehelper</groupId>

            <artifactId>pagehelper</artifactId>

            <version>4.1.6</version>

        </dependency>

         

        <!-- Swagger -->

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger2</artifactId>

            <version>${swagger.version}</version>

        </dependency>

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger-ui</artifactId>

            <version>${swagger.version}</version>

        </dependency>

         

        <dependency> 

            <groupId>org.springframework.boot</groupId> 

            <artifactId>spring-boot-starter-redis</artifactId>

            <version>1.3.8.RELEASE</version>

        </dependency>

         

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>druid</artifactId>

            <version>1.0.20</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>

        </dependencies>

    </dependencyManagement>

  

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

            <!--打包跳过测试插件-->

            <plugin>

            <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <configuration>

                    <skip>true</skip>

                </configuration>

            </plugin>

        </plugins>

    </build>

  

  

</project>

  • application.properties

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

#eureka

spring.application.name = user

server.port=12003

eureka.client.serviceUrl.defaultZone=http://localhost:12000/eureka/

feign.hystrix.enabled=true

  

#mybatis

mybatis.type-aliases-package=com.springcloud.demo.domain

mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml

mybatis.configuration.map-underscore-to-camel-case=true

mybatis.configuration.use-generated-keys=true

mybatis.configuration.default-fetch-size=100

mybatis.configuration.default-statement-timeout=30

  

#datasource

spring.datasource.username=root

spring.datasource.password=mysql

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/test

  

spring.datasource.initialSize=5

spring.datasource.minIdle=5

spring.datasource.maxActive=20

spring.datasource.maxWait=60000

spring.datasource.timeBetweenEvictionRunsMillis=60000

spring.datasource.validationQuery=SELECT 1

spring.datasource.testWhileIdle=true

spring.datasource.testOnBorrow=false

spring.datasource.testOnReturn=false

spring.datasource.poolPreparedStatements=true

spring.datasource.maxPoolPreparedStatementPerConnectionSize=20

spring.datasource.filters=stat

spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

  • BootMain.java

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

package com.springcloud.demo;

  

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.netflix.feign.EnableFeignClients;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

  

import springfox.documentation.annotations.ApiIgnore;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

  

@Controller

@SpringBootApplication

@ApiIgnore

@EnableScheduling

@EnableDiscoveryClient

@EnableFeignClients

@EnableSwagger2

@EnableHystrixDashboard

public class BootMain {

    public static void main(String[] args) {

        SpringApplication.run(BootMain.class, args);

    }

     

    @RequestMapping("/help")

    public String help() {

        return "redirect:swagger-ui.html";

    }

  

}

  • api接口uers.java

1

2

3

4

5

6

7

8

9

10

11

12

13

package com.springcloud.demo.api;

  

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

  

@RestController

public class UserApi {

    @RequestMapping(value="/test",method=RequestMethod.GET)

    public String test() {

        return "Hello World";

    }

}

基本代码与我们常写得api接口无异,只是在application.properties文件中添加了注册中心的路径


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计