新建Maven主项目
修改pom文件
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
<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依赖,添加hystrix熔断器的依赖。
建立Eureka服务模块
一致点击next即可
引入Eureka服务端依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
新建包和启动类
@EnableEurekaServer
@SpringCloudApplication
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class,args);
}
}
@EnableEurekaServer 表示当前工程为注册中心
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}
@SpringCloudApplication注解中@SpringBootApplication是SpringBoot项目的注解,@EnableDiscoveryClient表示当前工程可以注册到注册中心,@EnableCircuitBreaker是开启熔断器。
resource下添加application.yml文件
server:
port: 9300
Spring:
application:
name: eurakeserver
eureka:
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://localhost:9300/eureka/
register-with-eureka是是否注册到eureka服务端,fetch-registry是否读取其他Eureka服务端的信息,service-url注册到的位置,Eureka服务会默认将Spring的name转化为大写。
建立Eureka客户模块
和服务端一样,新建一个Maven模块 eurakeclient
修改pom文件,引入eureka-client客户端依赖和spring-boot-starter-web依赖
<dependencies>
<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-web</artifactId>
</dependency>
</dependencies>
新建application.yml
server:
port: 9310
Spring:
application:
name: eurakeclient
eureka:
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://localhost:9300/eureka/
新建启动类
@SpringCloudApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class,args);
}
}
新建一个controller
@RestController
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "Hello World! 这是一个Eureka客户端";
}
}
新建网关模块
和eureka服务模块一样,新建一个moven模块 gateway
修改pom文件
引入getaway和eureka-client客户端组件和webflux,gateway采用的是webFlux
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
新建启动类
@SpringCloudApplication
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class,args);
}
}
resource下新建application.yml文件
server:
port: 9320
Spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:9300/eureka/
spring.cloud.gateway.discovery.locator开启动态路由配置,开启之后,可以使用http://localhost:gateway port/serviceId/** 去访问别的模块的内容
启动测试
分别启动eurekaserver、eurekaclient和gateway三个服务。
http://localhost:9300/
可以看到三个服务。
通关网关访问
http://localhost:9320/EURAKECLIENT/hello