In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "the case study of SpringCloud based on RestTemplate micro service project". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the case study of SpringCloud based on RestTemplateMicro Service Project.
Based on RestTemplateMicro Service Project
Before writing SpringCloud to build microservices, I want to build a module that communicates with each other not through springcloud but through SpringBoot and Mybatis. Then add the SpringCloud framework on this basis.
Let's start with an explanation of the case.
The project has a maven parent module with three child modules:
Serverspringcloud: whole parent project.
Serverspringcloud-api: a common submodule that places common entity objects.
Serverspringcloud-provider-dept-8001: departmental micro service provider.
Serverspringcloud-consumer-dept-80: departmental micro-service consumers. Call part of the micro service provider interface for CRUD operation.
First, build the parent project
Main steps:
(1) create a Maven parent project and name serverspringcloud
(2) the packing method is POM.
(3) define the version number of each dependency in pom.xml (if the pom.xml dependency in Module does not specify a version number, the dependency will be added according to the version number of the parent project)
1. Create a Maven parent project
2. The packing method is POM.
3. Define each dependent version number in pom.xml
4.0.0 com.jincou.springcloudrest serverspringcloud 0.0.1-SNAPSHOT pom UTF-8 1.8 1.8 4.12 1.2.17 1.16.18 org.springframework.boot spring-boot-dependencies 1.5.9.RELEASE Pom import mysql mysql-connector-java 5.0.4 com.alibaba druid 1.0.31 org.mybatis.spring .boot mybatis-spring-boot-starter 1.3.0 ch.qos.logback logback-core 1.2.3 junit junit ${junit.version} Test log4j log4j ${log4j.version} serverspringcloud src/main/resources true Org.apache.maven.plugins maven-resources-plugin $serverspringcloud-api serverspringcloud-provider-dept-8001 serverspringcloud-consumer-dept-80 pom.xml II. Build serverspringcloud-api (common submodule)
Main steps
(1) create a new Module for Maven under the parent project, and the packaging method is jar.
(2) add other needed dependencies to the pom.xml under the Module
(3) clean the Maven project after completion, and then provide install to other modules to call
1. Create a new Module for Maven under the parent project, and the packaging method is jar.
After creating the submodule, take a look at some information about the Overview view of pom.xml.
2. Add other needed dependencies to the pom.xml under the Module
4.0.0 com.jincou.springcloudrest serverspringcloud 0.0.1-SNAPSHOT serverspringcloud-api
Pom.xml3, I added a Dept entity to this
Dept entity
Package com.jincou.springcloud.entities;import java.io.Serializable;public class Dept implements Serializable {private Long deptno; / / primary key private String dname; / / department name private String db_source;// comes from that database, because the microservice architecture can have a service corresponding to a database, and the same information is stored in a different database public Dept (String dname) {super () This.dname = dname;} public Dept (Long deptno, String dname, String db_source) {super (); this.deptno = deptno; this.dname = dname; this.db_source = db_source;} public Dept () {super ();} public Long getDeptno () {return deptno } public void setDeptno (Long deptno) {this.deptno = deptno;} public String getDname () {return dname;} public void setDname (String dname) {this.dname = dname;} public String getDb_source () {return db_source;} public void setDb_source (String db_source) {this.db_source = db_source Third, create a departmental micro service provider
Step: this is more complicated, as shown in the figure below
Here are some of the more important links
1. Pom.xml file
(1) first take a look at some information in the Overview view of pom.xml.
(2) pom.xml
4.0.0 com.jincou.springcloudrest serverspringcloud 0.0.1-SNAPSHOT serverspringcloud-provider-dept-8001 com.jincou.springcloudrest serverspringcloud-api ${project.version} junit junit mysql mysql-connector-java Com.alibaba druid ch.qos.logback logback-core org.mybatis.spring.boot mybatis-spring-boot-starter org.springframework.boot spring-boot-starter-jetty org.springframework.boot Spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework springloaded org.springframework.boot spring-boot-devtools
2 、 application.yml
Server: port: 8001 # Port number mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis configuration file path type-aliases-package: com.jincou.springcloud.entities # package for all Entity alias classes mapper-locations:-classpath:mybatis/mapper/**/*.xml # mapper mapping file spring: application: name : the name serverspringcloud-dept # is very important later if you inject eureka, it is very important datasource: type: com.alibaba.druid.pool.DruidDataSource # current data source operation type driver-class-name: org.gjt.mm.mysql.Driver # mysql driver package url: jdbc:mysql://localhost:3306/cloudDB01 # database name Username: root password: root dbcp2: min-idle: 5 # minimum number of maintained connections for database connection pool initial-size: 5 # number of initialized connections max-total: 5 # maximum number of connections max-wait-millis: 200 # maximum timeout for waiting for connection acquisition
3. MySQL table information
4. DAO interface information
@ Mapperpublic interface DeptDao {public boolean addDept (Dept dept); / / add department public Dept findById (Long id); / / find the department data public List findAll () through id; / / View all departments}
5. Controller layer class
@ RestControllerpublic class DeptController {@ Autowired private DeptService service; / / add departmental interface @ RequestMapping (value = "/ dept/add", method = RequestMethod.POST) public boolean add (@ RequestBody Dept dept) {return service.add (dept) } / / find department information @ RequestMapping (value = "/ dept/get/ {id}", method = RequestMethod.GET) public Dept get (@ PathVariable ("id") Long id) {return service.get (id);} / / find all department information @ RequestMapping (value = "/ dept/list", method = RequestMethod.GET) public List list () {return service.list () }}
6. Testing
First do a small test to see if the database connection is successful and whether the call to the api module is successful.
It means the test is successful!
Fourth, create departmental micro-service consumers
The main steps are shown in the figure
1. Pom.xml file
4.0.0 com.jincou.springcloudrest serverspringcloud 0.0.1-SNAPSHOT serverspringcloud-consumer-dept-80 department micro service consumer com.jincou.springcloudrest serverspringcloud-api ${project.version} org.springframework.boot spring-boot-starter-web Org.springframework springloaded org.springframework.boot spring-boot-devtools
2 、 application.yml
Server: port: 80
3. ConfigBean configuration class
@ Configurationpublic class ConfigBean / / @ Configuration configure ConfigBean = applicationContext.xml {@ Bean public RestTemplate getRestTemplate () {return new RestTemplate ();}}
4. DeptController_Consumer class
@ RestControllerpublic class DeptController_Consumer {private static final String REST_URL_PREFIX = "http://localhost:8001"; / * using restTemplate to access the restful interface is very simple and brainless. (url, requestMap, * ResponseBean.class) these three parameters represent the REST request address, the request parameter, and the object type to which the HTTP response transformation is converted. * / @ Autowired private RestTemplate restTemplate; @ RequestMapping (value = "/ consumer/dept/add") public boolean add (Dept dept) {return restTemplate.postForObject (REST_URL_PREFIX + "/ dept/add", dept, Boolean.class) } @ RequestMapping (value = "/ consumer/dept/get/ {id}") public Dept get (@ PathVariable ("id") Long id) {return restTemplate.getForObject (REST_URL_PREFIX + "/ dept/get/" + id, Dept.class) @ SuppressWarnings ("unchecked") @ RequestMapping (value = "/ consumer/dept/list") public List list () {return restTemplate.getForObject (REST_URL_PREFIX + "/ dept/list", List.class);}}
5. Test
The test is successful, and when I call the consumer interface, it calls the provider's interface again.
At this point, I believe you have a deeper understanding of "SpringCloud based on RestTemplateMicro Service Project case study". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.