从0搭建SpringCloudAlibaba项目

从0搭建SpringCloudAlibaba项目

1.搭建项目

搭建父项目使用idea搭建一个普通的maven工程即可,下午所有的模块都是普通的maven工程

1.1.1 pom坐标

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <alibaba.nacos.version>2.0.4alibaba.nacos.version>
        <druid.version>1.1.10druid.version>
        <junit.version>4.1.2junit.version>
        <lombok.version>1.16.10lombok.version>
        <log4j.vsrsion>1.2.17log4j.vsrsion>
    properties>
    <dependencyManagement>
    <dependencies>
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-dependenciesartifactId>
        <version>Hoxton.SR1version>
        <type>pomtype>
        <scope>importscope>
    dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>2.1.0.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-clientartifactId>
            <version>${alibaba.nacos.version}version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-dependenciesartifactId>
            <version>2.2.2.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.28version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>${druid.version}version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.8version>
        dependency>


2. 搭建服务

这里随意搭建服务,名称自取

服务1

  • pom坐标,这里不要加版本号
  		<dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
  • bootstrap.yml配置
spring:
  application:
    name: cloudadmin
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos地址
        namespace:  #命名空间,没有不写默认为public
      config:
        server-addr: localhost:8848 #nacos地址
        file-extension: yaml
        namespace: #命名空间,没有不写默认为public
server:
  port: 8002

Fegin远程调用

*pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        
        <dependency>
            <groupId>io.github.openfeigngroupId>
            <artifactId>feign-httpclientartifactId>
        dependency>
  • 服务远程调用接口
@FeignClient(value = "cloudsystem") //value名称是消费者要调用生产者的接口
@Component
public interface SystemFeign {
    @GetMapping("/hello")
    String hello();
}

  • 消费者调用要加@EnableFeignClients注解,然后使用@Resource注入

网关Gateway

踩坑最多的模块,会出现奇奇怪怪的问题,请务必按一下配置

  • pom配置,看引入的cloud版本,高版本移除了负载均衡需要自己引入,本文引用的cloud版本不高,不需要引入负载均衡

		<dependency>
		    <groupId>org.springframework.cloudgroupId>
		    <artifactId>spring-cloud-starter-loadbalancerartifactId>
		dependency>
 		<dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
  • bootstrap.yml
server:
  port: 10010
spring:
  application:
    name: gateway
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        namespace: #命名空间ID
        server-addr: localhost:8848 #nacos地址
    gateway:
      routes:
        - id: system-service
          uri: lb://cloudsystem
          predicates:
            - Path=/system/**
        - id: admin-service
          uri: lb://cloudadmin
          predicates:
            - Path=/user/**
      default-filters:
        - StripPrefix=1 #去除前缀,不然请求会带上前缀
        - AddResponseHeader=X-Response-Default-Foo, Default-Bar


你可能感兴趣的:(java,spring,cloud,开发语言)