SpringCloud微服务入门

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
都有结果,则配置正确
**

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值