fix:新增服务实例

上级 7d7e0a7f
package com.kwan.springcloudalibaba;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
......@@ -22,7 +24,13 @@ public class NacosConsumerApplication {
return new RestTemplate();
}
@Bean
public IRule randomRule() {
return new RandomRule(); //随机
//return new RoundRobinRule(); //轮训
//return new BestAvailableRule();
//return new ZoneAvoidanceRule(); //新版本默认的策略
}
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
......
......@@ -50,6 +50,7 @@ public class ConsumerUserController {
return Result.ok(restTemplate.getForObject(url, Result.class));
}
/**
* 使用ip访问
* http://127.0.0.1:8085/nacos/consumer/loadBalancerClientIp/1
......@@ -67,6 +68,22 @@ public class ConsumerUserController {
return Result.ok(restTemplate.getForObject(url, Result.class));
}
/**
* 通过服务名进行负载均衡访问
* http://127.0.0.1:8085/nacos/consumer/serverName/1
*
* @param id
* @return
*/
@GetMapping(value = "/serverName/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result serverName(@PathVariable("id") Integer id) {
String url = "http://nacos-server-producer/user/" + id;
System.out.println("request url:" + url);
return Result.ok(restTemplate.getForObject(url, Result.class));
}
/**
* feign调用
* http://127.0.0.1:8085/nacos/consumer/1
......
......@@ -19,9 +19,9 @@
<artifactId>nacos-server-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>nacos-server-producer</artifactId>
<name>nacos-server-producer</name>
<description>nacos-server-producer</description>
<artifactId>nacos-server-producer-9091</artifactId>
<name>nacos-server-producer-9091</name>
<description>nacos-server-producer-9091</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......
......@@ -13,10 +13,10 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
* @date : 2023/1/17 12:37
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class NacosProducerApplication {
public class NacosProducer9091Application {
public static void main(String[] args) {
SpringApplication.run(NacosProducerApplication.class, args);
System.out.println("-----------------------nacos-server-producer启动成功-----------------------");
SpringApplication.run(NacosProducer9091Application.class, args);
System.out.println("-----------------------nacos-server-producer-9091启动成功-----------------------");
}
}
<?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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.kwan.springcloudalibaba</groupId>
<artifactId>nacos-server-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>nacos-server-producer-9092</artifactId>
<name>nacos-server-producer-9092</name>
<description>nacos-server-producer-9092</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-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>com.kwan.springcloudalibaba</groupId>
<artifactId>nacos-server-common</artifactId>
<version>1.0.0</version>
</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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.kwan.springcloudalibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* http://localhost:18082/nacos/nameInfo
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/17 12:37
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class NacosProducer9092Application {
public static void main(String[] args) {
SpringApplication.run(NacosProducer9092Application.class, args);
System.out.println("-----------------------nacos-server-producer-9092启动成功-----------------------");
}
}
package com.kwan.springcloudalibaba.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mybatisplus配置
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/19 16:11
*/
@MapperScan("com.kwan.springcloudalibaba.mapper")//扫描我们的mapper文件夹 这里添加了@MapperScan注解就不需要Springboot启动类上加入@MapperScan注解了
@Configuration //配置类
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//注册乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.common.Result;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
@RequestMapping("/nacos/producer")
public class NacosProducerController {
/**
* 获取配置的变量
*/
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() {
return Result.ok();
}
}
package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.common.Result;
import com.kwan.springcloudalibaba.entity.User;
import com.kwan.springcloudalibaba.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用户相关
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/19 16:08
*/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
/**
* 通过主键查询单条数据
*
* http://localhost:9091/user/1
*
* @param id 主键
* @return 单条数据
*/
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result selectOne(@PathVariable Integer id) {
return Result.ok(userService.getUserById(id));
}
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<User> addAdvertise() {
log.info("测试日志={}", "success");
return userService.getUsers();
}
@RequestMapping(value = "/getUserByName", method = RequestMethod.GET)
public User getUserByName(@RequestParam String sex) {
return userService.getUserByName(sex);
}
}
\ No newline at end of file
package com.kwan.springcloudalibaba.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@TableName(value = "user")
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private String sex;
}
package com.kwan.springcloudalibaba.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kwan.springcloudalibaba.entity.User;
import org.apache.ibatis.annotations.Mapper;
/**
* mapper文件
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/19 16:12
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
User getUserByName(String sex);
}
package com.kwan.springcloudalibaba.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.kwan.springcloudalibaba.entity.User;
import java.util.List;
public interface IUserService extends IService<User> {
/**
* 获取所有用户
*
* @return
*/
List<User> getUsers();
/**
* 获取一个用户
*
* @return
*/
User getUserById(Integer id);
/**
* 根据姓名查询
*
* @param sex
* @return
*/
User getUserByName(String sex);
}
package com.kwan.springcloudalibaba.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springcloudalibaba.entity.User;
import com.kwan.springcloudalibaba.mapper.UserMapper;
import com.kwan.springcloudalibaba.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUsers() {
return userMapper.selectList(null);
}
@Override
public User getUserById(Integer id) {
return userMapper.selectById(id);
}
@Override
public User getUserByName(String sex) {
User userByName = userMapper.getUserByName(sex);
if (Objects.isNull(userByName)) {
userByName = new User();
}
return userByName;
}
}
\ No newline at end of file
#端口号
server:
port: 9092
#spring配置
spring:
application:
name: nacos-server-producer
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: http://120.79.36.53:8848 #服务注册地址
config:
server-addr: http://120.79.36.53:8848 #配置中心地址
file-extension: yaml #文件类型
group: DEV_GROUP #组别
namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间
refresh-enabled: true #默认自动刷新
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<!-- 此xml在spring-boot-1.5.3.RELEASE.jar里 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<!--控制台打印日志,不需要配置appender了-->
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<!-- 开启后可以通过jmx动态控制日志级别(springboot Admin的功能) -->
<!--<jmxConfigurator/>-->
<!--总日志-->
<appender name="ALL-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--日志存放路径-->
<file>../logs/springboot-mybatis.log</file>
<encoder>
<!-- pattern节点,用来设置日志的输入格式 -->
<pattern>%date [%level] [%thread] %logger{60} [%file : %line] %msg%n</pattern>
<!--文件编码格式-->
<charset>UTF-8</charset>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 添加.gz 历史日志会启用压缩 大大缩小日志文件所占空间 -->
<fileNamePattern>../logs/springboot-mybatis.log.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 保留30天日志 -->
<maxHistory>30</maxHistory>
<!--文件最大存储-->
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
</appender>
<!--错误日志-->
<appender name="ERROR-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--日志存放路径-->
<file>../logs/error.log</file>
<encoder>
<pattern>%date [%level] [%thread] %logger{60} [%file : %line] %msg%n</pattern>
<!--文件编码格式-->
<charset>UTF-8</charset>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 添加.gz 历史日志会启用压缩 大大缩小日志文件所占空间 -->
<fileNamePattern>../logs/error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 保留30天日志 -->
<maxHistory>30</maxHistory>
<!--文件最大存储-->
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<!-- 过滤日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<!-- 用于配置符合过滤条件的操作 -->
<onMatch>ACCEPT</onMatch>
<!-- 用于配置不符合过滤条件的操作 -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="com.kwan.springbootkwan" level="INFO"/>
<root level="INFO">
<!--控制台打印日志-->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ALL-FILE"/>
<appender-ref ref="ERROR-FILE"/>
</root>
</configuration>
\ No newline at end of file
<?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.kwan.springcloudalibaba.mapper.UserMapper">
<select id="getUserByName" resultType="com.kwan.springcloudalibaba.entity.User">
SELECT *
FROM user
WHERE sex = #{sex}
</select>
</mapper>
package com.kwan.springcloudalibaba.springcloudalibabaservernacos;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class NacosApplicationTests {
@Test
void contextLoads() {
}
}
......@@ -21,7 +21,8 @@
<modules>
<module>nacos-server-common</module>
<module>nacos-server-producer</module>
<module>nacos-server-producer-9091</module>
<module>nacos-server-producer-9092</module>
<module>nacos-server-consumer</module>
<module>nacos-feign-api</module>
<module>nacos-server-sentinel-consumer</module>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册