(一)项目总pom文件
1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0 .0 < / modelVersion>
< groupId> com. coffee< / groupId>
< artifactId> eureka- demo< / artifactId>
< packaging> pom< / packaging>
< version> 1.0 - SNAPSHOT< / version>
< modules>
< module > eureka- consumer< / module >
< module > eureka- provider< / module >
< module > eureka- server< / module >
< module > zuul< / module >
< / modules>
< properties>
< spring- boot. version> 2.3 .2 . RELEASE< / spring- boot. version>
< spring- cloud. version> Hoxton . SR10< / spring- cloud. version>
< / properties>
< dependencyManagement>
< dependencies>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- dependencies< / artifactId>
< version> ${ spring- boot. version} < / version>
< type> pom< / type>
< scope> import < / scope>
< / dependency>
< 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>
< pluginManagement>
< plugins>
< plugin>
< groupId> org. apache. maven. plugins< / groupId>
< artifactId> maven- compiler- plugin< / artifactId>
< configuration>
< source> 1.8 < / source>
< target> 1.8 < / target>
< compilerArgument> - parameters< / compilerArgument>
< / configuration>
< / plugin>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< version> 2.5 .0 < / version>
< executions>
< execution>
< goals>
< goal> repackage< / goal>
< / goals>
< configuration>
< mainClass> ${ main. class } < / mainClass>
< / configuration>
< / execution>
< / executions>
< / plugin>
< / plugins>
< / pluginManagement>
< / build>
< / project>
(二)Eureka服务器(eureka-server)
1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< parent>
< artifactId> eureka- demo< / artifactId>
< groupId> com. coffee< / groupId>
< version> 1.0 - SNAPSHOT< / version>
< / parent>
< modelVersion> 4.0 .0 < / modelVersion>
< artifactId> eureka- server< / artifactId>
< dependencies>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- web< / artifactId>
< / dependency>
< 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>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< / plugin>
< / plugins>
< / build>
< / project>
2. application.properties 配置
# 服务端口
server. port= 8800
# 服务名称
spring. application. name= eureka- server
# 服务地址
eureka. instance. hostname= localhost
# 不向注册中心注册自己
eureka. client. register- with - eureka= false
# 取消检索服务
eureka. client. fetch- registry= false
# 开启注册中心的保护机制,默认是开启
eureka. server. enable- self- preservation= true
# 注册中心路径
eureka. client. service- url. default - zone= http: / / ${ eureka. instance. hostname} : ${ server. port} / eureka
3. 主启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( EurekaServerApplication . class , args) ;
}
}
(三)服务提供者(eureka-provider)
1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< parent>
< artifactId> eureka- demo< / artifactId>
< groupId> com. coffee< / groupId>
< version> 1.0 - SNAPSHOT< / version>
< / parent>
< modelVersion> 4.0 .0 < / modelVersion>
< artifactId> eureka- provider< / artifactId>
< dependencies>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- web< / artifactId>
< / dependency>
< 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- test< / artifactId>
< scope> test< / scope>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< / plugin>
< / plugins>
< / build>
< / project>
2. application.properties 配置
server. port= 8810
spring. application. name= eureka- provider
eureka. client. serviceUrl. defaultZone= http: / / localhost: 8800 / eureka
3. Controller 编写
@RestController
public class ProviderController {
@GetMapping ( "/sayHello" )
public String sayHello ( @RequestParam ( "name" ) String name) {
return "Hello " + name + ", This is Spring Cloud Eureka!" ;
}
}
4. 主启动类
@SpringBootApplication
@EnableEurekaClient
public class EurekaProviderApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( EurekaProviderApplication . class , args) ;
}
}
(四)服务消费者(eureka-consumer)
1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< parent>
< artifactId> eureka- demo< / artifactId>
< groupId> com. coffee< / groupId>
< version> 1.0 - SNAPSHOT< / version>
< / parent>
< modelVersion> 4.0 .0 < / modelVersion>
< artifactId> eureka- consumer< / artifactId>
< dependencies>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- web< / artifactId>
< / dependency>
< 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- test< / artifactId>
< scope> test< / scope>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< / plugin>
< / plugins>
< / build>
< / project>
2. application.properties 配置
server. port= 8820
spring. application. name= eureka- consumer
eureka. client. serviceUrl. defaultZone= http: / / localhost: 8800 / eureka
3. Controller 编写
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping ( "/sayHello" )
public String sayHello ( @RequestParam ( "name" ) String name) {
return restTemplate. getForObject ( "http://eureka-provider/sayHello?name={1}" , String . class , name) ;
}
}
4. 主启动类
@SpringBootApplication
@EnableEurekaClient
public class EurekaConsumerApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( EurekaConsumerApplication . class , args) ;
}
@LoadBalanced
@Bean
public RestTemplate restTemplate ( ) {
return new RestTemplate ( ) ;
}
}
(五)网关(zuul)
1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< parent>
< artifactId> eureka- demo< / artifactId>
< groupId> com. coffee< / groupId>
< version> 1.0 - SNAPSHOT< / version>
< / parent>
< modelVersion> 4.0 .0 < / modelVersion>
< artifactId> zuul< / artifactId>
< dependencies>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- web< / artifactId>
< / dependency>
< dependency>
< groupId> org. springframework. cloud< / groupId>
< artifactId> spring- cloud- starter- netflix- eureka- client< / artifactId>
< / dependency>
< dependency>
< groupId> org. springframework. cloud< / groupId>
< artifactId> spring- cloud- starter- netflix- zuul< / artifactId>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- test< / artifactId>
< scope> test< / scope>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< / plugin>
< / plugins>
< / build>
< / project>
2. application.properties 配置
spring. application. name= zuul
server. port= 8888
eureka. client. service- url. defaultZone= http: / / localhost: 8800 / eureka
zuul. routes. eureka- consumer. service- id= eureka- consumer
zuul. routes. eureka- consumer. path= / consumer
3. 主启动类
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( ZuulApplication . class , args) ;
}
}
(六)验证
分别启动eureka-server、eureka-provider、eureka-consumer 和 zuul 访问 http://localhost:8888/consumer/sayHello?name=eureka,返回 “Hello eureka, This is Spring Cloud Eureka!” 则项目搭建成功。