IDEA搭建Finchley.SR2版本的SpringCloud父子基础项目-------Ribbon负载均衡

1.概念

Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。

1.1负载均衡

集中式LB:
即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责把访问请求通过某种策略转发至服务的提供方;
进程内LB
将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选择出一个合适的服务器。
Ribbon就属于进程内LB,它只是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址。

https://github.com/Netflix/ribbon/wiki/Getting-Started

1.2类图

在这里插入图片描述

Ribbon在工作时分成两步
第一步先选择 EurekaServer ,它优先选择在同一个区域内负载较少的server.
第二步再根据用户指定的策略,在从server取到的服务注册列表中选择一个地址。
其中Ribbon提供了多种策略:比如轮询、随机和根据响应时间加权。

1.3新建工程

参考provider-pay,新建两份,分别命名为provider-pay-8002,provider-pay-8003

1.4代码

1.1父工程

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.xql</groupId>
    <artifactId>PayUtilsMode</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.18.8</lombok.version>
        <api.version>1.0-SNAPSHOT</api.version>
        <utils.version>1.0-SNAPSHOT</utils.version>
        <springloaded.version>1.2.8.RELEASE</springloaded.version>
        <fastjson.version>1.2.83</fastjson.version>
        <ribbo.version>1.4.7.RELEASE</ribbo.version>
    </properties>

    <!-- 通过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。
    Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,
    然后它就会使用在这个dependencyManagement元素中指定的版本号。 -->
    <dependencyManagement>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.12</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <finalName>PayUtilsMode</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimit>$</delimit>
                    </delimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>eureka</module>
        <module>eureka-one</module>
        <module>eureka-two</module>
        <module>provider-pay</module>
        <module>pay-api</module>
        <module>consumer-pay</module>
        <module>provider-pay-8002</module>
        <module>provider-pay-8003</module>
    </modules>

</project>

客户端consumer-pay

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

    <artifactId>consumer-pay</artifactId>
    <description>支付微服务消费者</description>


    <dependencies>
        <dependency><!-- 引入自己定义的api通用包,可以使用Entity -->
            <groupId>com.xql</groupId>
            <artifactId>pay-api</artifactId>
            <version>${api.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>${springloaded.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <!-- Ribbon相关 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>${ribbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>${ribbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

    </dependencies>
</project>

#改端口号
server.port= 8002

#配置json日期出入格式
spring.mvc.date-format= yyyy-MM-dd HH:mm:ss
spring.jackson.date-format= yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone= GMT+8

#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
eureka.client.register-with-eureka=false

eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

package com.xql.rule;

/**
 * @author 许清磊
 * @version 1.0
 * @ClassName ConfigBean
 * @description: TODO
 * @date 2021/12/7 9:59
 */
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class MyRule
{

    @Bean
    public IRule myRule1()
    {
//        return new RandomRule();
        return new RetryRule();
    }
}



package com.xql.consumer;

import com.xql.rule.MyRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

/**
 * @author 许清磊
 * @version 1.0
 * @ClassName ConsumerPayApplication
 * @description: TODO
 * @date 2021/12/7 10:06
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaClient
@RibbonClient(name = "PROVIDER-PAY",configuration = MyRule.class)
public class ConsumerPayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerPayApplication.class, args);
    }

}

package com.xql.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.xql.api.model.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @author 许清磊
 * @version 1.0
 * @ClassName ConsumerPayController
 * @description: TODO
 * @date 2021/12/7 10:03
 */

@RestController
public class ConsumerPayController
{
//    private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://PROVIDER-PAY";
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value="/consumer/pay/add")
    public boolean add(Orders orders)
    {
        return restTemplate.postForObject(REST_URL_PREFIX+"/pay/add", orders, Boolean.class);
    }

    @RequestMapping(value="/consumer/pay/get/{id}")
    public Orders get(@PathVariable("id") String id)
    {
        return restTemplate.getForObject(REST_URL_PREFIX+"/pay/get/"+id, Orders.class);
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value="/consumer/pay/list")
    public JSONObject list()
    {
        return restTemplate.getForObject(REST_URL_PREFIX+"/pay/list", JSONObject.class);
    }
}





package com.xql.consumer.configBean;

/**
 * @author 许清磊
 * @version 1.0
 * @ClassName ConfigBean
 * @description: TODO
 * @date 2021/12/7 9:59
 */
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ConfigBean
{
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }

//    @Bean
//    @LoadBalanced
//    public IRule myRule()
//    {
        return new RandomRule();
//        return new RetryRule();
//    }
}



eureka集群
eureka

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

    <artifactId>eureka</artifactId>


    <dependencies>
        <!--eureka-server服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>${springloaded.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>

#改端口号
server.port= 7001
#改项目名
#server.servlet.context-path= /eureka


#eureka服务端的实例名称
eureka.instance.hostname= eureka7001.com
#false表示不向注册中心注册自己。
eureka.client.register-with-eureka= false 
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
eureka.client.fetch-registry=false
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。 单机
#eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
eureka.client.service-url.defaultZone=http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

#禁用自我保护模式
#eureka.server.enable-self-preservation = false




package com.xql.eureka;

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

/**
 * @author 许清磊
 * @version 1.0
 * @ClassName EurekaApplication
 * @description: TODO
 * @date 2021/12/7 20:44
 */
@SpringBootApplication
@EnableEurekaServer//EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}

//package com.xql.eureka.configuration;
//
///**
// * @author 许清磊
// * @version 1.0
// * @ClassName FeignBeanFactoryPostProcessor
// * @description: TODO
// * @date 2021/12/8 10:46
// */
//import java.util.Arrays;
//
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.config.BeanDefinition;
//import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
//import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
//import org.springframework.stereotype.Component;
///**
// * 防止热部署  项目对象注入报错  或者pom去掉本项目热部署相关jar
// */
//@Component
//public class FeignBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
//
//    @Override
//    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//        if (containsBeanDefinition(beanFactory, "feignContext", "eurekaAutoServiceRegistration")) {
//            BeanDefinition bd = beanFactory.getBeanDefinition("feignContext");
//            bd.setDependsOn("eurekaAutoServiceRegistration");
//        }
//    }
//
//    private boolean containsBeanDefinition(ConfigurableListableBeanFactory beanFactory, String... beans) {
//        return Arrays.stream(beans).allMatch(b -> beanFactory.containsBeanDefinition(b));
//    }
//}

eureka-one

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

    <artifactId>eureka-one</artifactId>

    <dependencies>
        <!--eureka-server服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>${springloaded.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>


#改端口号
server.port= 7002
#改项目名
#server.servlet.context-path= /eureka


#eureka服务端的实例名称
eureka.instance.hostname= eureka7002.com
#false表示不向注册中心注册自己。
eureka.client.register-with-eureka= false 
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
eureka.client.fetch-registry=false
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。 单机
#eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

#禁用自我保护模式
#eureka.server.enable-self-preservation = false

package com.xql.eurekaone;

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

@SpringBootApplication
@EnableEurekaServer//EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaOneApplication {

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

}

eureka-two

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

    <artifactId>eureka-two</artifactId>

    <dependencies>
        <!--eureka-server服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>${springloaded.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>


#改端口号
server.port= 7003
#改项目名
#server.servlet.context-path= /eureka


#eureka服务端的实例名称
eureka.instance.hostname= eureka7003.com
#false表示不向注册中心注册自己。
eureka.client.register-with-eureka= false 
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
eureka.client.fetch-registry=false
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。 单机
#eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

#禁用自我保护模式
#eureka.server.enable-self-preservation = false


package com.xql.eurekatwo;

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

@SpringBootApplication
@EnableEurekaServer//EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaTwoApplication {

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

}

提供者provider-pay
provider-pay

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

    <artifactId>provider-pay</artifactId>
    <description>支付微服务提供者</description>

    <dependencies>
        <dependency><!-- 引入自己定义的api通用包,可以使用Entity -->
            <groupId>com.xql</groupId>
            <artifactId>pay-api</artifactId>
            <version>${api.version}</version>
        </dependency>
        <!-- 将微服务provider侧注册进eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <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>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>${springloaded.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xql</groupId>
            <artifactId>utils</artifactId>
            <version>${utils.version}</version>
        </dependency>
    </dependencies>

</project>
package com.xql.provider;

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

/**
 * @author 许清磊
 * @version 1.0
 * @ClassName ProviderPayApplication
 * @description: TODO
 * @date 2021/12/8 9:25
 */
@SpringBootApplication(scanBasePackages={"com.xql.provider","com.xql.config","com.xql.utils","com.xql.interceptor"})
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
public class ProviderPayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderPayApplication.class, args);
    }

}

package com.xql.provider.controller;

/**
 * @author 许清磊
 * @version 1.0
 * @ClassName PayController
 * @description: TODO
 * @date 2021/12/7 8:59
 */

import com.xql.api.model.DTO.TestDto;
import com.xql.api.model.Orders;
import com.xql.model.Vo.PageVO;
import com.xql.model.Vo.ResultVO;
import com.xql.provider.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
public class PayController
{
    @Autowired
    private OrdersService ordersService;

    @RequestMapping(value="/pay/add",method= RequestMethod.POST)
    public boolean add(@RequestBody Orders orders)
    {
        return ordersService.add(orders);
    }

    @RequestMapping(value="/pay/get/{id}",method= RequestMethod.GET)
    public Orders get(@PathVariable("id") String id)
    {
        return ordersService.get(id);
    }

    @RequestMapping(value="/pay/list",method= RequestMethod.GET)
    public ResultVO<PageVO<Orders>> list(TestDto testDto)
    {
        return ordersService.list(testDto);
    }



}




#改端口号
server.port= 8001
#改项目名
#server.servlet.context-path= /provider-pay

spring.application.name=provider-pay

#配置热部署
server.servlet.jsp.init-parameters.development= true
#mysql数据源
spring.datasource.type= com.alibaba.druid.pool.DruidDataSource
spring.datasource.url= jdbc:mysql://xxxxx:3306/PayUtil?useUnicode=true&characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password= xxxx

#客户端注册进eureka服务列表内
#eureka.client.service-url.defaultZone=http://localhost:7001/eureka
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
#自定义服务名称信息
eureka.instance.instance-id=provider-pay8001
#访问路径可以显示IP地址
eureka.instance.prefer-ip-address=true

#点击服务名称出来的服务信息详情和版本号配置
info.app.name=provider-pay8001
info.company.name=http:baidu.com
info.build.artifactId= ${project.artifactId}
info.build.version= ${project.version}


#fdfs
fdfs.soTimeout=1500
fdfs.connectTimeout=600
fdfs.thumbImage.width=150
fdfs.thumbImage.height=150
fdfs.trackerList[0]=xxxxx:22122
fdfs.web-server-url=http://xxxx/

#扫描mybatis-config.xml
mybatis.config-location=classpath:mybatis/mybatis-config.xml
#起别名
mybatis.type-aliases-package= com.xql.provider.entity
#配置mapper实现类
mybatis.mapper-locations= classpath:com/xql/provider/mapper/*Mapper.xml

#配置视图解析器
spring.mvc.view.prefix= /
spring.mvc.view.suffix= .jsp
##配置json日期出入格式
#spring.mvc.date-format= yyyy-MM-dd HH:mm:ss
#spring.jackson.date-format= yyyy-MM-dd HH:mm:ss
#spring.jackson.time-zone= GMT+8

#配置上传文件大小
spring.servlet.multipart.max-request-size= 10000MB
spring.servlet.multipart.max-file-size= 1000MB

#配置Slf4j
#设置父日志级别
logging.level.root= error
#设置指定包的日志级别
logging.level.com.xql.provider.mapper= debug
logging.level.com.xql.provider.service=info
logging.level.com.xql.provider.dao=info
logging.level.com.xql.provider.controller=info

#文件名|路径 建议使用 / 代表项目所在磁盘的根目录 此配置使logging.path失效
logging.file=D:/rizhi/provider.log


provider-pay-8002

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

    <artifactId>provider-pay-8002</artifactId>
    <description>支付微服务提供者</description>

    <dependencies>
        <dependency><!-- 引入自己定义的api通用包,可以使用Entity -->
            <groupId>com.xql</groupId>
            <artifactId>pay-api</artifactId>
            <version>${api.version}</version>
        </dependency>
        <!-- 将微服务provider侧注册进eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <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>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>${springloaded.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xql</groupId>
            <artifactId>utils</artifactId>
            <version>${utils.version}</version>
        </dependency>
    </dependencies>

</project>

#改端口号
server.port= 8004
#改项目名
#server.servlet.context-path= /provider-pay

spring.application.name=provider-pay

#配置热部署
server.servlet.jsp.init-parameters.development= true
#mysql数据源
spring.datasource.type= com.alibaba.druid.pool.DruidDataSource
spring.datasource.url= jdbc:mysql://xxxxxxx:3306/cloudDB02?useUnicode=true&characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password= xxxx

#客户端注册进eureka服务列表内
#eureka.client.service-url.defaultZone=http://localhost:7001/eureka
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
#自定义服务名称信息
eureka.instance.instance-id=provider-pay8002
#访问路径可以显示IP地址
eureka.instance.prefer-ip-address=true

#点击服务名称出来的服务信息详情和版本号配置
info.app.name=provider-pay8003
info.company.name=http:baidu.com
info.build.artifactId= ${project.artifactId}
info.build.version= ${project.version}


#fdfs
fdfs.soTimeout=1500
fdfs.connectTimeout=600
fdfs.thumbImage.width=150
fdfs.thumbImage.height=150
fdfs.trackerList[0]=xxxxx:22122
fdfs.web-server-url=http://xxxxx/

#扫描mybatis-config.xml
mybatis.config-location=classpath:mybatis/mybatis-config.xml
#起别名
mybatis.type-aliases-package= com.xql.provider.entity
#配置mapper实现类
mybatis.mapper-locations= classpath:com/xql/provider/mapper/*Mapper.xml

#配置视图解析器
spring.mvc.view.prefix= /
spring.mvc.view.suffix= .jsp
##配置json日期出入格式
#spring.mvc.date-format= yyyy-MM-dd HH:mm:ss
#spring.jackson.date-format= yyyy-MM-dd HH:mm:ss
#spring.jackson.time-zone= GMT+8

#配置上传文件大小
spring.servlet.multipart.max-request-size= 10000MB
spring.servlet.multipart.max-file-size= 1000MB

#配置Slf4j
#设置父日志级别
logging.level.root= error
#设置指定包的日志级别
logging.level.com.xql.provider.mapper= debug
logging.level.com.xql.provider.service=info
logging.level.com.xql.provider.dao=info
logging.level.com.xql.provider.controller=info

#文件名|路径 建议使用 / 代表项目所在磁盘的根目录 此配置使logging.path失效
logging.file=D:/rizhi/provider.log


package com.xql.provider;

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

@SpringBootApplication(scanBasePackages={"com.xql.provider","com.xql.config","com.xql.utils","com.xql.interceptor"})
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
public class ProviderPay8002Application {

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

}

provider-pay-8003

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

    <artifactId>provider-pay-8003</artifactId>
    <description>支付微服务提供者</description>

    <dependencies>
        <dependency><!-- 引入自己定义的api通用包,可以使用Entity -->
            <groupId>com.xql</groupId>
            <artifactId>pay-api</artifactId>
            <version>${api.version}</version>
        </dependency>
        <!-- 将微服务provider侧注册进eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <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>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>${springloaded.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xql</groupId>
            <artifactId>utils</artifactId>
            <version>${utils.version}</version>
        </dependency>
    </dependencies>

</project>

#改端口号
server.port= 8003
#改项目名
#server.servlet.context-path= /provider-pay

spring.application.name=provider-pay

#配置热部署
server.servlet.jsp.init-parameters.development= true
#mysql数据源
spring.datasource.type= com.alibaba.druid.pool.DruidDataSource
spring.datasource.url= jdbc:mysql://xxxxxxx:3306/cloudDB03?useUnicode=true&characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password= xxxxxx

#客户端注册进eureka服务列表内
#eureka.client.service-url.defaultZone=http://localhost:7001/eureka
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
#自定义服务名称信息
eureka.instance.instance-id=provider-pay8003
#访问路径可以显示IP地址
eureka.instance.prefer-ip-address=true

#点击服务名称出来的服务信息详情和版本号配置
info.app.name=provider-pay8003
info.company.name=http:baidu.com
info.build.artifactId= ${project.artifactId}
info.build.version= ${project.version}


#fdfs
fdfs.soTimeout=1500
fdfs.connectTimeout=600
fdfs.thumbImage.width=150
fdfs.thumbImage.height=150
fdfs.trackerList[0]=xxxxx:22122
fdfs.web-server-url=http://xxxxx/

#扫描mybatis-config.xml
mybatis.config-location=classpath:mybatis/mybatis-config.xml
#起别名
mybatis.type-aliases-package= com.xql.provider.entity
#配置mapper实现类
mybatis.mapper-locations= classpath:com/xql/provider/mapper/*Mapper.xml

#配置视图解析器
spring.mvc.view.prefix= /
spring.mvc.view.suffix= .jsp
##配置json日期出入格式
#spring.mvc.date-format= yyyy-MM-dd HH:mm:ss
#spring.jackson.date-format= yyyy-MM-dd HH:mm:ss
#spring.jackson.time-zone= GMT+8

#配置上传文件大小
spring.servlet.multipart.max-request-size= 10000MB
spring.servlet.multipart.max-file-size= 1000MB

#配置Slf4j
#设置父日志级别
logging.level.root= error
#设置指定包的日志级别
logging.level.com.xql.provider.mapper= debug
logging.level.com.xql.provider.service=info
logging.level.com.xql.provider.dao=info
logging.level.com.xql.provider.controller=info

#文件名|路径 建议使用 / 代表项目所在磁盘的根目录 此配置使logging.path失效
logging.file=D:/rizhi/provider.log


package com.xql.provider;

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

@SpringBootApplication(scanBasePackages={"com.xql.provider","com.xql.config","com.xql.utils","com.xql.interceptor"})
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
public class ProviderPay8003Application {

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

}

项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从入门小白到小黑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值