SpringCloud环境搭建
已有SpringBoot基础
搭建Eureka服务器
第一步:
第二步:
第三步:
- application.yaml
server:
port: 8761 #服务器的端口
eureka:
client:
register-with-eureka: false #当有多个eureka实例进行高可靠部署时,这里应当配置为true,即当前eureka实例做为其它eureka实例的客户端,并在其它eureka上注册
fetch-registry: false #当有多个eureka进行可靠部署时,这里应当配置为true,即当eureka实例从其他eureka上获得同步注册信息
service-url:
defaultZone: http://localhost:8761/eureka #设置eureka服务器的访问地址,在浏览器上访问eureka时地址栏上应当输入http://localhost:8761
- SpringBoot的主配置类
package com.bwl.base;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //说明为Eureka服务器
public class SpringbootEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootEurekaApplication.class, args);
}
}
- 测试:访问:http://localhost:8761
服务组件
组件1
其他步骤一样,下面不同:
- application.yaml
server:
port: 8081
logging:
level:
root: info
web: trace
spring:
application:
name: my-service1 #注册在eureka上的名字
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #配置注册访问eureka的标准
instance:
prefer-ip-address: true #将客户端ip地址注册到eureka上,而默认将客户端主机名注册到eureka上
- Controller
package com.example.springbootservice1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Mr Zhao
* @version jdk1.8 tomcat8.5
* @date 2020/6/15 0:08
*/
@RestController
public class Controller {
@RequestMapping("/s1t1")
public String show(){
return "你好,这是服务组件1测试!";
}
@RequestMapping("/s1t2")
public String ahow1(){
return "你好,这是服务组件1的另一个测试!";
}
}
- 主配置类
package com.example.springbootservice1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootService1Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootService1Application.class, args);
}
}
- 如果看到下图片,则组件在eurake注册成功
网关组件Zuul
- application.yaml
server:
port: 80
spring:
application:
name: springboot-zuul
logging:
level:
root: info
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
- 主配置类
package com.example.springbootzuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy//启用网关
public class SpringbootZuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootZuulApplication.class, args);
}
}
测试:
输入:http://localhost/my-service1/s1t1
http://localhost/my-service1/s1t2
http://localhost/my-service2/s2t1
http://localhost/my-service2/s2t2
都能看到结果,则正确!!
网关配置路由
网关Zull中application.yaml中配置路由
#配置网关路由
zuul:
routes:
service1: #路由配置的名称,只要是访问前缀为/api/s1/的路径都是访问my-service1
path: /api/s1/**
serviceId: my-service1
stripPrefix: true #网关将请求地址向微服务传递时,是否忽略前缀(这里是不忽略前缀)
my-service2: #路由配置的名称,只要是访问前缀为/api/s1/的路径都是访问my-service1
path: /api/s2/**
stripPrefix: true #网关将请求地址向微服务传递时,是否忽略前缀(这里是不忽略前缀)
**测试:http://localhost/api/s1/s1t1
http://localhost/api/s1/s1t1
http://localhost/api/s2/s2t1
http://localhost/api/s2/s2t2
都有结果,则配置正确
**