一,创建SpringCloud项目
创建完成之后的pom.xml
<dependencies> <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> <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>
application.yml文件:
server: port: 8761 #指定端口号 eureka: instance: hostname: localhost #指定eureka注册中心地址 client: registerWithEureka: false #是否把自己注册到eureka 默认是 true fetchRegistry: false #是否从uereka server 中获取信息 serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类
然后启动项目.看看效果 http://localhost:8761/
二,创建Client 项目
创建项目如上相同,不贴图了
pom.xml的依赖
<dependencies> <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> <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>
application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ #使用eureka的注册中心地址 server: port: 8762 #端口号 spring: application: name: service-client #注册成功的服务名称
启动类
启动项目
打开刚才启动的springcloud 注册中心的网页 http://localhost:8761/
打开新窗口 输入 http://localhost:8762/hi?name=cloud
可以按照上述方式多创建几个项目.
一个简单的Spring Cloud 就搭建成功了.本人正在学习Spring Cloud的一切.会不定期更新一些.自己学到的东西.欢迎大家多多点评.
后面会更新RIBBON方式调用其它服务