springcloud架构搭建(二) Zuul路由服务器搭建及配置

springcloud架构搭建(二) Zuul路由服务器搭建及配置

  • 本文只针对刚开始接触或者没有接触过springcloud的小白
  • 如果下面有什么不足之处请大家及时指出
  • 写这个博客的目的主要是为了大家共同学习交流、共同进步

第一步,创建一个普通的springboot项目

以下方法都可以快速创建一个boot项目:
1. 浏览器访问http://start.spring.io/,填写信息,下载zip包,加压到你的ide的工作空间直接使用。
2. 使用idea自动生成boot项目(如果没创建过自行百度……)
3. 使用eclipse创建boot项目(如果没创建过自行百度……)

第二步,在pom.xml中添加依赖(以下是我的完整依赖)


<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">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.lxbgroupId>
    <artifactId>zuulartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>zuulname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.4.RELEASEversion>
        <relativePath />
    parent>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Dalston.SR4spring-cloud.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zuulartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
                <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>springloadedartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
        dependency>
        
        <dependency>
            <groupId>de.codecentricgroupId>
            <artifactId>spring-boot-admin-starter-clientartifactId>
            <version>1.3.2version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.jolokiagroupId>
            <artifactId>jolokia-coreartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-securityartifactId>
        dependency>
  dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

第三步,修改配置文件application-dev.properties

  1. 将application.properties文件名称修改为application-dev.properties。这个是生产环境的
  2. 添加内容
#appname默认
spring.application.name=zuul-server
#注册端口号
server.port=81

#安全认证
security.basic.enabled=false
security.user.name=admin
security.user.password=123456

spring.boot.admin.url=http://localhost:8080
#-----------------------编写者相关信息----------------------
#-----------------------编写者相关信息----------------------
info.owner=LTY
info.version=@project.version@
info.app.name=@project.name@
info.app.version=@project.version@
info.app.description=@project.description@
info.app.spring-boot-version=@project.parent.version@


#------------------------服务与发现相关信息---------------------
#------------------------服务与发现相关信息---------------------
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

#路由网关相关
zuul.routes.api-b.path=/compute-service/**
zuul.routes.api-b.serviceId=compute-service

第四步,添加application.yml

spring: 
  profiles: 
    active: dev

第五步,修改Application类

  1. 添加注解
    @EnableZuulProxy
    @SpringCloudApplication

  2. 修改main() 方法

public static void main(String[] args) {
        new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);
    }

第六步,启动Application类

springcloud架构搭建(二) Zuul路由服务器搭建及配置_第1张图片
配置完成!

你可能感兴趣的:(cloud,服务器,架构)