微服务搭建及部署
基于Spring Cloud 搭建微服务项目,目前我们用到的微服务组件有Eureka、Hystrix、Ribbon、Zuul。
初始化maven结构的Spring Boot 项目,可以进http://start.spring.io/,需要用什么组件直接选择就行。目前主流IDE(STS(eclipse)、Intellij Idea等)都已经集成了这个功能。
下面的例子就是从Idea初始化的目录结构开始,如下图
├─.mvn
│ └─wrapper
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─example
│ │ │ └─demo
│ │ └─resources
│ │ ├─static
│ │ └─templates
│ └─test
│ └─java
│ └─com
│ └─example
│ └─demo
└─target
├─classes
│ ├─com
│ │ └─example
│ │ └─demo
│ └─templates
└─test-classes
└─com
└─example
└─demo
Eureka 服务端
- 首先在
pom
文件中引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 然后在Spring Boot入口启动类
EurekaServerApplication
加入Eureka客户端注解@EnableEurekaClient
package com.example.seven;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- Eureka Server 配置
applications.yml
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
default-zone: http://localhost:${server.port}/eureka/
Eureka 客户端1
- 首先在
pom
文件中引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 然后入口启动类加入Eureka客户端注解
@EnableEurekaClient
注解
package com.example.seven;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class BussinessApplication {
public static void main(String[] args) {
SpringApplication.run(BussinessApplication.class, args);
}
}
- Eureka Client配置
applications.yml
server:
port: 8762
spring:
application:
name: microservice-bussiness
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
Eureka 客户端2
- 首先在
pom
文件中引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 然后入口启动类加入Eureka客户端注解
@EnableEurekaClient
注解
package com.example.seven;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class BussinessApplication {
public static void main(String[] args) {
SpringApplication.run(BussinessApplication.class, args);
}
}
- Eureka Client配置
applications.yml
server:
port: 8763
spring:
application:
name: microservice-bussiness
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
<!-- 这里是我遇到的一大坑,项目可以正常启动没有任何问题,但是没有注册到服务中心 -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.cloud</groupId> -->
<!-- <artifactId>spring-cloud-netflix-eureka-client</artifactId> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
相继启动Eureka Server、Client1、Client2 三个工程
访问localhost:8761