主要使用了分发的功能
三个服务端口分别是9091 9092 9093
现在使用zuul 统一对外暴露端口是9090
新建一个zuul服务
pom
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>1.5.4.RELEASE</version> </dependency> </dependencies>
启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @SpringBootApplication @EnableEurekaServer @EnableZuulProxy public class ZuulServerApplication { public static void main(String[] args) { SpringApplication.run(ZuulServerApplication.class, args); } /** * 项目可以进行跨域请求。 * @return */ @Bean public WebMvcConfigurer webMvcConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*").allowedMethods("*"); } }; }
配置文件
server.port=9090 eureka.client.serviceUrl.defaultZone=http://10.38.0.5:8761/eureka/ spring.application.name=zuulserver zuul.routes.product.path=/product/** zuul.routes.product.service-id=product zuul.routes.order.path=/order/** zuul.routes.order.service-id=order
另外的服务是product 和 order
service-id 是其他服务在ererka上的服务名