用了springcloud一段时间了
今天想动手搭建,结果忘了
注册中心和服务都建好了
天真的以为可以通过注册中心的地址加上服务名就可以访问服务了
结果忘了网关这一茬
看来还是对springcloud组件的功能了解非常不足
先贴这块了
注册中心
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudApplication.class, args);
}
}
#端口号
server.port=1111
# eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。
# 由于当前这个应用就是Eureka Server,故而设为false
eureka.client.register-with-eureka=false
# eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,
# 不需要同步其他的Eureka Server节点的数据,故而设为false。
eureka.client.fetch-registry=false
# eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringServiceApplication.class, args);
}
}
#服务名称
spring.application.name=compute-service2
#端口号
server.port=2222
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.cloud.config.discovery.enabled=true
##注册中心的服务id
#spring.cloud.config.discovery.serviceId=compute-server
网关
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZuulApplication.class, args);
}
}
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=3333
spring.application.name=service-zuul
#表示只要访问以/api-a/开头的多层目录都可以路由到 id为compute-service1的服务上
#zuul.routes.compute-service2=/api-a/**
#表示只要访问以/api-a/开头的多层目录都可以路由到 id为compute-service1的服务上
#zuul.routes.compute-service1=/api-a/**
#上面的一行等同于下面的两行
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=compute-service2
本文介绍了作者在使用SpringCloud过程中,遇到通过注册中心访问服务时忽视了网关配置的问题,强调了对SpringCloud组件理解的重要性。文章内容包括注册中心、服务及网关的搭建过程。
168万+

被折叠的 条评论
为什么被折叠?



