微服务-注册中心、配置中心、网关

 pom.xml 

<!--服务注册/发现-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!--配置中心来做配置管理-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

 

 1、Spring Cloud Alibaba -Nacos  [作为注册中心]
       1)、spring-cloud-alibaba:https://github.com/alibaba/spring-cloud-alibaba

 

      2)、 nacos文档:Nacos 快速开始

     3)、下载 Nacos Server

      4)、启动Nacos Server 

               a、双击bin中的startup.cmd文件

               b、访问 http://localhost:8848/nacos/

               c、使用默认的nacos/nacos进行登录

      5)、将微服务注册到 nacos

                a 、修改pom.xml,引入Nacos Discovery Starter

<dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

                b、在application.yml配置文件中配置nacos地址

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

                c、使用 @EnableDiscoveryClient开启服务注册发现功能

@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

                d、启动应用,查看nacos服务列表是否已经注册服务

     6)、feign的远程调用

                a、引入open-feign

                b、开启@EnableFeignClients(basePackages = "xxx.xxx.xxx")

                c、编写接口,进行远程调用

/**
 * 这是一个声明式的远程调用
 */
@FeignClient("stores")
public interface StoreClient {
    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Store> getStores();
    @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
    Store update(@PathVariable("storeId") Long storeId, Store store);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Cloud跨域配置可以通过以下步骤来完成。 首先,在Spring Cloud项目的配置文件中进行相配置。可以通过在配置文件中设置以下属性来允许跨域访问: ```yml spring: cloud: gateway: globalcors: corsConfigurations: '[/**]': allowedOrigins: "*" allowedMethods: - GET - POST allowCredentials: true ``` 上述配置中,allowedOrigins设置为"*"表示允许来自所有域的请求。 其次,可以在编写路由规则的时候,设置cors参数来针对特定的请求路径进行跨域配置。例如: ```yml spring: cloud: gateway: routes: - id: service-route uri: http://localhost:8081 predicates: - Path=/service/** filters: - RewritePath=/service/(?<remaining>.*), /\$\{remaining} - RewritePath=/service/(?<remaining>.*), /\$\{remaining} metadata: response-headers: Access-Control-Allow-Origin: "*" Access-Control-Allow-Methods: "GET, POST" Access-Control-Max-Age: "3600" Access-Control-Allow-Headers: "Content-Type, x-requested-with, Authorization" ``` 在上述示例中,针对路径为/service/**的请求进行了跨域配置,允许来自任意域的请求,并设置了允许的请求方法和响应头信息。 最后,还可以使用自定义的CorsWebFilter来进行更灵活的跨域配置。可以通过编写一个实现CorsConfigurationSource接口的类,并在该类中自定义cors配置,然后在WebFluxConfigurer中注册该CorsConfigurationSource实现即可。 通过以上的配置和步骤,Spring Cloud可以实现跨域访问的配置。这样就能够允许不同域的请求访问,并实现前后端的数据交互。 ### 回答2: Spring Cloud Gateway 是基于Spring WebFlux框架的一个API服务,用于构建微服务架构中的路由转发和过滤器链。在Spring Cloud Gateway中配置跨域请求是相对较简单的。 在Spring Cloud Gateway中可以通过添加过滤器来实现跨域请求。首先,在配置文件中添加路由配置,指定需要跨域的路径和目标服务。然后,编写一个全局的过滤器类,通过继承GlobalFilter和Ordered接口来实现全局过滤器。在过滤器中,通过CorsUtils类来判断请求是否为跨域请求,如果是,则设置跨域响应头信息。 具体的步骤如下: 1. 在application.yml文件中配置路由: ```yaml spring: cloud: gateway: routes: - id: myRoute uri: http://example.com predicates: - Path=/api/** filters: - StripPrefix=1 ``` 2. 创建一个全局的过滤器类CorsFilter.java: ```java @Component public class CorsFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); if (CorsUtils.isCorsRequest(request)) { ServerHttpResponse response = exchange.getResponse(); HttpHeaders headers = response.getHeaders(); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PUT, OPTIONS, DELETE"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*"); headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "3600"); headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); } return chain.filter(exchange); } @Override public int getOrder() { return -1; } } ``` 3. 启动应用,并访问配置的跨域接口。 通过以上步骤,我们可以简单地在Spring Cloud Gateway中配置跨域请求。在过滤器中,我们判断请求是否为跨域请求,并在响应头中添加相的跨域信息。这样,就能够实现对跨域请求的支持。 ### 回答3: Spring Cloud是一个基于Spring Cloud的微服务架构中的核心组件,它可以用来进行统一的请求路由、请求过滤、集中认证等功能。当我们在使用Spring Cloud时,可能会遇到跨域的问题,下面我将详细介绍Spring Cloud的跨域配置。 要配置Spring Cloud的跨域,我们可以在配置文件中添加如下代码段: ``` spring: cloud: gateway: globalcors: cors-configurations: '[/**]': allowed-origins: '*' allowed-methods: - GET - POST - PUT - DELETE allowed-headers: '*' allow-credentials: true max-age: 1800 ``` 在这个配置中,我们使用`spring.cloud.gateway.globalcors.cors-configurations`参数指定了全局的CORS配置。`'[/**]'`表示匹配所有路径,我们也可以根据自己的需求指定一个具体的路径。`allowed-origins`指定了允许的源,可以使用`*`表示允许所有源。`allowed-methods`指定了允许的HTTP方法。`allowed-headers`指定了允许的自定义头信息。`allow-credentials`表示是否允许发送cookie等身份验证信息。`max-age`表示预检请求的缓存时间。 除了全局配置外,我们还可以在GatewayFilter中对特定的路径进行跨域配置,例如: ``` @Configuration public class GatewayConfig { @Bean public RouteLocator routeLocator(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.path("/api/**") .filters(f -> f.hystrix(c -> c.setFallbackUri("/fallback"))) .uri("lb://service-a")) .build(); } @Bean public CorsWebFilter corsFilter() { return new CorsWebFilter(new UrlBasedCorsConfigurationSource()); } } ``` 在上述配置中,`routeLocator()`方法用于配置路由规则,其中`.path("/api/**")`表示匹配以`/api/`开头的路径,然后使用`filters()`方法添加一些过滤器,最后使用`uri()`方法将请求转发到service-a服务。 `corsFilter()`方法用于添加CorsWebFilter,通过`UrlBasedCorsConfigurationSource`来实现对特定路径的跨域配置。 以上就是Spring Cloud跨域配置的一些常用方法,我们可以根据实际需求选择适合的方式来配置跨域。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值