微服务项目通常会存在多个服务,但服务与服务之间的调用,联调就用到了服务注册与发现,spring cloud全家桶就包含了Eureka来实现这个功能。java
1.首先建立服务注册中心 利用spring Initializr来建立项目,new Module->选中Spring Initializr->Nextweb
而后填写项目名称,以及maven项目仍是gradle 项目spring
最后选择要用到何种依赖,此时选择的服务发现的eureka-serverapp
到此项目就构建完成了,此时的build.gradle文件是这样的:eclipse
buildscript { ext { springBootVersion = '2.0.4.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } ext { springCloudVersion = 'Finchley.SR1' } dependencies { compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
项目布局是这样的:maven
启动主方法所在类EurekaServerApplication.javaspring-boot
package com.example.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
而后启动一个服务注册中心,在EurekaServerApplication类上面加一个注解@EnableEurekaServer;微服务
application.yml配置文件:布局
server: port: 8791 spring: application: name: eureka-server eureka: instance: hostname: localhost client: register-with-eureka: false #由于默认状况下一个eureka-server也是一个eureka-client,因此设置注册eureka为false fetch-registry: false service-url: defalutZone: http://localhost:8791/eureka
而后启动该项目,访问http://localhost:8791,此时标明尚未服务注册进来fetch
2.建立服务提供者 eureka-client
build.gradle文件,增长了eureka-client依赖
buildscript { ext { springBootVersion = '2.0.4.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } ext { springCloudVersion = 'Finchley.SR1' } dependencies { compile('org.springframework.boot:spring-boot-starter') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
启动类EurekaClientSayHiApplication.java文件
package com.example.eurekaclientsayhi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableEurekaClient @SpringBootApplication public class EurekaClientSayHiApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientSayHiApplication.class, args); } @RequestMapping("/hi") public String sayHi(){ return "hi"; } }
application.yml文件
server: port: 8792 spring: application: name: eureka-client-say-hi eureka: client: service-url: defaultZone: http://localhost:8791/eureka/ #eureka-server的地址
启动该eureka-client项目:
访问http://localhost:8792/hi
此时刷新一下地址:http://localhost:8791 ,发现已经有服务注册到服务注册中心了。