我正在尝试创建与Eureka集成的多个微服务,以用于发现和Zuul作为网关。
以上所有内容都是在弹簧靴中开发的。
Eureka应用程序
application.yml
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
EurekaDiscoveryService
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@EnableEurekaServer
@Import({ FrameworkConfiguration.class })
public class EurekaDiscoveryService extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(EurekaDiscoveryService.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(EurekaDiscoveryService.class);
}
}
server:
port: 9091
servlet:
context-path: /user-service
spring:
application:
name: user
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
data:
rest:
base-path: /api
eureka:
client:
registerWithEureka: true
serviceUrl:
defaultZone: http://localhost:8761/discovery/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication(exclude={MongoAutoConfiguration.class})
@EnableDiscoveryClient
@EnableFeignClients
@EnableJpaRepositories(ConfigurationUtil.SpringConfiguration.REST_REPO_SCAN_PKG)
@EnableTransactionManagement
public class UserApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(UserApplication.class);
}
}
网关应用
application.yml
server:
port: 8762
servlet:
context-path: /gateway-service
spring:
application:
name: gateway
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration
eureka:
client:
registerWithEureka: false
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/discovery/eureka
# Map paths to services
zuul:
ignoredServices: '*'
routes:
account:
path: /account/**
service-id: account
user:
path: /user/**
serviceId: user
stripPrefix : true
网关应用
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableZuulProxy
public class ProxyApplication {
public static void main(String[] args) {
SpringApplication.run(ProxyApplication.class, args);
}
}
Web安全配置
这在网关和用户应用程序中都使用
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/api","/user").permitAll();
http.csrf().disable();
}
}
当我依次运行所有三个应用程序时,可以在Eureka服务器中的URL http://localhost:8761/discovery/ 中看到 User 和 gateway 应用程序。每当我尝试调用用户微服务特定的URL http://localhost:9091/user-service/api/user/search/searchByUserName?username=syam 时,都会得到预期的以下响应。
{
"id" : 100,
"dateofBirth" : "1985-05-20",
"firstName" : "Syam",
"lastName" : "Mohan"
}
当我尝试通过网关URL访问同一文件时,出现404错误。我使用了以下端点。
我不确定我在哪里以及哪里出错。有人可以帮我解决这个问题吗?