提交 ea47eff3 编写于 作者: Q qinyingjie

fix:ribbon负载均衡调用

上级 d7abb067
......@@ -25,7 +25,9 @@
</properties>
<modules>
<module>springcloud-service-goods</module>
<module>springcloud-service-goods-9100</module>
<module>springcloud-service-goods-9200</module>
<module>springcloud-service-goods-9300</module>
<module>springcloud-service-common</module>
<module>springcloud-service-portal</module>
<module>springcloud-service-eureka</module>
......
......@@ -7,9 +7,9 @@
<artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>springcloud-service-goods</artifactId>
<artifactId>springcloud-service-goods-9100</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-goods</name>
<name>springcloud-service-goods-9100</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
......@@ -48,11 +48,11 @@
<version>8.0.11</version>
</dependency>
<!--扩展包 -->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-extension</artifactId>-->
<!-- <version>3.5.2</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-extension</artifactId>-->
<!-- <version>3.5.2</version>-->
<!-- </dependency>-->
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
......
......@@ -6,10 +6,10 @@ import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class GoodsApplication {
public class GoodsApplication9100 {
public static void main(String[] args) {
SpringApplication.run(GoodsApplication.class, args);
SpringApplication.run(GoodsApplication9100.class, args);
}
}
......@@ -18,6 +18,11 @@ eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
instance:
lease-renewal-interval-in-seconds: 2 #每间隔2s,向服务端发送一次心跳,证明自己依然"存活”
lease-expiration-duration-in-seconds: 10 #告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我踢出掉
prefer-ip-address: true #告诉服务端,服务实例以IP作为链接,而不是取机器名
instance-id: springcloud-service-goods-9100 #告诉服务端,服务实例的id,id要是唯一的
management:
endpoints:
......
<?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>
<parent>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>springcloud-service-goods-9200</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-goods-9200</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-common</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</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>
<scope>test</scope>
</dependency>
<!--mybatis-plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--扩展包 -->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-extension</artifactId>-->
<!-- <version>3.5.2</version>-->
<!-- </dependency>-->
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.kwan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class GoodsApplication9200 {
public static void main(String[] args) {
SpringApplication.run(GoodsApplication9200.class, args);
}
}
package com.kwan.springcloud.controller;
import com.alibaba.fastjson.JSONArray;
import com.kwan.springcloud.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
public class HelloController {
@Autowired
private DiscoveryClient client;
@Autowired
private Registration registration;
/**
* produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE
* 解决不是json显示问题
*/
@RequestMapping(value = "/hello", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result index() {
List<ServiceInstance> instances = client.getInstances(registration.getServiceId());
log.info(JSONArray.toJSONString(instances));
return Result.ok(instances);
}
}
package com.kwan.springcloud.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwan.springcloud.entity.KwanGoodsInfo;
import com.kwan.springcloud.response.Result;
import com.kwan.springcloud.service.KwanGoodsInfoService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
@RestController
@RequestMapping("kwanGoodsInfo")
public class KwanGoodsInfoController {
/**
* 服务对象
*/
@Resource
private KwanGoodsInfoService kwanGoodsInfoService;
/**
* 分页查询所有数据
*
* @param page 分页对象
* @param kwanGoodsInfo 查询实体
* @return 所有数据
*/
@GetMapping
public Result selectAll(Page<KwanGoodsInfo> page, KwanGoodsInfo kwanGoodsInfo) {
return Result.ok(this.kwanGoodsInfoService.page(page, new QueryWrapper<>(kwanGoodsInfo)));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping(value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result selectOne(@PathVariable Serializable id) {
return Result.ok(this.kwanGoodsInfoService.getById(id));
}
/**
* 新增数据
*
* @param kwanGoodsInfo 实体对象
* @return 新增结果
*/
@PostMapping
public Result insert(@RequestBody KwanGoodsInfo kwanGoodsInfo) {
return Result.ok(this.kwanGoodsInfoService.save(kwanGoodsInfo));
}
/**
* 修改数据
*
* @param kwanGoodsInfo 实体对象
* @return 修改结果
*/
@PutMapping
public Result update(@RequestBody KwanGoodsInfo kwanGoodsInfo) {
return Result.ok(this.kwanGoodsInfoService.updateById(kwanGoodsInfo));
}
/**
* 删除数据
*
* @param idList 主键结合
* @return 删除结果
*/
@DeleteMapping
public Result delete(@RequestParam("idList") List<Long> idList) {
return Result.ok(this.kwanGoodsInfoService.removeByIds(idList));
}
}
package com.kwan.springcloud.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kwan.springcloud.entity.KwanGoodsInfo;
import org.apache.ibatis.annotations.Mapper;
/**
* (KwanGoodsInfo)表数据库访问层
*
* @author makejava
* @since 2023-01-08 10:21:28
*/
@Mapper
public interface KwanGoodsInfoDao extends BaseMapper<KwanGoodsInfo> {
}
package com.kwan.springcloud.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
/**
* (KwanGoodsInfo)表实体类
*
* @author makejava
* @since 2023-01-08 10:21:28
*/
public class KwanGoodsInfo extends Model<KwanGoodsInfo> {
private Integer id;
//商品名称
private String goodsName;
//商品价格
private Integer goodsPrice;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Integer getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(Integer goodsPrice) {
this.goodsPrice = goodsPrice;
}
/**
* 获取主键值
*
* @return 主键值
*/
@Override
public Serializable pkVal() {
return this.id;
}
}
package com.kwan.springcloud.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.kwan.springcloud.entity.KwanGoodsInfo;
/**
* (KwanGoodsInfo)表服务接口
*
* @author makejava
* @since 2023-01-08 10:21:29
*/
public interface KwanGoodsInfoService extends IService<KwanGoodsInfo> {
}
package com.kwan.springcloud.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springcloud.dao.KwanGoodsInfoDao;
import com.kwan.springcloud.entity.KwanGoodsInfo;
import com.kwan.springcloud.service.KwanGoodsInfoService;
import org.springframework.stereotype.Service;
/**
* (KwanGoodsInfo)表服务实现类
*
* @author makejava
* @since 2023-01-08 10:21:30
*/
@Service("kwanGoodsInfoService")
public class KwanGoodsInfoServiceImpl extends ServiceImpl<KwanGoodsInfoDao, KwanGoodsInfo> implements KwanGoodsInfoService {
}
#端口号
server:
port: 9200
#兼容swagger配置
spring:
# mysql
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 716288qwe
application:
name: goods-service #服务名称
#服务提供者
eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
instance:
lease-renewal-interval-in-seconds: 2 #每间隔2s,向服务端发送一次心跳,证明自己依然"存活”
lease-expiration-duration-in-seconds: 10 #告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我踢出掉
prefer-ip-address: true #告诉服务端,服务实例以IP作为链接,而不是取机器名
instance-id: springcloud-service-goods-9200 #告诉服务端,服务实例的id,id要是唯一的
management:
endpoints:
web:
base-path: /actuator #修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改
health:
show-details: always #显示健康具体信息 默认不会显示详细信息
#mybatis-plus配置
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
call-setters-on-nulls: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
banner: false
# 逻辑删除配置
db-config:
id-type: AUTO
logic-delete-field: delFlag
logic-delete-value: 1
logic-not-delete-value: 0
table-underline: true
\ No newline at end of file
package com.kwan.springcloud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class GoodsApplicationTests {
@Test
void contextLoads() {
}
}
<?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>
<parent>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>springcloud-service-goods-9300</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-goods-9300</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-common</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</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>
<scope>test</scope>
</dependency>
<!--mybatis-plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--扩展包 -->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-extension</artifactId>-->
<!-- <version>3.5.2</version>-->
<!-- </dependency>-->
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.kwan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class GoodsApplication9300 {
public static void main(String[] args) {
SpringApplication.run(GoodsApplication9300.class, args);
}
}
package com.kwan.springcloud.controller;
import com.alibaba.fastjson.JSONArray;
import com.kwan.springcloud.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
public class HelloController {
@Autowired
private DiscoveryClient client;
@Autowired
private Registration registration;
/**
* produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE
* 解决不是json显示问题
*/
@RequestMapping(value = "/hello", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result index() {
List<ServiceInstance> instances = client.getInstances(registration.getServiceId());
log.info(JSONArray.toJSONString(instances));
return Result.ok(instances);
}
}
package com.kwan.springcloud.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwan.springcloud.entity.KwanGoodsInfo;
import com.kwan.springcloud.response.Result;
import com.kwan.springcloud.service.KwanGoodsInfoService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
@RestController
@RequestMapping("kwanGoodsInfo")
public class KwanGoodsInfoController {
/**
* 服务对象
*/
@Resource
private KwanGoodsInfoService kwanGoodsInfoService;
/**
* 分页查询所有数据
*
* @param page 分页对象
* @param kwanGoodsInfo 查询实体
* @return 所有数据
*/
@GetMapping
public Result selectAll(Page<KwanGoodsInfo> page, KwanGoodsInfo kwanGoodsInfo) {
return Result.ok(this.kwanGoodsInfoService.page(page, new QueryWrapper<>(kwanGoodsInfo)));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping(value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result selectOne(@PathVariable Serializable id) {
return Result.ok(this.kwanGoodsInfoService.getById(id));
}
/**
* 新增数据
*
* @param kwanGoodsInfo 实体对象
* @return 新增结果
*/
@PostMapping
public Result insert(@RequestBody KwanGoodsInfo kwanGoodsInfo) {
return Result.ok(this.kwanGoodsInfoService.save(kwanGoodsInfo));
}
/**
* 修改数据
*
* @param kwanGoodsInfo 实体对象
* @return 修改结果
*/
@PutMapping
public Result update(@RequestBody KwanGoodsInfo kwanGoodsInfo) {
return Result.ok(this.kwanGoodsInfoService.updateById(kwanGoodsInfo));
}
/**
* 删除数据
*
* @param idList 主键结合
* @return 删除结果
*/
@DeleteMapping
public Result delete(@RequestParam("idList") List<Long> idList) {
return Result.ok(this.kwanGoodsInfoService.removeByIds(idList));
}
}
package com.kwan.springcloud.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kwan.springcloud.entity.KwanGoodsInfo;
import org.apache.ibatis.annotations.Mapper;
/**
* (KwanGoodsInfo)表数据库访问层
*
* @author makejava
* @since 2023-01-08 10:21:28
*/
@Mapper
public interface KwanGoodsInfoDao extends BaseMapper<KwanGoodsInfo> {
}
package com.kwan.springcloud.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
/**
* (KwanGoodsInfo)表实体类
*
* @author makejava
* @since 2023-01-08 10:21:28
*/
public class KwanGoodsInfo extends Model<KwanGoodsInfo> {
private Integer id;
//商品名称
private String goodsName;
//商品价格
private Integer goodsPrice;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Integer getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(Integer goodsPrice) {
this.goodsPrice = goodsPrice;
}
/**
* 获取主键值
*
* @return 主键值
*/
@Override
public Serializable pkVal() {
return this.id;
}
}
package com.kwan.springcloud.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.kwan.springcloud.entity.KwanGoodsInfo;
/**
* (KwanGoodsInfo)表服务接口
*
* @author makejava
* @since 2023-01-08 10:21:29
*/
public interface KwanGoodsInfoService extends IService<KwanGoodsInfo> {
}
package com.kwan.springcloud.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springcloud.dao.KwanGoodsInfoDao;
import com.kwan.springcloud.entity.KwanGoodsInfo;
import com.kwan.springcloud.service.KwanGoodsInfoService;
import org.springframework.stereotype.Service;
/**
* (KwanGoodsInfo)表服务实现类
*
* @author makejava
* @since 2023-01-08 10:21:30
*/
@Service("kwanGoodsInfoService")
public class KwanGoodsInfoServiceImpl extends ServiceImpl<KwanGoodsInfoDao, KwanGoodsInfo> implements KwanGoodsInfoService {
}
#端口号
server:
port: 9300
#兼容swagger配置
spring:
# mysql
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/kwan?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 716288qwe
application:
name: goods-service #服务名称
#服务提供者
eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
instance:
lease-renewal-interval-in-seconds: 2 #每间隔2s,向服务端发送一次心跳,证明自己依然"存活”
lease-expiration-duration-in-seconds: 10 #告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我踢出掉
prefer-ip-address: true #告诉服务端,服务实例以IP作为链接,而不是取机器名
instance-id: springcloud-service-goods-9300 #告诉服务端,服务实例的id,id要是唯一的
management:
endpoints:
web:
base-path: /actuator #修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改
health:
show-details: always #显示健康具体信息 默认不会显示详细信息
#mybatis-plus配置
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
call-setters-on-nulls: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
banner: false
# 逻辑删除配置
db-config:
id-type: AUTO
logic-delete-field: delFlag
logic-delete-value: 1
logic-not-delete-value: 0
table-underline: true
\ No newline at end of file
package com.kwan.springcloud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class GoodsApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册