andonr / vue

0 stars 0 forks source link

社区版IDEA创建spring cloud工程 #3

Open andonr opened 6 years ago

andonr commented 6 years ago

1、创建maven工程 2、编写pom.xml

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

    <groupId>com.hpa.fisetup</groupId>
    <artifactId>fisetup</artifactId>
    <version>1.0-SNAPSHOT</version><!--自己的工程名-->

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    </dependencies>
</project>

3、右键pom.xml->Maven->Create settings.xml

<?xml version="1.0"?>
<settings>
    <localRepository>E:\work\repository</localRepository><!--需要改成自己的maven的本地仓库地址-->
    <mirrors>
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <name>local private nexus</name>
                    <url>http://maven.oschina.net/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
            </repositories>

            <pluginRepositories>
                <pluginRepository>
                    <id>nexus</id>
                    <name>local private nexus</name>
                    <url>http://maven.oschina.net/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile></profiles>
</settings>

4、右键pom.xml->Maven->Download Sources 5、右键工程,创建子Module,ArtifactId设置为discovery,作为服务注册中心 pom.xml中增加依赖

    <!-- @EnableEurekaServer -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-server</artifactId>
            <!--<version>1.1.6.RELEASE</version>-->
        </dependency>
    </dependencies>

默认情况下,src/main/java是源码路径,右键java目录,新建一个包路径,如discovery 右键discovery,增加一个注册中心类,如DiscoveryApplicaion

package discovery;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplicaion {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplicaion.class, args);
    }
}

在resources目录下创建application.yml

server:
 port: 8080 #注册中心端口
eureka:
 instance:
  hostname: localhost
 client:
  registerWithEureka: false
  fetchRegistry: false
  serviceUrl:
   defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

配置启动配置,Application,java类,module信息。 启动该module,就可以访问http://localhost:8080/看到注册中心的状态了,不过目前没有服务

6、创建service0 右键工程,继续创建一个module,ArtifactId设置为service0_test,作为服务提供者 创建service0_test包路径,创建Service0Test类

package service0_test;

        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/2/1.
 */

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Service0Test {
    @GetMapping("/service0")
    public String service(){
        return "This is service";
    }

    public static void main(String[] args) {
        SpringApplication.run(Service0Test.class, args);
    }
}

在resources下创建application.yml

spring:
 application:
  name: service0
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8080/eureka/
server:
 port: 8081

对外,只和这里的name:service0有关,启动之后,可以访问 http://localhost:8081/service0 也可以在注册中心中看到service0注册成功 http://localhost:8080

7、同样方法创建service1

8、创建gateway路由 创建gateway的module pom.xml增加

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
            <version>1.3.0.RELEASE</version>
        </dependency>
    </dependencies>

增加gateway包路径,增加Gateway类

package gateway;

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;

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class Gateway {
    public static void main(String[] args) {
        SpringApplication.run(Gateway.class, args);
    }
}

在resources中增加application.yml

eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8080/eureka/
spring:
 application:
  name: gateway
server:
 port: 8083
zuul:
 routes:
  service0: /service0/**
  service1: /service1/**

zuul配置解释 https://github.com/andonr/vue/issues/3#issuecomment-362229864

访问http://localhost:8080/ 可以看到gateway也注册上去了 http://localhost:8083/service0/service0 即/service0/service0转发到service0的/service0路径 http://localhost:8083/service1/service1 都可以访问了

andonr commented 6 years ago
zuul:
  routes:
    service0: /service0_test/**  #/service0_test/**的请求会跳转到service0的**,如/service0_test/user请求会跳转到service0的/user
    service1: /service1/**
andonr commented 6 years ago

gateway 去找service 是找注册到注册中的服务名称的,在服务提供者的application.yml里面配置的

spring:
  application:
    name: service0
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/  #注册中心地址
  instance:
    hostname: localhost
    instance-id: http://localhost:8081
server:
  port: 8081

name: service0

andonr commented 6 years ago

服务和gateway都可以在注册中心上看到