日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微服务调用组件Feign:简介以及搭建环境

發布時間:2024/4/13 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微服务调用组件Feign:简介以及搭建环境 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>learn_parent</artifactId><groupId>com.learn</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>learn_system</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.learn</groupId><artifactId>learn_common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.learn</groupId><artifactId>learn_common_model</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies> </project> package com.learn.system;import com.learn.common.utils.IdWorker; import com.learn.common.utils.JwtUtils; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;//1.配置springboot的包掃描 @SpringBootApplication(scanBasePackages = "com.learn") //2.配置jpa注解的掃描 @EntityScan(value="com.learn.domain.system") @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class SystemApplication {/*** 啟動方法*/public static void main(String[] args) {SpringApplication.run(SystemApplication.class,args);}@Beanpublic IdWorker idWorker() {return new IdWorker();}@Beanpublic JwtUtils jwtUtils() {return new JwtUtils();}//解決no session@Beanpublic OpenEntityManagerInViewFilter openEntityManagerInViewFilter() {return new OpenEntityManagerInViewFilter();} } package com.learn.company.controller;import com.learn.common.controller.BaseController; import com.learn.common.entity.Result; import com.learn.common.entity.ResultCode; import com.learn.company.service.CompanyService; import com.learn.company.service.DepartmentService; import com.learn.domain.company.Company; import com.learn.domain.company.Department; import com.learn.domain.company.response.DeptListResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;import java.util.List;//1.解決跨域 @CrossOrigin //2.聲明restContoller @RestController //3.設置父路徑 @RequestMapping(value="/company") // company/deparment public class DepartmentController extends BaseController{@Autowiredprivate DepartmentService departmentService;@Autowiredprivate CompanyService companyService;/*** 保存*/@RequestMapping(value="/department",method = RequestMethod.POST)public Result save(@RequestBody Department department) {//1.設置保存的企業id/*** 企業id:目前使用固定值1,以后會解決*/department.setCompanyId(companyId);//2.調用service完成保存企業departmentService.save(department);//3.構造返回結果return new Result(ResultCode.SUCCESS);}/*** 查詢企業的部門列表* 指定企業id*/@RequestMapping(value="/department",method = RequestMethod.GET)public Result findAll() {//1.指定企業idCompany company = companyService.findById(companyId);//2.完成查詢List<Department> list = departmentService.findAll(companyId);//3.構造返回結果DeptListResult deptListResult = new DeptListResult(company,list);return new Result(ResultCode.SUCCESS,deptListResult);}/*** 根據ID查詢department*/@RequestMapping(value="/department/{id}",method = RequestMethod.GET)public Result findById(@PathVariable(value="id") String id) {Department department = departmentService.findById(id);return new Result(ResultCode.SUCCESS,department);}/*** 修改Department*/@RequestMapping(value="/department/{id}",method = RequestMethod.PUT)public Result update(@PathVariable(value="id") String id,@RequestBody Department department) {//1.設置修改的部門iddepartment.setId(id);//2.調用service更新departmentService.update(department);return new Result(ResultCode.SUCCESS);}/*** 根據id刪除*/@RequestMapping(value="/department/{id}",method = RequestMethod.DELETE)public Result delete(@PathVariable(value="id") String id) {departmentService.deleteById(id);return new Result(ResultCode.SUCCESS);}} #注冊到eureka的服務地址 eureka:client:service-url:defaultZone: http://localhost:6868/eureka/ #服務配置 server:port: 9001 #spring配置 spring:#1.應用配置application:name: learn-company #指定服務名#2.數據庫連接池datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8username: rootpassword: 123456#3.JPAjpa:database: MySQLshow-sql: trueopen-in-view: trueredis:host: localhostport: 6379password: 123456 package com.learn.system.client;import com.learn.common.entity.Result; import com.learn.domain.company.Department; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam;/*** 聲明接口,通過feign調用其他微服務*/ //聲明調用的微服務名稱 @FeignClient("learn-company") public interface DepartmentFeignClient {/*** 調用微服務的接口*/@RequestMapping(value="/company/department/{id}",method = RequestMethod.GET)public Result findById(@PathVariable(value="id") String id);@RequestMapping(value="/company/department/search",method = RequestMethod.POST)public Department findByCode(@RequestParam(value="code") String code, @RequestParam(value="companyId") String companyId); }

?

總結

以上是生活随笔為你收集整理的微服务调用组件Feign:简介以及搭建环境的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。