preface
Hello everyone, tomorrow is National Day, I will post another blog today.
In fact, this blog is linked to a blog.
Eureka + Swagger +mybatisPlus: juejin.cn/post/684490…
Therefore, this blog post is developed from the project created in the last blog post. Please build all the projects in the last blog post before building this project.
content
The list of projects from my last post:
The project directory after the successful construction of this blog post:
Therefore, the following contents should be noted, and I will post them one by one for you.
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. The blog < / groupId > < artifactId > permission - service < / artifactId > The < version > 0.0.1 permissionService - the SNAPSHOT < / version > < packaging > jar < / packaging > < name > permission - service < / name > <description>Demo projectforSpring Boot</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.5.1. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! <dependency> <groupId>org.mybatis.spring. Boot </groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 1.2.0 < / version > < / dependency > <! <dependency> <groupId>com.baomidou</groupId> < artifactId > mybatisplus - spring - the boot - starter < / artifactId > < version > 1.0.5 < / version > < / dependency > < the dependency > <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.1.8</version> </dependency> <! <dependency> <groupId>org.apache.velocity</groupId> <artifactId> The < version > 1.7 < / version > < / dependency > <! --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> The < version > 5.1.6 < / version > < / dependency > <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! --Herbnate entity dependency --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence- API </artifactId> The < version > 1.0.2 < / version > < / dependency > <! -- Common base Jar packages, Commons -pool2</artifactId> Commons --> <dependency> <groupId>org.apache.commons </dependency> <! Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId> Lombok </artifactId> <scope>provided</scope> </dependency> <! -- Spring uses yML configuration by default, But sometimes use the traditional XML or properties configuration --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <! Alibaba </groupId> <artifactId>fastjson</artifactId> The < version > 1.1.41 < / version > < / dependency > <! -- JWT authentication protocol dependencies, Jsonwebtoken </groupId> <artifactId> JJWT </artifactId> <version>0.9.0</version> </dependency> <! Eureka to Swagger --> <! --<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>--> <! -- These three dependencies, Is for the sake of serialized object into json - > < the dependency > < groupId > com. Fasterxml. Jackson. Core < / groupId > < artifactId > Jackson - databind < / artifactId > </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <! - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -- > <! - springCloud start relying on - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter</artifactId> </dependency> <! - srpingCloud eureka, service registry depend on the client side - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - netflix - eureka - client < / artifactId > < version >. 2.0.0 RELEASE < / version > < / dependency > <! SrpingCloud's Eureka service registers its dependency on the server side because the service acts as both a producer, And to act as consumers - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - netflix - eureka - server < / artifactId > < version >. 2.0.0 RELEASE < / version > < / dependency > <! - springCloud fiegn depend on - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <! Com.net flix. Ribbon </groupId> <artifactId>ribbon</artifactId> The < version > 2.2.2 < / version > < / dependency > <! - histrix dependence - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <! Hystrix </groupId> <artifactId>hystrix-javanica</artifactId> </artifactId> </dependency> <! -- New dependencies, swaggerUI--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> < version > 2.7.0 < / version > < / dependency > < the dependency > < groupId >. IO springfox < / groupId > <artifactId> Springfox-Swagger - UI </artifactId> <version>2.7.0</version> </dependency> </dependencies> </dependencies> <! -- Introduce spring Cloud dependencies, not less, <dependency management > <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <! <directory> SRC /main/resources</directory> <! --> <includes> <include>**/. XML </include> <include>**/. Properties </include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering>
</resource>
</resources>
<finalName>permission</finalName>
</build>
</project>
Copy the code
application.properties
# service name
spring.application.name=permission
# port
server.port=3333
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Database informationspring.datasource.url: jdbc:mysql://localhost:3306/myblog? useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username: root
spring.datasource.password: 123456
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
spring.jmx.default-domain=${spring.application.name}
spring.jpa.open-in-view=false
# SpringFramework. web logs are output at DEBUG level
logging.level.org.springframework.web=info
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Classpath :/com/blog/*/mapper/* mapper.xml
Mapper.xml :/ mapper.xml :/ mapper.xml
mybatis-plus.mapper-locations=classpath:/mapper/*Mapper.xml
# Entity scan, separate multiple packages with commas or semicolons
mybatis-plus.type-aliases-package=com.blog.*.entity
# Hump underline conversion
mybatis-plus.global-config.db-column-underline=true
# Refresh mapper debugger
mybatis-plus.global-config.refresh-mapper=false
# database uppercase underline conversion
mybatis-plus.global-config.capital-mode=true
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Configure Swagger information (from Eureka to Swagger page)
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=:${spring.cloud.client.ipAddress}
eureka.instance.status-page-url:http://${spring.cloud.client.ipAddress}:${server.port}/swagger-ui.html
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Eureka registry path
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
# indicates the heartbeat time that the service sends to the Eureka server. Do not set the heartbeat time too long; otherwise, Eureka will fail to detect the service and drive the service offline
eureka.instance.leaseRenewalIntervalInSeconds: 2
The eureka server removes the cache of the service when the service is offline
eureka.instance.leaseExpirationDurationInSeconds: 4
The name of the service registered on Eureka
eureka.instance.instance-id=http://${spring.cloud.client.ipAddress}:${server.port}/${spring.application.name}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Logical delete configuration (the following 3 configurations), not real delete data, just logical delete data
mybatis-plus.global-config.logic-delete-value=1
mybatis-plus.global-config.logic-not-delete-value=0
mybatis-plus.global-config.sql-injector=com.baomidou.mybatisplus.mapper.LogicSqlInjector
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
mybatis-plus.global-config.meta-object-handler=com.blog.permission.mabatisPlus.MyMetaObjectHandler
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
mybatis-plus.configuration.jdbc-type-for-null=null
mybatis-plus.typeAliasesPackage=com.blog.permission.entity
mybatis-plus.global-config.id-type=3
mybatis-plus.global-config.field-strategy=2
mybatis-plus.global-config.key-generator=com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Default timeout setting
hystrix.metrics.polling-interval-ms=60000
# start Hystrix
hystrix.metrics.enabled=true
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Place the logs in the permission.log file of the logs folder within the project
logging.file=logs/permission.log
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Copy the code
PermissionApplication.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.swagger2.annotations.EnableSwagger2; @enableFeignClients // Feign Annotation @ComponentScan(basePackages ="com.blog.permission") @enableAsync // Support multi-threading @enablesWagger2 // Swagger annotation @enableWebMVC // with WebMvcConfig config file, The purpose is to use webMVC @SpringBootApplication @EnableEurekaclient // to register an annotation on Eureka @enablecircuitbreaker //Hystrix fuse annotation public class PermissionApplication { public static void main(String[] args) { SpringApplication.run(PermissionApplication.class, args);; }}Copy the code
Next, add an interface to test Feign and Hystrix:
The controller layer
@ApiOperation(value = "Testing feign's interface")
@PostMapping("/testForFeign")
public Result testForFeign(@ApiParam(value = "User id")@RequestParam(required = false) String userId){
return userService.testForFeign(userId);
}
Copy the code
The service layer
@Override
public Result testForFeign(String userId) {
Result result=perimissionFallback.get(userId);
return result;
}
Copy the code
Feature: perimissionFallback. Java
import com.blog.permission.feign.PermissionClient;
import com.blog.permission.method.Result;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class PerimissionFallback {
@Resource
private PermissionClient permissionClient;
private static final Logger logger = LoggerFactory.getLogger(PerimissionFallback.class);
@HystrixCommand(fallbackMethod = "getFallBack")
public Result get(String userId) {
Result result=permissionClient.get(userId);
return result;
}
public Result getFallBack(String userId) {
logger.info("Calling interface: Permission /login/get failed, degraded processing");
return new Result().fallBack("permission/login/get",userId); }}Copy the code
HystrixCommand(fallbackMethod = “getFallBack”). This is the Hystrix method demote. The demote method is getFallBack.
Feature: PermissionClient. Java
import com.blog.permission.method.Result;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
@Component
@FeignClient(value = "permission") public interface PermissionClient {// This is the "permission/login/get" interface of the service @requestMapping (value ="/login/get", method = RequestMethod.GET)
public Result get(@RequestParam("userId") String userId);
}
Copy the code
The cross-service method that you want to call looks like this:
Ok, now you can start the service.
SpringCloud Framework (A) – Eureka server: juejin.cn/post/684490…
Click: http://192.168.2.92:3333/permission, enter the swagger page.
Invoke the interface to start the test:
Next, we started testing Hystrix’s circuit breaker. The service layer break point in interface: login/get
Now that we’ve covered Feign and Hystrix, where is the Ribbon? When you start using Feign, springCloud uses the Ribbon by default to distribute Feign requests as long as your dependencies have the Ribbon in them, so we don’t have to worry about that.
conclusion
This construction process, in fact, is quite tortuous, the reason is that some technical blogs on the Internet, may be due to springboot and springCloud rely on different versions, in fact, can not give me help, so many configuration aspects, I need to see the official documents, this is more tired.
Finally, I hope to help you reading this article.