Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Preliminary Construction method of SpringCloud Environment

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of the preliminary building methods of the SpringCloud environment, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the preliminary building methods of the SpringCloud environment. Let's take a look at it.

Micro service

Personal understanding:

The so-called micro-service means that each module can complete its own core business independently, and the collapse of one module does not affect the operation of other modules.

Each microservice has its own separate database

Each tiny service forms a complex software system.

Micro-service architecture

Micro-service refers to the size of the service, focusing on the completion of a specific service (hospital department)

Micro-service architecture is an architectural model, which advocates dividing a single application into a set of small services, which coordinate and interact with each other.

Cooperate to provide users with the final value. A method of developing a single application into a set of small services, each running on it

In the process. (whole hospital)

Advantages:

Release for specific services with low impact, low risk and low cost

Release versions frequently and deliver requirements quickly

Low-cost expansion, elastic scaling, adapting to cloud environment

Introduction to SpringCould

Springboot focuses on faster development of monolithic microservices, while SpringCloud focuses on global service governance

Spring Cloud is an ordered collection of a series of frameworks, which makes use of the development convenience of Spring Boot to skillfully simplify the development of distributed system infrastructure, such as service discovery registration, configuration center, message bus, load balancing, circuit breaker, data monitoring and so on. Spring Boot development style can be used to do one-click startup and deployment.

Spring does not repeat manufacturing wheels, it just combines the more mature service frameworks developed by various companies that can stand the actual test, and re-encapsulates the complex configuration and implementation principles through the Spring Boot style, and finally leaves a set of distributed system development kits for developers that are easy to understand, easy to deploy and easy to maintain.

Environment building

Create a new maven parent project in idea to configure the corresponding dependencies in its pom to manage the dependent versions of the subclasses

1.18.18 1.2.17 org.springframework.cloud spring-cloud-starter-netflix-eureka-client 3.0.2 org.springframework.cloud spring- Cloud-dependencies Hoxton.SR9 pom import org.springframework.boot spring-boot-dependencies 2.3.5.RELEASE pom import Mysql mysql-connector-java 8.0.22 com.alibaba druid 1.1.21 junit junit 4.13 test org.projectlombok lombok ${lombock.version} import log4j log4j ${log4j.version} 1. Create a database

Create a database called db01 with a table dept

two。 Create a new submodule api

There is only one entity class dept that corresponds to the table in our database

Public class Dept {private int deptnumber; private String dname; private String dumped source; / / get and set methods for corresponding properties} 3. Create a new submodule provider as the service provider

Import dependency

Org.example springcloud-api 1.0-SNAPSHOT mysql mysql-connector-java com.alibaba druid junit junit test org.springframework.boot spring-boot- Starter org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1

Configure configuration files and corresponding operation classes and interfaces

Application.xml

Server: port: 8081mybatis: type-aliases-package: pojo mapper-locations: classpath:Mybatis/mapper/*.xml config-location: classpath:Mybatis/mybatis-config.xmlspring: application: name: provider-name datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: jdbc:mysql://localhost:3306/db01?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8

Mapper-config.xml and Deptmapper.xml

Insert into dept (dname, d_source) values (# {dname}, DATABASE ()); select * from dept where deptnumber = # {id}; select * from dept

Corresponding Dao,Controller,Service

@ Mapperpublic interface DeptDao {boolean add (Dept dept); Dept queryByID (long id); List queryAll ();} @ Servicepublic class DeptImpl implements DeptService {@ Resource private DeptDao deptDao; public boolean add (Dept dept) {return deptDao.add (dept);} public Dept queryByID (long id) {System.out.println ("deptimpl" + id); return deptDao.queryByID (id) } public List queryAll () {return deptDao.queryAll ();} @ RestControllerpublic class DeptController {@ Autowired private DeptImpl deptimpl; @ PostMapping ("/ dev/add") public boolean addDept (@ RequestBody Dept dept) {System.out.println (dept); return deptimpl.add (dept) } @ GetMapping ("/ dev/ {id}") public Dept DeptqueryByID (@ PathVariable ("id") long id) {System.out.println ("deptcontroller" + id); return deptimpl.queryByID (id);} @ PostMapping ("/ dev/list") public List DeptqueryAll () {return deptimpl.queryAll ();}

Startup class

@ SpringBootApplicationpublic class SApplication {public static void main (String [] args) {SpringApplication.run (SApplication.class,args);}} New submodule consumer as a consumer

Import dependency

Org.example springcloud-api 1.0-SNAPSHOT org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web

Configuration file

Server: port: 80

Config class and controller

/ * RestTemplate is a HTTP request tool supported from Spring3.0. It provides templates for common REST request scenarios, such as GET request, POST request, PUT request, DELETE request and some general request execution methods exchange and execute. * / @ Configurationpublic class ConfigBean {@ Bean public RestTemplate getRestTemplate () {return new RestTemplate ();}} @ RestControllerpublic class ConsumerController {@ Autowired private RestTemplate template; private static final String url= "http://localhost:8001"; @ RequestMapping ("/ consumer/get/ {id}") public Dept getByID (@ PathVariable long id) {/ / the path of the request, the object returned by the request is Dept getEntity = template.getForObject (url + "/ dev/" + id, Dept.class); return getEntity } @ RequestMapping ("/ consumer/add") public boolean add (String dname) {Dept dept = new Dept (); dept.setDname (dname); System.out.println (dept); / / the path of the request, the parameter passed, the returned object return template.postForObject (url+ "/ dev/add", dept,Boolean.class) } @ RequestMapping ("/ consumer/list") public List list () {/ / path of the request, returned object return template.postForObject (url+ "/ dev/list", void.class,List.class);}}

Startup class and running result

Debug-connect refuserd---debug- passes parameters to null

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

Debug

Child projects cannot use dependent packages of parent projects

1 if the parent project pom uses

....

Method, the child project pom will not automatically use the jar package in the parent pom. If you need to use it, you need to give groupId and artifactId instead of version.

It is used to manage version information uniformly

When using in a sub-project, you still need to introduce coordinates, but you don't need to give version.

In the POM file at the top level of our project, the element.

Manage the version of the jar package through its element

Let the subproject reference a dependent column that does not need to be displayed.

Maven will look up the parent-son level.

Until you find a project with a dependencyManagement element

It then uses the version number specified in the dependencyManagement element.

Required a bean of type 'DeptDao' that could not be found.

Use @ Mapper instead of @ Repository

1. After using @ mapper, you do not need to set the scan address in the spring configuration. Through the namespace attribute in mapper.xml corresponding to the relevant mapper class, spring will dynamically generate Bean and inject it into ServiceImpl.

2. @ repository needs to configure the scan packet address in the Spring, then generate the bean of the dao layer, and then be injected into the ServiceImpl

Connection refused connect

Check the URL port number, whether it is written correctly, and whether it is occupied.

RestTemplate delivery object cannot be received (null)

Add @ RequestBody to the corresponding parameters on the server side

@ PostMapping ("/ dev/add") public boolean addDept (@ RequestBody Dept dept) {System.out.println (dept); return deptimpl.add (dept);} this is the end of the article on "preliminary ways to build a SpringCloud environment". Thank you for reading! I believe you all have a certain understanding of the "preliminary method of building a SpringCloud environment". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report