IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> SpringCloud项目创建和其中遇到的一点问题 -> 正文阅读

[Java知识库]SpringCloud项目创建和其中遇到的一点问题

SpringCloud项目创建和其中遇到的一点问题

0.主要思路

实体类单独构建,provider/server/consumer分别构建,其中为了实现集群选择构建多个相同配置不同端口server,为了检验ribbon实现的负载均衡,选择构建两个provider。

provider和consumer都在server注册,互相不发生直接联系。consumer访问server通过spring.application.name找到相应的provider。

1.主项目

主项目是分布式架构的底座,不写任何代码,仅保留maven、idea配置和全局的pom.xml

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zjy</groupId>
    <artifactId>SpringCloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>SpringCloud</name>
    <description>SpringCloud</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
        <mybatis.version>2.1.4</mybatis.version>

        <mysql.version>8.0.30</mysql.version>
        <jdbc.version>2.7.3</jdbc.version>
        <druid.version>1.2.12</druid.version>
    </properties>
    <modules>

        <module>Eureka_9001</module>
        <module>Eureka_9002</module>
        <module>Eureka_9003</module>
        <module>data</module>
        <module>provider_8888</module>
        <module>consumer_8081</module>
        <module>Eureka_9003</module>
        <module>Eureka_9001</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>


        <!--JDBC相关-->

<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-jdbc</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>mysql</groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
<!--                        JDBC相关-->
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-jdbc</artifactId>
                            <version>${jdbc.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>${mysql.version}</version>
                            <scope>import</scope>
                            <type>pom</type>
                        </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.springcloud.SpringCloudApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2.pojo模块

pojo部分只存储类,不直接连接服务器,所以不需要任何配置设置

这里以mysql自带的sakilia.actor表为例创建Actor类

目录格式如下

├─data
├─.mvn
│ └─wrapper
├─src
├─main
├─java
└─com
└─zjy
├─data
└─pojo

2.1实体类

package com.zjy.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.sql.Timestamp;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Actor implements Serializable {
    //继承序列化接口备用
    private int actorId;
    private String firstName;
    private String lastName;
    private Timestamp lastUpdate;
    private String database="`sakila`.`cloud`";
}

3.服务提供者(provider)

├─.mvn
│ └─wrapper
├─src
└─main
├─java
│ └─com
│ └─zjy
│ ├─controller
│ ├─mapper
│ └─service
└─resources
└─mappers

3.1依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>provider_8888</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider_8888</name>
    <description>provider_8888</description>
    <parent>
        <artifactId>SpringCloud</artifactId>
        <groupId>com.zjy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>


        <dependency>
            <groupId>com.zjy</groupId>
            <artifactId>data</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>


    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.zjy.Provider8888Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

3.2业务代码

三层架构

mapper

package com.zjy.mapper;

import com.zjy.pojo.Actor;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ActorMapper {
    List<Actor> getAll();
    //    void insert(Actor actor);
//    boolean delete();
    Actor queryById(Integer actorId);
}

service

package com.zjy.service;

import com.zjy.pojo.Actor;
import com.zjy.mapper.ActorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActorService {
    @Autowired
    private ActorMapper actorMapper;
    public List<Actor> getAll(){
        return actorMapper.getAll();
    }
    public Actor queryById(Integer id){
        System.out.println("query for actor id="+id);
        return actorMapper.queryById(id);
    }
}

controller

package com.zjy.controller;

import com.zjy.pojo.Actor;
import com.zjy.service.ActorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller
@ResponseBody
public class ProviderActorController {
    @Autowired
    private ActorService actorService;
    @Autowired
    private EurekaDiscoveryClient eurekaDiscoveryClient;
    @RequestMapping("/list")
    public List<Actor> getAll() {
        return actorService.getAll();
    }

    @RequestMapping("/{id}")
    public Actor queryById(@PathVariable Integer id) {
        System.out.println(id);
        return actorService.queryById(id);
    }
    @RequestMapping("/desc")
    public List discovery(){
        System.out.println("instance:");
        List<ServiceInstance> instances = eurekaDiscoveryClient.getInstances("ACTOR-PROVIDER-APPLICATION");
        for (ServiceInstance instance : instances) {
            System.err.println(
                    instance.getHost() + "\t" + // 主机名称
                            instance.getPort() + "\t" + // 端口号
                            instance.getUri() + "\t" + // uri
                            instance.getServiceId() // 服务id
            );
        }
        System.out.println(new SimpleDateFormat("hh:mm:ss").format(new Date()));
        return instances;

    }
}

主程序

package com.zjy;

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;


@SpringBootApplication
@EnableEurekaClient //Eureka使能
@EnableDiscoveryClient //Discovery可以携带一些在dashboard显示的信息,具体见Controller层注掉的方法
public class Provider8888Application {

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

}

3.3配置

ActorMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zjy.mapper.ActorMapper">



    <select id="getAll" resultType="com.zjy.pojo.Actor">
        select *from sakila.actor;
    </select>
    <select id="queryById" resultType="com.zjy.pojo.Actor">
        select *from sakila.actor where `actor_id`=#{id};
    </select>



</mapper>

application.yaml

server:
  port: 8888
mybatis:
  type-aliases-package: com.zjy.pojo
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mappers/*.xml
spring:
  application:
    name: actor-provider-8888
    #惯例,Druid数据源连接,参数写入时区、编码以防乱码
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306?useUnicode=true&chractorEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456
eureka:
  client:
    service-url:
    #绑定链接,我这里将localhost映射成了eureka9001.com eureka9002.com eureka9003.com三个地址,方便后面在Eureka的dashboard观察现象。从源代码可以看到,这里绑定url输入的不是url而是一个key为defaultZone的键值对,支持一次绑定多个,中间用逗号隔开
      defaultZone: http://eureka9001.com:9001/eureka,http://eureka9002.com:9002/eureka,http://eureka9003.com:9003/eureka
  instance:
    appname: actor-provider-8888
#    命名,以便在后台观察和区分
    instance-id: 114514
    prefer-ip-address: true
   #有时后台不显示集群成员,加入这个大概可解

3.4另一provider

配置

server:
  port: 8889
mybatis:
  type-aliases-package: com.zjy.pojo
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mappers/*.xml
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306?useUnicode=true&chractorEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456
  application:
    name: actor-provider-application
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka9001.com:9001/eureka,http://eureka9002.com:9002/eureka,http://eureka9003.com:9003/eureka

其他类和依赖都与前一provider完全一致,仅模块名修改为provider_8889

4.服务消费者(consumer)

目录结构:

├─.mvn
│ └─wrapper
├─src
├─main
├─java
│ └─com
│ └─zjy
│ ├─config
│ └─controller
└─resources

4.1依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>consumer_8081</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer_8081</name>
    <description>consumer_8081</description>
    <parent>
        <artifactId>SpringCloud</artifactId>
        <groupId>com.zjy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
<!--负载均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>





        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.zjy.Consumer8081Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

4.2业务代码

Config类

实际上consumer的controller层(consumer作为客户端,当然没有service层)获取provider提供的对象需要用到restTemplate实例,而源代码中没有提供Bean,所以我们这里写一个Bean以便装配。

package com.zjy.config;

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  //我们这里选用ribbon在客户端均衡负载,这里提前加入了依赖并写好了注解
    				//现版本ribbon已经集成,不需要额外引入依赖
    public RestTemplate restTemplate(){
        return new RestTemplate();
    };

}

controller类

package com.zjy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Controller
public class ActorController {
    @Autowired
    private RestTemplate restTemplate;
    private static final String URL_PREFIX="http://localhost:8888";
    @RequestMapping("/list")
    public Object getAll(){
       return restTemplate.getForEntity(URL_PREFIX+"/list",Object.class);
    }
    @RequestMapping("/{id}")
    public Object getById(@PathVariable("id") Integer id){
        return restTemplate.getForEntity(URL_PREFIX+"/"+id.toString(),Object.class);
    }
    @RequestMapping("/get2") //这里完全是为了玩一下getForEntity方法中使用Map传参的重载
    public Object get(){
        Map map=new HashMap();
        map.put("id",2);
        return restTemplate.getForEntity(URL_PREFIX+"/{id}",Object.class,map);
    }

}

主程序

package com.zjy;

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


@SpringBootApplication
@EnableEurekaClient
//Eureka客户端使能
public class Consumer8081Application {

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

}

4.3配置

server:
  port: 8081

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #是否向服务器注册
    fetch-registry: true #是否向注册中心获取注册信息,作为客户端是需要获取的
    service-url:
      defaultZone: http://eureka9001.com:9001/eureka,http://eureka9002.com:9002/eureka,http://eureka9003.com:9003/eureka

5.Eureka服务端

目录结构

├─src
├─main
├─java
│ └─com.zjy.Eureka
└─resources

5.1依赖

<?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>SpringCloud</artifactId>
        <groupId>com.zjy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Eureka_9001</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.zjy.Eureka.EurekaApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

5.2启动类

package com.zjy.Eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class Eureka_9001 {

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

}

5.3配置

server:
  port: 9001
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #是否向服务器注册
    fetch-registry: false #是否向注册中心获取注册信息
    service-url:
      defaultZone: http://localhost:8888/eureka,http://eureka9002.com:9002/eureka,http://eureka9003.com:9003/eureka
    eureka-server-port: 9001
    prefer-ip-address: true

5.4其他两个Eureka服务实例

事实上建立模式与上文并无不同,为方便读者,将另外两个Eureka服务源代码贴出

9002端口:

依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>Eureka_9002</artifactId>

    <name>eureka</name>
    <description>eureka</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <parent>
        <artifactId>SpringCloud</artifactId>
        <groupId>com.zjy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.zjy.Eureka.EurekaApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

配置

server:
  port: 9002
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #是否向服务器注册
    fetch-registry: false #是否向注册中心获取注册信息
    service-url:
      defaultZone: http://localhost:8888/eureka,http://eureka9001.com:9001/eureka,http://eureka9003.com:9003/eureka
    eureka-server-port: 9002
    prefer-ip-address: true

启动类

package com.zjy.Eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class Eureka_9002 {

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

}

9003端口:

依赖

<?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>SpringCloud</artifactId>
        <groupId>com.zjy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Eureka_9003</artifactId>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.zjy.Eureka.EurekaApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

配置

server:
  port: 9003
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #是否向服务器注册
    fetch-registry: false #是否向注册中心获取注册信息
    service-url:
      defaultZone: http://localhost:8888/eureka,http://eureka9001.com:9001/eureka,http://eureka9002.com:9002/eureka
    eureka-server-port: 9003
    prefer-ip-address: true

启动类

package com.zjy.Eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class Eureka_9003 {

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

}

6.遇到的一些问题

1.UNKNOWHOSTEXCEPTION

之前建立工程后测试,三部分均正常,但consumer提示找不到host(unknownhost),provider也不能通过发现服务打印实例(instance)信息,怀疑是根本未生成实例。

检查配置未发现异常,注意到provider在Eureka注册的默认名称不是UNKNOWN而是DATA,配置spring.application.name后仍然只显示DATA。于是去检查DATA模块。

data模块下有一个provider目录(不晓得怎么引进去的,一开始还以为是依赖导入的)。将data模块中无用的springboot依赖和provider目录删除,业务运行正常。

2.关于数据传递

之前在provider和consumer间试图传递基本参数(int、String等),均报错。后传递map等键值对或Actor等实体类,成功。

推测是spring自带的数据传递都要封装成json,而JSON要求传递的是键值对而不是单一量。

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-09-21 00:12:55  更:2022-09-21 00:14:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 10:17:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码