k8s部署微服务springcloud从0-1(zuul网关的实现)

Posted luoguo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s部署微服务springcloud从0-1(zuul网关的实现)相关的知识,希望对你有一定的参考价值。

Zuul实现

一.新建项目并配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>sm1234_parent</artifactId>
        <groupId>cn.sm1234</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>sm1234_zuul</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

2.应用配置文件配置

server:
  port: 8888
spring:
  application:
    name: sm1234-zuul
zuul:
  routes:
    app:
      path: /app/*
      serviceId: sm1234-article

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:7000/eureka
    instance:
      instance-id: zuul.com
      prefer-ip-address: true

3.应用的编写

package cn.sm1234.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
 * 微服务网关
 */
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulApplication 
    public static void main(String[] args) 
        SpringApplication.run(ZuulApplication.class,args);
    

4.文章微服务和Zuul注入Eureka

4.1 修改微服务pom文件

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

4.2 修改应用配置文件

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:7000/eureka
    instance:
      instance-id: zuul.com  #不同服务需要修改
      prefer-ip-address: true

4.3 声明是Eureka客户端

package cn.sm1234.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
 * 微服务网关
 */
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient   //标记是Eureka客户端
public class ZuulApplication 
    public static void main(String[] args) 
        SpringApplication.run(ZuulApplication.class,args);
    

4.4 通过zuul网关访问

以上是关于k8s部署微服务springcloud从0-1(zuul网关的实现)的主要内容,如果未能解决你的问题,请参考以下文章

k8s部署微服务springcloud从0-1(zuul网关的实现)

k8s+SpringCloud全栈技术:在k8s平台部署亿级高并发的SpringCloud项目

docker+k8s+springcloud微服务集群部署实例

SpringCloud微服务电商系统在Kubernetes集群中上线详细教程

spring cloud 微服务 需要service层吗

基于k8s环境的spring-cloud服务发现和调用配置