欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot 2.1.x 中 Spring Cloud完整入门(Greenwich版本)

程序员文章站 2022-05-22 09:41:51
...

参考官方文档
https://cloud.spring.io/spring-cloud-static/Greenwich.SR1/single/spring-cloud.html)

1.父工程pom文件配置
由于父工程的pom文件能够起到版本依赖的作用,并且其他Module模块均继承该pom文件,所以我们在父工程pom文件中配置spring cloud依赖。

<?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.chenchen</groupId>
    <artifactId>ccdn_parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>ccdn_eureka</module>
    </modules>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.9</java.version>
        <maven.compiler.source>1.9</maven.compiler.source>
        <maven.compiler.target>1.9</maven.compiler.target>
        <mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version>
        <mybatisplus.version>2.2.0</mybatisplus.version>
        <fastjson.version>1.2.39</fastjson.version>
        <gson.version>2.8.0</gson.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

</project>

注意:我使用的jdk版本是1.9,而在9以后,jaxb是没有默认加载的,所以需要手动导入一下依赖,如果是8的版本则不需要

<dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

2.新建Module(Eureka服务器)
首先我们需要新建Eureka服务注册中心
1>在父工程上右键单击->new->Module->新建Maven工程
2>修改新创建工程的pom文件

<?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>ccdn_parent</artifactId>
        <groupId>com.chenchen</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ccdn_eureka</artifactId>

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

</project>

3>编辑application.yml文件

server:
  port: 6868
spring:
  application:
    name: ccdn-eureka
eureka:
  client:
    # eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。
    register-with-eureka: false
    # eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8800/eureka/

4>Application启动类

package com.chenchen.eureka;

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

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

5>Run 启动类查看运行效果
SpringBoot 2.1.x 中 Spring Cloud完整入门(Greenwich版本)
到此Eureka注册中心便搭建好了。

3.创建Eureka客户端
1> 在要注册的客户端模块的pom文件中导入依赖

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

2> 修改application.yml配置文件,增加如下配置

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:6868/eureka/
  instance: #在部署到线上时能够跨域访问
    prefer-ip-address: true

3> 在启动类上添加@EnableEurekaClient注解

package com.chenchen.article;
import com.chenchen.common.util.IdWorker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableEurekaClient
public class ArticleApplication {

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

	@Bean
	public IdWorker idWorkker(){
		return new IdWorker(1, 1);
	}
	
}

4.查看注册效果
SpringBoot 2.1.x 中 Spring Cloud完整入门(Greenwich版本)

至此Eureka的服务和客户端就已经搭建好了。

4.Eureka客户端之间的调用
在我的父工程下有两个子模块,一个是ccdn_base,另一个是ccdn_qa,这两个模块已经通过上面的步骤完成了Eureka的注册。
下面要通过ccdn_qa调用ccdn_qa中LabelController的findLabelById方法来演示Eureka客户端之间的调用
通俗的讲ccdn_base是生产者,而ccdn_qa是消费者

1> 修改ccdn_qa的pom文件,加入一下依赖

Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。这里我们采用feign方式。

<dependency>
		  <groupId>org.springframework.cloud</groupId>
		  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2> 创建接口类

package com.chenchen.qa.client;

import com.chenchen.common.entity.ResultEntity;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("ccdn-base")
public interface BaseClient {

    /**
     * 根据Id查询标签
     * @param labelId
     * @return
     */
    @RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET)
    public ResultEntity findLabelById(@PathVariable("labelId") String labelId);
}

注意:@FeiClient中的参数是服务名,是在application.yml中配置的
SpringBoot 2.1.x 中 Spring Cloud完整入门(Greenwich版本)
并且是不可出现下划线的,这里需要注意一下!!!

4> 在ccdn_qaController中编写方法调用BaseClient接口

@RequestMapping("/problem")
public class ProblemController {

	@Autowired
	private ProblemService problemService;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private BaseClient baseClient;

	@RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET)
	public ResultEntity findByLabelId(@PathVariable("labelId") String labelId) {
		ResultEntity result = baseClient.findLabelById(labelId);
		return result;
	}

4>在ccdn_qa启动类上添加@EnableDiscoveryClient和@EnableFeignClients这两个注解,一个是可以发现客户端,两一个是采用Feign的方式

package com.chenchen.qa;
import com.chenchen.common.util.IdWorker;
import com.chenchen.common.util.JwtUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

/**
 * 问答微服务启动类
 * @author chenchen
 */
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class QaApplication {

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

	@Bean
	public IdWorker idWorkker(){
		return new IdWorker(1, 1);
	}

	@Bean
	public JwtUtil jwtUtil() {
		return new JwtUtil();
	}
}

5> 启动ccdn_eureka,ccdn_base,ccdn_qa这三个服务,在postman中进行测试
SpringBoot 2.1.x 中 Spring Cloud完整入门(Greenwich版本)
测试通过!!!!!

至此一个完整的spring cloud(Greenwich版本)功能便演示成功!