2019独角兽企业重金招聘Python工程师标准>>>
初学springcloud-笔录,详细介绍看原文链接。这里直接上调试通过后代码
eclipse Version: 2018-12 (4.10.0)
spring-boot : 2.1.1.RELEASE
spring-cloud: Greenwich.RELEASE
JDK: 1.8
apache-maven-3.3.9
--------------------
一、创建Eureka Server ,新建maven工程: erurekaserver
1、编写 pom.xml
4.0.0
com.wg
eurekaserver
0.0.1-SNAPSHOT
jar
springcloud.helloworld.Eureka.server
Demo Spring Eureka Server
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
UTF-8
UTF-8
1.8
Greenwich.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-test
test
io.projectreactor
reactor-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2、编写EurekaServerApplication.java
package wg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3、编写application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enableSelfPreservation: false
4、启动项目
访问http://localhost:8761
二、创建Eureka Client
1、编写 pom.xml
4.0.0
wg
eurekaclient1
1.0-SNAPSHOT
eurekaclient1
http://www.example.com
UTF-8
UTF-8
Greenwich.RELEASE
1.8
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-config-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2、编写EurekaServerApplication.java
package wg;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home() {
return "hello world from port " + port;
}
}
3、编写application.yml
server:
port: 8762
spring:
application:
name: eurekaclient1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
4、启动项目
访问http://localhost:8762
5、访问http://localhost:8761
项目截图:
下一篇:Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)(Greenwich.RELEASE)