zuul网关找不到服务_Zuul网关服务

微服务调用过程

前面的博客文章已经介绍过spring cloud的服务的相关内容信息,那么我们回顾一下多个微服务的调用过程。微服务一般会由不同的团队去维护,,那么就会由不同的域名和IP。客户端不可能维护那么多的域名,所以就需要统一的维护所有的域名。这个就是spring cloud提供的网关,网关维护着所有的微服务,客户端通过请求到网关,由网关分发到不同的微服务,这样客户端就只需要维护网关的域名即可。

使用网关服务,将网关对外,实现简化和统一管理。网关就类似我们的路由器,由于所有的请求都要经过网关,所以可以在网关中拦截请求,过滤非法请求等。

简介

上面已经简单说明了网关的作用,这里我们对网关的功能简单介绍一下,但是这里只搭建简单的网关服务,无法详细介绍所有功能。但是我相信google总有你需要的东西。

Zuul是Netflix开源的服务网关,它可以和前面介绍的Eureka/Ribbon/Hystrix/Feign等组件很好的兼任使用。Zuul的核心是过滤器,通过过滤器实现功能。

身份认证与安全

审查与监控

动态链路

压力测试

负载均衡

静态响应处理

容错降级

Zuul默认使用Apache HTTP Client作为HTTP客户端。如果要使用RestClient可以配置ribbon.restclient.enabled=true。如果要使用okhttp3.0kHttpClient可以配置ribbon.okhttp.enabled=true。

实现服务网关

1.maven依赖

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-zuul

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.cloud

spring-cloud-starter-openfeign

org.springframework.boot

spring-boot-starter-test

test

2.启动类添加注解@EnableZuulProxy

@EnableZuulProxy

@SpringBootApplication

public class ApiGatewayApplication {

public static void main(String[] args) {

SpringApplication.run(ApiGatewayApplication.class, args);

}

}

3.编写application.yml配置文件

server:

port: 9000

spring:

application:

name: api-gateway

eureka:

client:

service-url:

default-zone: http://localhost:8761/eureka/

zuul:

routes:

provider-user: /provider-user/**

consumer: /consumer/**

这里需要对zuul.routes.xxxx配置做讲解。zuul.routes是固定的,后面的provider-user/consumer是微服务在注册中心的名称。后面的值是映射地址。所有的/provider-user/匹配的请求都会被转发到provider-user名称的微服务中。/consumer也是一样的道理。当然写法有多种,这里是简便的写法。

Eureka.client.service-url.default-zone的作用就是将网关服务注册到注册中心。

只要启动网关服务,通过网关的ip和域名+zuul.routes中配置的路由规则就可以访问到对于的服务。

4.路由配置介绍

忽略指定的服务:zuul.ignored-services=名称1,名称2

忽略所有微服务,只路由指定的服务:zuul.ignored-services:’*’;zuul.routes.consumer=/consumer/**

忽略某些路径:zuul.ignored-patterns=/**/admin/**(包含admin的都被忽略)

关注微信公众号(程序员小兔)不定期分享技术

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,Java Spring Cloud 项目的接口可以不通过 Zuul 路由进行访问。如果您希望禁止外部访问某些接口,可以通过 Spring Security 进行控制。以下是一个简单的示例: 1. 首先,您需要在 pom.xml 文件中添加 Spring Security 依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 然后,在您的 Spring Boot 应用程序类中添加注释 @EnableWebSecurity,并创建一个继承自 WebSecurityConfigurerAdapter 的配置类: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").authenticated() .and().httpBasic() .and().csrf().disable(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 3. 在上面的代码中,我们配置了只有经过身份验证的用户才能访问“/api”路径下的接口,并使用了基本身份验证。我们还使用了一个内存中的用户存储。 4. 最后,您可以在您的控制器类中使用注释 @PreAuthorize,以限制特定方法的访问权限。例如: ```java @RestController @RequestMapping("/api") public class MyController { @GetMapping("/public") public String publicMethod() { return "This method is public."; } @GetMapping("/private") @PreAuthorize("hasRole('USER')") public String privateMethod() { return "This method is private."; } } ``` 在上面的代码中,我们使用了注释 @PreAuthorize,并指定了只有具有“USER”角色的用户才能访问“/api/private”接口。 希望这些信息能帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值