pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5
6 <groupId>org.jimmy</groupId>
7 <artifactId>springclouddemo</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 <packaging>jar</packaging>
10
11 <name>SpringCloudDemo20180502</name>
12 <description>Demo project for Spring Boot</description>
13
14 <parent>
15 <groupId>org.springframework.boot</groupId>
16 <artifactId>spring-boot-starter-parent</artifactId>
17 <version>2.0.1.RELEASE</version>
18 <relativePath/> <!-- lookup parent from repository -->
19 </parent>
20
21 <properties>
22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24 <java.version>1.8</java.version>
25 <spring-cloud.version>Finchley.RC1</spring-cloud.version>
26 </properties>
27
28 <dependencies>
29 <dependency>
30 <groupId>org.springframework.cloud</groupId>
31 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
32 </dependency>
33
34 <dependency>
35 <groupId>org.springframework.boot</groupId>
36 <artifactId>spring-boot-starter-test</artifactId>
37 <scope>test</scope>
38 </dependency>
39
40 <dependency>
41 <groupId>org.springframework.cloud</groupId>
42 <artifactId>spring-cloud-starter-eureka-server</artifactId>
43 </dependency>
44 </dependencies>
45
46 <dependencyManagement>
47 <dependencies>
48 <dependency>
49 <groupId>org.springframework.cloud</groupId>
50 <artifactId>spring-cloud-dependencies</artifactId>
51 <version>${spring-cloud.version}</version>
52 <type>pom</type>
53 <scope>import</scope>
54 </dependency>
55 </dependencies>
56 </dependencyManagement>
57
58 <build>
59 <plugins>
60 <plugin>
61 <groupId>org.springframework.boot</groupId>
62 <artifactId>spring-boot-maven-plugin</artifactId>
63 </plugin>
64 </plugins>
65 </build>
66
67 <repositories>
68 <repository>
69 <id>spring-milestones</id>
70 <name>Spring Milestones</name>
71 <url>https://repo.spring.io/milestone</url>
72 <snapshots>
73 <enabled>false</enabled>
74 </snapshots>
75 </repository>
76 </repositories>
77
78 </project>
application-server.yml
代码如下:
eureka:
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8761
spring:
application:
name: service-hi
application.yml
代码如下:
spring:
profiles:
active: server
application-client.yml
代码如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-hi
Config.java
1 package org.jimmy;
2
3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
4 import org.springframework.context.annotation.Bean;
5 import org.springframework.context.annotation.Configuration;
6 import org.springframework.web.client.RestTemplate;
7
8 /**
9 * Created by Administrator on 2018/5/2 0002.
10 */
11 @Configuration
12 public class Config {
13 @LoadBalanced
14 @Bean
15 public RestTemplate restTemplate() {
16 return new RestTemplate();
17 }
18 }
ServiceHiApplication.java
1 package org.jimmy;
2
3 import org.springframework.beans.factory.annotation.Value;
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.boot.autoconfigure.SpringBootApplication;
6 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestParam;
10
11 @SpringBootApplication
12 @EnableEurekaServer
13 public class ServiceHiApplication {
14 public static void main(String[] args) throws Exception {
15 SpringApplication.run(ServiceHiApplication.class, args);
16 }
17
18 }
ClientHiApplication.java
1 package org.jimmy;
2
3 import org.springframework.beans.factory.annotation.Value;
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.boot.autoconfigure.SpringBootApplication;
6 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RequestParam;
9 import org.springframework.web.bind.annotation.RestController;
10
11 /**
12 * Created by Administrator on 2018/5/3 0003.
13 */
14 @SpringBootApplication
15 @EnableDiscoveryClient
16 @RestController
17 public class ClientHiApplication {
18
19 public static void main(String[] args) {
20 SpringApplication.run(ClientHiApplication.class, args);
21 }
22
23 @Value("${server.port}")
24 String port;
25 @RequestMapping("/hi")
26 public String home(@RequestParam String name) {
27 System.out.println("name:" + name + ",port:" + port);
28 return "hi "+name+",i am from port:" +port;
29 }
30 }
第一步,运行ServiceHiApplication.java.服务可以启动了.
第二步,将
application.yml
改为如下:
spring:
profiles:
active: client
之前的Server是服务端,此时再启动一个客户端.(server对应application-server.yml,client对应application-client.yml)
运行ClientHiApplication.java.客户端可以启动了.
在Web浏览器(Chrome,FireFox等)中访问url:
http://localhost:8762/hi?name=Dawn
如果页面显示:hi Dawn,i am from port:8762
说明代码没有问题,可以成功访问了.