最近发现SpringCloud构建微服务架构中,网上不少只是用到了SpringBoot2.x以前的版本,显然使用SpringBoot2.x以后构建,网上的资料会给初学者带来不少不方便,并且没有多大的参考价值,因此,这里将使用SpringBoot2.0.0版本,构建SpringCloud Eureka服务治理。 java
服务治理分了两部分:注册中心和服务提供者web
工具环境:IntelliJ IDEAspring
1、搭建注册中心架构
一、打开IDEA,File->new->Project->maven...app
如上图所示,这一步很重要,由于建立maven项目能够有不少种方式,若是构建简单的项目,能够选择快速maven,可是SpringCloud Eureka确定必需要选择那个webapp项目,否则的话,结果出来会没法正常访问Spring Eureka页面。建立项目的后续操做这里不详细述说,相信不少玩过maven项目的都会。webapp
二、配置pom文件,导入相关包maven
<!-- SpringBoot 2.0.0 依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> </parent> <!-- JUnit测试依赖 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 添加spring-boot-starter-web模块依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring cloud 配置依赖, 这个能够先不导入 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- SpringCloud Eureka依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <!-- Spring Cloud 管理依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
上面的信息是主要的依赖导入,这里特别须要指出几个注意点:spring-boot
①eureka-server包微服务
对于SpringBoot2.0.0,是使用spring-cloud-starter-netflix-eureka-server;对于一些低版本的SpringBoot,是使用spring-cloud-starter-eureka-server;若是使用版本不匹配,就会没法导入@EnableEurekaServer。工具
②SpringCloud集中管理版本
对于SpringBoot2.x版本,SpringCloud应该使用Finchley版本,SpringCloud的版本命名是根据英国街道名字,具体能够百科一下,下面给出不一样SpringBoot版本对应的SpringCloud版本代号:
详细能够参考:https://blog.csdn.net/54powerman/article/details/79163440
三、配置application.properties文件(有些人喜欢用yaml,这里使用.properties)
完整application.properties配置以下:
server.port=1111 eureka.instance.hostname=localhost spring.application.name=hello-service1 #因为该应用是注册中心,false:表明不向注册中心注册本身;true:表明注册本身 eureka.client.register-with-eureka=false #是否启动检测服务,因为注册中心的职责是维护服务实例,因此它不须要检服务 eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
四、配置启动类
package com.cjs; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * Hello world! * */ @SpringBootApplication @EnableEurekaServer public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); SpringApplication.run(App.class, args); } }
@EnableEurekaServer表示,此项目做为Eureka服务中心
最后,启动项目,在网页上输入:http://127.0.0.1:1111,就会跳到Spring Eureka的界面,以下图:
出现上面图片,证实搭建注册中心成功。注意红色框,显示没有“实例”(“服务”)可用,固然了,首先,properties文件已经关掉服务中心本身注册本身的功能,其次,没有其余服务提供者使用1111端口管理。
2、搭建服务提供者
步骤跟搭建注册中心如出一辙,能够直接copy过去,须要修改几点位置:
①properties文件
#设置服务提供者名字 spring.application.name=hello-server-index eureka.instance.hostname=localhost eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:1111/eureka/
②启动类文件
package com.cjs; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableDiscoveryClient public class App { public static void main(String[] args){ System.out.println("启动————提供服务者"); SpringApplication.run(App.class, args); } }
在注册中心是使用@EnableEurekaServer,而在服务提供者这里,要使用@EnableDiscoveryClient。
最后,在启动注册中心的基础上,再将此项目也启动一下,再次访问http:127.0.0.1:1111,这是就会发现:
这里能够看到刚才建立的服务提供者的名字。
接下来,再服务提供者项目里建立一个简单的Controller,用来测试,
package com.cjs.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.logging.Logger; @Controller public class HelloController { @Autowired private DiscoveryClient client; @RequestMapping("/index") @ResponseBody public String index() { return "hello World"; } }
访问http://127.0.0.1:8080/index(注意:这里端口为8080),会出现下面效果:
以上就是简单搭建Eureka的注册中心和服务提供者的操做步骤与注意点,但愿对于各位读者有帮助。