spring cloud - 2
阿新 • • 發佈:2018-11-08
consumer:
1. pom.xml
1) properties
<properties> ...... <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties>
2) dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency>
3) dependencyManagement
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2. config file
1) bootstrap.yml
#http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-zookeeper-config.html
spring:
cloud:
zookeeper:
discovery:
register: false
connectString: 192.168.1.186:2181,192.168.1.187:2181,192.168.1.188:2181
2) application.properties
server.port=8282
spring.application.name=springcloud-consumer
3. discovery client - ON
spring boot main class.
@EnableDiscoveryClient
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class DemoSpringcloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(DemoSpringcloudConsumerApplication.class, args); } }
4. feign class
1) interface
@FeignClient(name = "springcloud-zk") interface TheClient { @RequestMapping(path = "/user/listPage", method = RequestMethod.GET) String findAll(); }
Note:
FeignClient預設只能在方法的RequestMapping 上加全部url路徑, 類的RequestMapping,不能合併到方法上; http://www.jianshu.com/p/191d45210d16全url路徑=class+method
2) impl
public String findAll() { return theClient.findAll(); }
5. invoke feign class
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.example.demo.feign.springcloud_zk.FindAllClient; @RestController public class Springcloud_zk_Controller { @Autowired FindAllClient findAllClient; @RequestMapping(value = "/listPage", method = RequestMethod.GET) public String getUserAll() { return findAllClient.findAll(); } }
Reference:
1. An Intro to Spring Cloud Zookeeper