您现在的位置是:主页 > news > 电梯企业网站制作/semi final

电梯企业网站制作/semi final

admin2025/5/9 0:24:20news

简介电梯企业网站制作,semi final,怎样建外贸公司网站,广州美霖室内设计学校feign简介 在上一节的 ribbon的demo中是配置并使用 RestTemplate (或 HttpClient)来访问微服务的api的,这里的feign不需要RestTemplate。Feign 是在 Ribbon的基础上进行了一次改进,采用接口的方式, 只需要创建一个接口…

电梯企业网站制作,semi final,怎样建外贸公司网站,广州美霖室内设计学校feign简介 在上一节的 ribbon的demo中是配置并使用 RestTemplate (或 HttpClient)来访问微服务的api的,这里的feign不需要RestTemplate。Feign 是在 Ribbon的基础上进行了一次改进,采用接口的方式, 只需要创建一个接口…

feign简介

在上一节的 ribbon的demo中是配置并使用 RestTemplate (或 HttpClient)来访问微服务的api的,这里的feign不需要RestTemplate。Feign 是在 Ribbon的基础上进行了一次改进,采用接口的方式, 只需要创建一个接口,然后在上面添加注解即可 ,将需要调用的其他服务的方法定义成抽象方法即可, 不需要自己构建http请求。

撸代码配置使用feign

我这里新建了一个公用的模块名字叫做demo-api,里面放实体类等公用的东西。于是我把feign的service也放在里面作为公用部分(不懂不要急,后面做出解释)。demo-api模块没有yml。

demo-api的pom依赖包部分

我这里用的是2.0.0.M2的feign版本,比较新了。

<dependencies><!-- lombok简化实体类代码 --><!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.0.0.M2</version></dependency></dependencies>

DeptClientService.java

@FeignClient(value = "MICROSERVICE-DEMO")public interface DeptClientService {/*** http://MICROSERVICE-DEMO/接口URI*/@GetMapping("/dept/list")List<Dept> getAll();@PostMapping("/dept/add")boolean add(@RequestBody Dept model);@GetMapping("/dept/findById/{id}")List<Dept> get(@PathVariable("id") Long id);@PostMapping("/dept/delete/{id}")boolean delete(@PathVariable("id") Long id);}

配置客户端

模块的目录如下,遮了的目录是没用的目录,不用纠结了:)

 POM的依赖包部分

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- demo-api --><dependency><groupId>com.djun.demo</groupId><artifactId>demo-api</artifactId><version>${project.version}</version></dependency><!-- feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>${feign.version}</version></dependency><!-- 热部署 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId><version>${springloaded.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><!-- 服务注册 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- Ribbon相关 --><dependency><groupId>com.netflix.ribbon</groupId><artifactId>ribbon-core</artifactId></dependency><dependency><groupId>com.netflix.ribbon</groupId><artifactId>ribbon-httpclient</artifactId></dependency><!-- 熔断机制 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency></dependencies>

application.yml

server:port: 80
spring:application:name: demo-consumer-feign
eureka:client:register-with-eureka: falseservice-url:defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/# defaultZone: http://localhost:7001/eureka/

DeptController_Consumer.java


/*** created by DJun on 2019/7/1*/@RestController
public class DeptController_Consumer {@Resourceprivate DeptClientService service;@GetMapping("/test")public String test(){System.out.println("Hello,World!");return "HELLO,WORLD!";}@GetMapping("/consumer/dept/list")public List<Dept> getAll(){return this.service.getAll();}@PostMapping("/consumer/dept/add")public boolean add( @RequestBody Dept model){return this.service.add(model);}@GetMapping("/consumer/dept/findById/{id}")public List<Dept> get(@PathVariable("id") Long id){return this.service.get(id);}@PostMapping("/consumer/dept/delete/{id}")public boolean delete(@PathVariable("id") Long id){return this.service.delete(id);}}

DeptConsumerFeign80_App.application

这里注意一下下面的这个扫的包,我上次没注意这个问题,让我又掉了不少发丝~

/*** created by DJun on 2019/7/1*/
@SpringBootApplication@EnableEurekaClient
// 开启feign服务
@EnableFeignClients(basePackages = {"com.djun.demo"})public class DeptConsumerFeign80_App {public static void main(String[] args) {SpringApplication.run(DeptConsumerFeign80_App.class,args);}
}

到这里配置就结束了,如果要运行起来的话,你是配置的集群的话,先把eureka的全部集群服务端启动起来,然后在将微服务端启动起来,最后才启动这个消费者端。然后访问80端口。