我正在练习在Spring Boot上使用Netflix Zuul和Eureka Server使用负载平衡。而且我遇到了一个问题。 因此,我创建了2个Spring Boot项目。一个项目用于创建Eureka Server,另一个项目用于创建Eureka Client。这是两个项目的代码。
Eureka Server项目
package com.example.eureka;
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);
}
}
和application.yaml
# This default profile is used when running a single instance completely standalone:
spring:
profiles: default
server:
port: 9000
eureka:
instance:
hostname: my-eureka-server.com
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
---
spring:
profiles: united-states
application:
name: eureka-server-clustered # ==> This is Service-Id
server:
port: 9001
eureka:
instance:
hostname: my-eureka-server-us.com
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://my-eureka-server-fr.com:9002/eureka/,http://my-eureka-server-vn.com:9003/eureka/
---
spring:
profiles: france
application:
name: eureka-server-clustered # ==> This is Service-Id
server:
port: 9002
eureka:
instance:
hostname: my-eureka-server-fr.com
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://my-eureka-server-us.com:9001/eureka/,http://my-eureka-server-vn.com:9003/eureka/
---
spring:
profiles: vietnam
application:
name: eureka-server-clustered # ==> This is Service-Id
server:
port: 9003
eureka:
instance:
hostname: my-eureka-server-vn.com
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://my-eureka-server-us.com:9001/eureka/,http://my-eureka-server-fr.com:9002/eureka/
我已经将新域名添加到C:\Windows\System32\drivers\etc\host
中
还有Eureka_Client项目
package com.example.config;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.netflix.discovery.EurekaClient;
@SpringBootApplication
@Controller
public class EurekaClientApplication {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
@Lazy
private EurekaClient eurekaClient;
@Value("${eureka.instance.appname}")
private String appName;
@Value("${server.port}")
private String port;
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@RequestMapping("/client")
public String getService(Model m) {
List<String> listService = discoveryClient.getServices();
String name = eurekaClient.getApplication(appName).getName();
String info = "Name app: " + name + " Port: " + port;
m.addAttribute("info",info);
m.addAttribute("listService",listService);
return "service";
}
@RequestMapping("/home")
public String goHome() {
return "home";
}
}
和application.yaml
spring:
application:
name: ABC-SERVICE # ==> This is Service-Id
---
# Items that apply to ALL profiles:
eureka:
instance:
appname: ABC-SERVICE # ==> This is a instance of ABC-SERVICE
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://my-eureka-server-us.com:9001/eureka
server:
port: 8000
---
spring:
profiles: firstService
eureka:
instance:
appname: ABC-SERVICE # ==> This is a instance of ABC-SERVICE
preferIpAddress: true
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://my-eureka-server-us.com:9001/eureka
server:
port: 8001
---
spring:
profiles: secondService
eureka:
instance:
appname: ABC-SERVICE # ==> This is a instance of ABC-SERVICE
preferIpAddress: true
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://my-eureka-server-us.com:9001/eureka
server:
port: 8002
---
spring:
profiles: thirdService
eureka:
instance:
appname: ABC-SERVICE # ==> This is a instance of ABC-SERVICE
preferIpAddress: true
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://my-eureka-server-us.com:9001/eureka
server:
port: 8003
我同时运行了两个具有不同弹簧轮廓的项目,并且效果很好。当我点击该网址http://localhost:8001/home
或http://localhost:8001/service
时,它会返回我想要的内容。
然后,我为Netflix Zuul创建了一个项目,这是其application.yaml
文件
eureka:
client:
fetchRegistry: true
registerWithEureka: true
serviceUrl:
defaultZone: http://my-eureka-server-us.com:9001/eureka
instance:
preferIpAddress: true
server:
port: 8762
spring:
application:
name: zuul-server
当我点击此网址localhost:8762/ABC-SERVICE/home
或localhost:8762/ABC-SERVICE/client
时,浏览器返回白标签页面错误。
我知道我的项目中Netflix Zuul的配置有问题。你们可以帮我弄清楚吗?
答案 0 :(得分:0)
您似乎没有配置路线。 以下是有关如何配置此设置的一些信息:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.2.RELEASE/single/spring-cloud-netflix.html#netflix-zuul-reverse-proxy
编辑:如果您忘记了@EnableZuulProxy
注释,则很可能是原因。