Springboot2.x整合eureka-server和netflix-zuul项目实战
Springboot2.x整合eureka-server和netflix-zuul项目实战,有啥问题欢迎提出优化。一起快乐的板砖。
微服务架构:
graph LR
A[whrm_gate] -- 访问 --> B(whrm_company)
A -- 访问 --> C(whrm_system)
A -- 注册 -->D[whrm_eureka]
B -- 注册 -->D
C -- 注册 -->D
E[用户] --> A
项目结构如下:
在这里插入图片描述1、新建maven的父项目whrm
- 删除src文件,父项目不需要。
- 添加pom.xml文件内容如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、新建maven子项目whrm_eureka
在这里插入图片描述项目右键->New->Module... 根据流程下一步下一步新建就可以了。
代码目录如下:
在这里插入图片描述
- pox.xml文件
<dependencies>
<!--eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
- 新建application.properties
server.port=6868
spring.application.name=whrm_eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:${server.port}/eureka/
- 新建EurekaApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer //开启Eureka服务端配置
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
- 新建ServletInitializer.java
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(EurekaApplication.class);
}
}
到此,第一个子项目完成。
3、新建maven子项目whrm_gate
zuul网关项目
项目结构如下:
在这里插入图片描述
- pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--zuul网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
- 新建application.properties文件
server.port=9091
spring.application.name=whrm_gate
eureka.client.service-url.defaultZone=http://localhost:6868/eureka
#eureka.instance.ip-address=true
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
# zuul 连接时间
zuul.host.connect-timeout-millis=60000
zuul.host.socket-timeout-millis=60000
zuul.routes.whrm-system.path=/sys/**
zuul.routes.whrm-system.serviceId=whrm-system
zuul.routes.whrm-system.stripPrefix=false
zuul.routes.whrm-system.sensitive-headers=true
zuul.routes.whrm-system.customSensitiveHeaders=true
zuul.routes.ihrm-company.path=/company/**
zuul.routes.ihrm-company.serviceId=whrm-company
zuul.routes.ihrm-company.stripPrefix=false
zuul.routes.ihrm-company.sensitive-headers=true
zuul.routes.ihrm-company.customSensitiveHeaders=true
hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=240000
ribbon.ReadTimeout=60000
ribbon.ConnectTimeout=60000
ribbon.MaxAutoRetries=0
ribbon.MaxAutoRetriesNextServer=1
ribbon.eureka.enabled=true
- 新建GateApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
//开启zuul网关功能
@EnableZuulProxy
//开启服务发现功能
@EnableDiscoveryClient
public class GateApplication {
public static void main(String[] args) {
SpringApplication.run(GateApplication.class, args);
}
}
- 新建ServletInitializer.java
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(GateApplication.class);
}
}
到此,whrm_gate子项目完成。
4、新建maven子项目whrm_system
新建步骤同2
- pom.xml
<dependencies>
<!--eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
- 新建application.properties
server.port=9001
spring.application.name=whrm-system
#注册到eureka server
eureka.client.service-url.defaultZone=http://localhost:6868/eureka/
eureka.instance.preferIpAddress=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
- 新建SystemApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class SystemApplication {
public static void main(String[] args) {
SpringApplication.run(SystemApplication.class, args);
}
}
- 新建ServletInitializer.java
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SystemApplication.class);
}
}
- 新建 SystemController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/sys")
public class SystemController {
@RequestMapping("/hello")
public String hello(){
return "hello system controller!";
}
}
5、新建maven子项目whrm_company
新建步骤同2
- pom.xml
<dependencies>
<!--eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
- 新建application.properties
server.port=9002
spring.application.name=whrm-company
#eureka server
eureka.client.service-url.defaultZone=http://localhost:6868/eureka/
eureka.instance.preferIpAddress=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
- 新建CompanyApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class CompanyApplication {
public static void main(String[] args) {
SpringApplication.run(CompanyApplication.class,args);
}
}
- 新建ServletInitializer.java
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(CompanyApplication.class);
}
}
- 新建 CompanyController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/company")
public class CompanyController {
@RequestMapping("/hello")
public String hello(){
return "hello CompanyController !";
}
}
到此,whrm_company项目就创建完了。
完成,把每个微服务都运行起来!
显示以下页面就运行成功了!
在这里插入图片描述
zuul 网关代理成功
-
就访问http://127.0.0.1:9091/sys/hello 正确显示hello system controller!
-
就访问http://127.0.0.1:9091/company/hello 正确显示hello CompanyController !
注: springboot 的版本一定要和spring-cloud-dependencies项目对应。不知道对应搜索就可以了。
网友评论