更新时间:2024-03-07 gmt 08:00
构建spring cloud工程-j9九游会登录
创建父工程
- 创建maven工程。
图1 创建maven工程
- 父pom添加依赖。
org.springframework.boot spring-boot-dependencies 2.2.8.release pom import
创建子工程servicea
- 创建maven工程。
图2 创建maven工程
- 新建src目录。
图3 新建src目录
- 编写业务代码。
图4 业务代码文件
- 编写启动类
package com.huawei.demo.servicea; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.netflix.eureka.enableeurekaclient; /** * 启动类 * * @author xxx * @since 2023-12-05 */ @springbootapplication @enableeurekaclient public class serviceaspringbootapplication { public static void main(string[] args) { springapplication.run(serviceaspringbootapplication.class, args); } } - 编写controller类
package com.huawei.demo.servicea.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; import com.huawei.demo.servicea.pojo.userinfo; import com.huawei.demo.servicea.service.userservice; /** * 用户对外接口 * * @author xxx * @since 2023-12-06 */ @restcontroller @requestmapping("/user") public class usercontroller { @autowired private userservice userservice; @getmapping("/{userid}") public userinfo getuserbyname(@pathvariable string userid) { return userservice.getuserbyid(userid); } } - 编写mapper类
package com.huawei.demo.servicea.mapper; import org.apache.ibatis.annotations.mapper; import org.apache.ibatis.annotations.select; import com.huawei.demo.servicea.pojo.userinfo; /** * 用户查询 * * @author xxx * @since 2023-12-06 */ @mapper public interface usermapper { @select("select * from demo_user_info where user_id = #{userid}") userinfo getuserbyid(string userid); } - 编写pojo类
package com.huawei.demo.servicea.pojo; import lombok.data; /** * user信息 * * @author xxx * @since 2023-12-06 */ @data public class userinfo { private string userid; private string username; private string phone; private string address; } - 编写service类
package com.huawei.demo.servicea.service; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import com.huawei.demo.servicea.mapper.usermapper; import com.huawei.demo.servicea.pojo.userinfo; /** * userservice * * @author xxx * @since 2023-12-06 */ @service public class userservice { @autowired private usermapper usermapper; public userinfo getuserbyid(string userid) { return usermapper.getuserbyid(userid); } } - 配置微服务
server: port: 8081 spring: application: name: demoservicea datasource: url: jdbc:mysql://127.0.0.1:3306/spring_cloud_demo?useunicode=true&characterencoding=utf-8&servertimezone=asia/shanghai username: root password: *** driver-class-name: com.mysql.jdbc.driver mybatis: configuration: map-underscore-to-camel-case: true type-aliases-package: com.huawei.dmo.servicea
- 编写启动类
创建子工程serviceb
- 创建maven工程。
图5 创建maven工程
- 新建src目录。
图6 新建src目录
- 编写业务代码。
图7 业务代码文件
- 编写启动类
package com.huawei.demo.serviceb; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.loadbalancer.loadbalanced; import org.springframework.cloud.netflix.eureka.enableeurekaclient; import org.springframework.context.annotation.bean; import org.springframework.web.client.resttemplate; /** * 功能描述 * * @author xxx * @since 2023-12-05 */ @springbootapplication @enableeurekaclient public class servicebspringbootapplication { public static void main(string[] args) { springapplication.run(servicebspringbootapplication.class, args); } @bean @loadbalanced public resttemplate resttemplate() { return new resttemplate(); } } - 编写controller类
package com.huawei.demo.serviceb.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; import com.huawei.demo.serviceb.pojo.orderinfo; import com.huawei.demo.serviceb.service.orderservice; /** * 用户对外接口 * * @author xxx * @since 2023-12-06 */ @restcontroller @requestmapping("/order") public class ordercontroller { @autowired private orderservice orderservice; @getmapping("/{orderid}") public orderinfo findorder(@pathvariable string orderid) { return orderservice.findorder(orderid); } } - 编写domain类
package com.huawei.demo.serviceb.domain; import lombok.data; /** * user信息 * * @author xxx * @since 2023-12-06 */ @data public class userinfo { private string userid; private string username; private string phone; private string address; } - 编写mapper类
package com.huawei.demo.serviceb.mapper; import org.apache.ibatis.annotations.mapper; import org.apache.ibatis.annotations.select; import com.huawei.demo.serviceb.pojo.orderinfo; /** * 用户查询 * * @author xxx * @since 2023-12-06 */ @mapper public interface ordermapper { @select("select * from demo_order_info where order_id = #{orderid}") orderinfo getorderbyid(string orderid); } - 编写pojo类
package com.huawei.demo.serviceb.pojo; import com.huawei.demo.serviceb.domain.userinfo; import lombok.data; /** * order信息 * * @author xxx * @since 2023-12-06 */ @data public class orderinfo { private string orderid; private string ordername; private string price; private string userid; private userinfo userinfo; } - 编写service类
package com.huawei.demo.serviceb.service; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.web.client.resttemplate; import com.huawei.demo.serviceb.domain.userinfo; import com.huawei.demo.serviceb.mapper.ordermapper; import com.huawei.demo.serviceb.pojo.orderinfo; /** * 功能描述 * * @author xxx * @since 2023-12-06 */ @service public class orderservice { @autowired private resttemplate resttemplate; @autowired private ordermapper ordermapper; public orderinfo findorder(string orderid) { orderinfo orderinfo=ordermapper.getorderbyid(orderid); string url = "http://demoservicea/user/" orderinfo.getuserid(); userinfo userinfo = resttemplate.getforobject(url, userinfo.class); orderinfo.setuserinfo(userinfo); return orderinfo; } } - 配置微服务
server: port: 8082 spring: application: name: demoserviceb datasource: url: jdbc:mysql://127.0.0.1:3306/spring_cloud_demo?useunicode=true&characterencoding=utf-8&servertimezone=asia/shanghai username: root password: *** driver-class-name: com.mysql.jdbc.driver eureka: client: service-url: defaultzone: http://localhost:11011/eureka/ mybatis: configuration: map-underscore-to-camel-case: true type-aliases-package: com.huawei.dmo.servicea
- 编写启动类
创建eureka注册中心
- 创建eureka微服务。
- 添加依赖。
org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-starter-web - 新建启动类。
图8 新建启动类
- 启动类中增加注解@enableeurekaserver,标明注册中心微服务。
@springbootapplication @enableeurekaserver public class eurekaspringbootapplication { public static void main(string[] args) { springapplication.run(eurekaspringbootapplication.class, args); } } - 在eureka微服务中增加配置文件。
图9 增加配置文件
- 配置注册中心微服务端口等。
server: port: 11011 spring: application: name: eureka eureka: client: register-with-eureka: false fetch-registry: false - 其他微服务中增加注册中心配置。
eureka: client: service-url: defaultzone: http://localhost:11011/eureka/ - 依次启动eureka、demoservicea、demoserviceb,访问http://localhost:11011/,可以看到demoservicea、demoserviceb的信息。
图10 查看微服务注册信息
- 调用接口:http://localhost:8082/order/1。
图11 调用接口
父主题:
相关文档
意见反馈
文档内容是否对您有帮助?
提交成功!非常感谢您的反馈,我们会继续努力做到更好!
您可在查看反馈及问题处理状态。
系统繁忙,请稍后重试
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨