Ver Código Fonte

springcloud-example工程初始化

huangbo 6 anos atrás
pai
commit
0942f98275
26 arquivos alterados com 463 adições e 0 exclusões
  1. 1 0
      springcloud-example/.gitignore
  2. 17 0
      springcloud-example/.project
  3. 3 0
      springcloud-example/eureka-server/.gitignore
  4. 23 0
      springcloud-example/eureka-server/.project
  5. 24 0
      springcloud-example/eureka-server/pom.xml
  6. 14 0
      springcloud-example/eureka-server/src/main/java/com/ai/ipu/service/eureka/EurekaServerStart.java
  7. 10 0
      springcloud-example/eureka-server/src/main/resources/application.properties
  8. 3 0
      springcloud-example/feign-service-consumer/.gitignore
  9. 23 0
      springcloud-example/feign-service-consumer/.project
  10. 32 0
      springcloud-example/feign-service-consumer/pom.xml
  11. 37 0
      springcloud-example/feign-service-consumer/src/main/java/com/ai/ipu/service/consume/Application.java1
  12. 14 0
      springcloud-example/feign-service-consumer/src/main/java/com/ai/ipu/service/consume/FeignServiceConsumerStart.java
  13. 20 0
      springcloud-example/feign-service-consumer/src/main/java/com/ai/ipu/service/consume/HelloController.java
  14. 12 0
      springcloud-example/feign-service-consumer/src/main/java/com/ai/ipu/service/consume/HelloServiceClient.java
  15. 4 0
      springcloud-example/feign-service-consumer/src/main/resources/application.properties
  16. 3 0
      springcloud-example/feign-service-provider/.gitignore
  17. 23 0
      springcloud-example/feign-service-provider/.project
  18. 39 0
      springcloud-example/feign-service-provider/pom.xml
  19. 15 0
      springcloud-example/feign-service-provider/src/main/java/com/ai/ipu/sevice/provide/FeignServiceProviderStart.java
  20. 13 0
      springcloud-example/feign-service-provider/src/main/java/com/ai/ipu/sevice/provide/HelloController.java
  21. 4 0
      springcloud-example/feign-service-provider/src/main/resources/application.properties
  22. 70 0
      springcloud-example/pom.xml
  23. 3 0
      springcloud-example/service-api/.gitignore
  24. 23 0
      springcloud-example/service-api/.project
  25. 21 0
      springcloud-example/service-api/pom.xml
  26. 12 0
      springcloud-example/service-api/src/main/java/com/ai/ipu/service/api/HelloService.java

+ 1 - 0
springcloud-example/.gitignore

@ -0,0 +1 @@
1
/.settings/

+ 17 - 0
springcloud-example/.project

@ -0,0 +1,17 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>springcloud-example</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.m2e.core.maven2Builder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
	</buildSpec>
14
	<natures>
15
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
16
	</natures>
17
</projectDescription>

+ 3 - 0
springcloud-example/eureka-server/.gitignore

@ -0,0 +1,3 @@
1
/.settings/
2
/target/
3
/.classpath

+ 23 - 0
springcloud-example/eureka-server/.project

@ -0,0 +1,23 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>eureka-server</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
		<buildCommand>
14
			<name>org.eclipse.m2e.core.maven2Builder</name>
15
			<arguments>
16
			</arguments>
17
		</buildCommand>
18
	</buildSpec>
19
	<natures>
20
		<nature>org.eclipse.jdt.core.javanature</nature>
21
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
22
	</natures>
23
</projectDescription>

+ 24 - 0
springcloud-example/eureka-server/pom.xml

@ -0,0 +1,24 @@
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<parent>
5
		<groupId>com.ai.ipu.example</groupId>
6
		<artifactId>springcloud-example</artifactId>
7
		<version>1.0-SNAPSHOT</version>
8
	</parent>
9
	<artifactId>eureka-server</artifactId>
10
	<name>eureka-server</name>
11
	<description>eureka服务</description>
12

13
	<dependencies>
14
		<!-- 对比
15
		spring-cloud-starter-eureka-server
16
		spring-cloud-netflix-eureka-server
17
		spring-cloud-starter-netflix-eureka-server
18
		 -->
19
		<dependency>
20
            <groupId>org.springframework.cloud</groupId>
21
            <artifactId>spring-cloud-netflix-eureka-server</artifactId>
22
        </dependency>
23
	</dependencies>
24
</project>

+ 14 - 0
springcloud-example/eureka-server/src/main/java/com/ai/ipu/service/eureka/EurekaServerStart.java

@ -0,0 +1,14 @@
1
package com.ai.ipu.service.eureka;
2

3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6

7
@EnableEurekaServer
8
@SpringBootApplication
9
public class EurekaServerStart {
10
    
11
    public static void main(String[] args) {
12
        SpringApplication.run(EurekaServerStart.class,args);
13
    }
14
}

+ 10 - 0
springcloud-example/eureka-server/src/main/resources/application.properties

@ -0,0 +1,10 @@
1
#提供服务端口
2
server.port=1111
3
#提供服务的域名,本地可以使用localhost或者配置hosts测试
4
eureka.instance.hostname=localhost
5
#关闭向注册中心注册自己
6
eureka.client.register-with-eureka=false
7
#关闭发现注册服务,注册中心仅用于维护节点
8
eureka.client.fetch-registry=false
9
#配置注册中心提供服务的url(这里引用上边的配置)
10
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

+ 3 - 0
springcloud-example/feign-service-consumer/.gitignore

@ -0,0 +1,3 @@
1
/.settings/
2
/target/
3
/.classpath

+ 23 - 0
springcloud-example/feign-service-consumer/.project

@ -0,0 +1,23 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>feign-service-consumer</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
		<buildCommand>
14
			<name>org.eclipse.m2e.core.maven2Builder</name>
15
			<arguments>
16
			</arguments>
17
		</buildCommand>
18
	</buildSpec>
19
	<natures>
20
		<nature>org.eclipse.jdt.core.javanature</nature>
21
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
22
	</natures>
23
</projectDescription>

+ 32 - 0
springcloud-example/feign-service-consumer/pom.xml

@ -0,0 +1,32 @@
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<parent>
5
		<groupId>com.ai.ipu.example</groupId>
6
		<artifactId>springcloud-example</artifactId>
7
		<version>1.0-SNAPSHOT</version>
8
	</parent>
9
	<artifactId>feign-service-consumer</artifactId>
10
	<name>feign-service-consumer</name>
11
	<description>feign服务消费者</description>
12
	
13
	<dependencies>
14
		<dependency>
15
            <groupId>org.springframework.boot</groupId>
16
            <artifactId>spring-boot-starter-web</artifactId>
17
        </dependency>
18
        <dependency>
19
            <groupId>org.springframework.cloud</groupId>
20
            <artifactId>spring-cloud-starter-eureka</artifactId>
21
        </dependency>
22
        <dependency>
23
            <groupId>org.springframework.cloud</groupId>
24
            <artifactId>spring-cloud-starter-feign</artifactId>
25
        </dependency>
26
        <dependency>
27
			<groupId>com.ai.ipu.api</groupId>
28
			<artifactId>service-api</artifactId>
29
			<version>1.0-SNAPSHOT</version>
30
		</dependency>
31
    </dependencies>
32
</project>

+ 37 - 0
springcloud-example/feign-service-consumer/src/main/java/com/ai/ipu/service/consume/Application.java1

@ -0,0 +1,37 @@
1
package com.ai.ipu.service.consume;
2
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.boot.builder.SpringApplicationBuilder;
6
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7
import org.springframework.cloud.netflix.feign.EnableFeignClients;
8
import org.springframework.cloud.netflix.feign.FeignClient;
9
import org.springframework.web.bind.annotation.GetMapping;
10
import org.springframework.web.bind.annotation.RestController;
11
12
import com.ai.ipu.service.api.HelloService;
13
14
@EnableFeignClients
15
@EnableDiscoveryClient
16
@SpringBootApplication
17
public class Application {
18
19
    public static void main(String[] args) {
20
        new SpringApplicationBuilder(Application.class).web(true).run(args);
21
    }
22
23
    @FeignClient("feign-service-provider")
24
    interface HelloServiceClient extends HelloService {
25
    }
26
27
    @RestController
28
    class TestController {
29
        @Autowired
30
        private HelloServiceClient helloServiceClient;
31
32
        @GetMapping("/test")
33
        public String test(String name) {
34
            return helloServiceClient.hello(name);
35
        }
36
    }
37
}

+ 14 - 0
springcloud-example/feign-service-consumer/src/main/java/com/ai/ipu/service/consume/FeignServiceConsumerStart.java

@ -0,0 +1,14 @@
1
package com.ai.ipu.service.consume;
2

3
import org.springframework.boot.autoconfigure.SpringBootApplication;
4
import org.springframework.boot.builder.SpringApplicationBuilder;
5
import org.springframework.cloud.netflix.feign.EnableFeignClients;
6

7
@EnableFeignClients
8
@SpringBootApplication
9
public class FeignServiceConsumerStart {
10

11
    public static void main(String[] args) {
12
        new SpringApplicationBuilder(FeignServiceConsumerStart.class).web(true).run(args);
13
    }
14
}

+ 20 - 0
springcloud-example/feign-service-consumer/src/main/java/com/ai/ipu/service/consume/HelloController.java

@ -0,0 +1,20 @@
1
package com.ai.ipu.service.consume;
2

3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.web.bind.annotation.GetMapping;
5
import org.springframework.web.bind.annotation.RequestParam;
6
import org.springframework.web.bind.annotation.RestController;
7

8

9
@RestController
10
public class HelloController{
11
    
12
    @Autowired
13
    private HelloServiceClient helloServiceClient;
14

15
    @GetMapping("/hi")
16
    public String hi(@RequestParam(value = "name") String name) {
17
        return helloServiceClient.hello(name);
18
    }
19
    
20
}

+ 12 - 0
springcloud-example/feign-service-consumer/src/main/java/com/ai/ipu/service/consume/HelloServiceClient.java

@ -0,0 +1,12 @@
1
package com.ai.ipu.service.consume;
2

3
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
4
import org.springframework.cloud.netflix.feign.FeignClient;
5

6
import com.ai.ipu.service.api.HelloService;
7

8
@EnableDiscoveryClient
9
@FeignClient("feign-service-provider")
10
public interface HelloServiceClient extends HelloService {
11
    
12
}

+ 4 - 0
springcloud-example/feign-service-consumer/src/main/resources/application.properties

@ -0,0 +1,4 @@
1
spring.application.name=feign-service-consumer
2
server.port=2102
3
4
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

+ 3 - 0
springcloud-example/feign-service-provider/.gitignore

@ -0,0 +1,3 @@
1
/.settings/
2
/target/
3
/.classpath

+ 23 - 0
springcloud-example/feign-service-provider/.project

@ -0,0 +1,23 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>feign-service-provider</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
		<buildCommand>
14
			<name>org.eclipse.m2e.core.maven2Builder</name>
15
			<arguments>
16
			</arguments>
17
		</buildCommand>
18
	</buildSpec>
19
	<natures>
20
		<nature>org.eclipse.jdt.core.javanature</nature>
21
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
22
	</natures>
23
</projectDescription>

+ 39 - 0
springcloud-example/feign-service-provider/pom.xml

@ -0,0 +1,39 @@
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<parent>
5
		<groupId>com.ai.ipu.example</groupId>
6
		<artifactId>springcloud-example</artifactId>
7
		<version>1.0-SNAPSHOT</version>
8
	</parent>
9
	<artifactId>feign-service-provider</artifactId>
10
	<name>feign-service-provider</name>
11
	<description>feign服务提供者</description>
12

13
	<dependencies>
14
		<dependency>
15
            <groupId>org.springframework.boot</groupId>
16
            <artifactId>spring-boot-starter-web</artifactId>
17
        </dependency>
18
		<dependency>
19
			<groupId>org.springframework.cloud</groupId>
20
			<artifactId>spring-cloud-starter-eureka</artifactId>
21
		</dependency>
22
		<dependency>
23
			<groupId>com.ai.ipu.api</groupId>
24
			<artifactId>service-api</artifactId>
25
			<version>1.0-SNAPSHOT</version>
26
		</dependency>
27
		
28
		<!--eureka的客户端依赖-->
29
        <!-- <dependency>
30
            <groupId>org.springframework.cloud</groupId>
31
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
32
        </dependency> -->
33
        <!--feign的依赖-->
34
        <!-- <dependency>
35
            <groupId>org.springframework.cloud</groupId>
36
            <artifactId>spring-cloud-starter-openfeign</artifactId>
37
        </dependency> -->
38
	</dependencies>
39
</project>

+ 15 - 0
springcloud-example/feign-service-provider/src/main/java/com/ai/ipu/sevice/provide/FeignServiceProviderStart.java

@ -0,0 +1,15 @@
1
package com.ai.ipu.sevice.provide;
2

3
import org.springframework.boot.autoconfigure.SpringBootApplication;
4
import org.springframework.boot.builder.SpringApplicationBuilder;
5
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6

7
@EnableDiscoveryClient
8
@SpringBootApplication
9
public class FeignServiceProviderStart {
10

11
    public static void main(String[] args) {
12
        /*web()?*/
13
        new SpringApplicationBuilder(FeignServiceProviderStart.class).web(true).run(args);
14
    }
15
}

+ 13 - 0
springcloud-example/feign-service-provider/src/main/java/com/ai/ipu/sevice/provide/HelloController.java

@ -0,0 +1,13 @@
1
package com.ai.ipu.sevice.provide;
2

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

5
import com.ai.ipu.service.api.HelloService;
6

7
@RestController
8
class HelloController implements HelloService {
9
    
10
    public String hello(String name) {
11
        return "hello " + name;
12
    }
13
}

+ 4 - 0
springcloud-example/feign-service-provider/src/main/resources/application.properties

@ -0,0 +1,4 @@
1
spring.application.name=feign-service-provider
2
server.port=2101
3
4
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

+ 70 - 0
springcloud-example/pom.xml

@ -0,0 +1,70 @@
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4

5
	<groupId>com.ai.ipu.example</groupId>
6
	<artifactId>springcloud-example</artifactId>
7
	<version>1.0-SNAPSHOT</version>
8
	<packaging>pom</packaging>
9

10
	<name>springcloud-example</name>
11
	<description>springcloud的简单范例</description>
12
	<modules>
13
		<module>eureka-server</module>
14
		<module>feign-service-provider</module>
15
		<module>feign-service-consumer</module>
16
		<module>service-api</module>
17
	</modules>
18

19
	<properties>
20
		<spring-boot-dependencies>1.5.21.RELEASE</spring-boot-dependencies>
21
		<spring-cloud-dependencies>Edgware.SR6</spring-cloud-dependencies>
22
	</properties>
23
	<!-- 管理依赖 -->
24
	<dependencyManagement>
25
		<dependencies>
26
			<dependency>
27
				<groupId>org.springframework.cloud</groupId>
28
				<artifactId>spring-cloud-dependencies</artifactId>
29
				<version>${spring-cloud-dependencies}</version>
30
				<type>pom</type>
31
				<scope>import</scope>
32
			</dependency>
33
			<!-- Spring Boot无侵入依赖引入方式 -->
34
			<dependency>
35
				<groupId>org.springframework.boot</groupId>
36
				<artifactId>spring-boot-dependencies</artifactId>
37
				<version>${spring-boot-dependencies}</version>
38
				<type>pom</type>
39
				<scope>import</scope>
40
			</dependency>
41
		</dependencies>
42

43
	</dependencyManagement>
44

45
	<dependencies>
46
		<!-- 
47
		<dependency>
48
			<groupId>org.springframework.boot</groupId>
49
			<artifactId>spring-boot-starter-web</artifactId>
50
		</dependency>
51
		-->
52
	</dependencies>
53

54
	<build>
55
		<plugins>
56
			<plugin>
57
				<groupId>org.springframework.boot</groupId>
58
				<artifactId>spring-boot-maven-plugin</artifactId>
59
			</plugin>
60
			<plugin>
61
				<groupId>org.apache.maven.plugins</groupId>
62
				<artifactId>maven-compiler-plugin</artifactId>
63
				<configuration>
64
					<source>1.8</source>
65
					<target>1.8</target>
66
				</configuration>
67
			</plugin>
68
		</plugins>
69
	</build>
70
</project>

+ 3 - 0
springcloud-example/service-api/.gitignore

@ -0,0 +1,3 @@
1
/.settings/
2
/target/
3
/.classpath

+ 23 - 0
springcloud-example/service-api/.project

@ -0,0 +1,23 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>service-api</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
		<buildCommand>
14
			<name>org.eclipse.m2e.core.maven2Builder</name>
15
			<arguments>
16
			</arguments>
17
		</buildCommand>
18
	</buildSpec>
19
	<natures>
20
		<nature>org.eclipse.jdt.core.javanature</nature>
21
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
22
	</natures>
23
</projectDescription>

+ 21 - 0
springcloud-example/service-api/pom.xml

@ -0,0 +1,21 @@
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<parent>
5
		<groupId>com.ai.ipu.example</groupId>
6
		<artifactId>springcloud-example</artifactId>
7
		<version>1.0-SNAPSHOT</version>
8
	</parent>
9

10
	<groupId>com.ai.ipu.api</groupId>
11
	<artifactId>service-api</artifactId>
12
	<name>service-api</name>
13
	<description>服务调用的API接口</description>
14

15
	<dependencies>
16
		<dependency>
17
			<groupId>org.springframework.boot</groupId>
18
			<artifactId>spring-boot-starter-web</artifactId>
19
		</dependency>
20
	</dependencies>
21
</project>

+ 12 - 0
springcloud-example/service-api/src/main/java/com/ai/ipu/service/api/HelloService.java

@ -0,0 +1,12 @@
1
package com.ai.ipu.service.api;
2
3
import org.springframework.web.bind.annotation.GetMapping;
4
import org.springframework.web.bind.annotation.RequestParam;
5
6
public interface HelloService {
7
8
    /*String hello(String name);*/
9
    @GetMapping("/hello")
10
    String hello(@RequestParam(value = "name") String name);
11
    
12
}